Module: Jekyll::WebAwesome::CodeBlockTransformer

Defined in:
lib/jekyll/webawesome/code_block_transformer.rb

Overview

Transforms markdown code blocks to Jekyll highlight syntax

Constant Summary collapse

CODE_BLOCK_PATTERN =
/```([a-zA-Z0-9.+#_-]+)?(\n.*?)```/m
@@protected_blocks =

Class variable to store protected blocks across hook calls

{}

Class Method Summary collapse

Class Method Details

.clear_protected_blocksObject



13
14
15
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 13

def clear_protected_blocks
  @@protected_blocks.clear
end

.contains_webawesome_syntax?(content) ⇒ Boolean

Check if a code block contains WebAwesome syntax that should be preserved

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 42

def contains_webawesome_syntax?(content)
  # Check for WebAwesome patterns
  callout_pattern = /^:::(info|success|neutral|warning|danger)/m
  details_pattern = /^\^\^\^/m
  tabs_pattern = /^\+{6}/m

  content.match?(callout_pattern) || content.match?(details_pattern) || content.match?(tabs_pattern)
end

.process(content) ⇒ Object



93
94
95
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 93

def process(content)
  transform_code_blocks(content)
end

.protect_all_code_blocks(content) ⇒ Object

Protect all code blocks by converting them to placeholders This prevents markdown processing from corrupting code blocks inside custom elements



53
54
55
56
57
58
59
60
61
62
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 53

def protect_all_code_blocks(content)
  counter = @@protected_blocks.size

  content.gsub(CODE_BLOCK_PATTERN) do |match|
    placeholder = "<!--PROTECTED_CODE_BLOCK_#{counter}-->"
    @@protected_blocks[placeholder] = match
    counter += 1
    placeholder
  end
end

.restore_protected_blocks(content) ⇒ Object

Restore protected WebAwesome example blocks after WaElementTransformer processing



86
87
88
89
90
91
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 86

def restore_protected_blocks(content)
  @@protected_blocks.each do |placeholder, original_block|
    content = content.gsub(placeholder, original_block)
  end
  content
end

.transform_code_blocks(content) ⇒ Object

Transform code blocks from markdown syntax to Jekyll highlight tags This should be called AFTER WebAwesome transformers have processed the content



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 66

def transform_code_blocks(content)
  # Transform code blocks that were previously protected
  @@protected_blocks.transform_values! do |match|
    if match =~ CODE_BLOCK_PATTERN
      language = Regexp.last_match(1)
      code_content = Regexp.last_match(2).strip

      if language && language.downcase != 'plain'
        "{% highlight #{language} %}\n#{code_content}\n{% endhighlight %}"
      else
        match # Return original block if no language or 'plain'
      end
    else
      match
    end
  end
  content
end

.transform_documents_enabled?(site) ⇒ Boolean

Check if documents transformation is enabled

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 30

def transform_documents_enabled?(site)
  # Check plugin configuration first
  return Jekyll::WebAwesome.configuration.transform_documents if Jekyll::WebAwesome.configuration

  # Check site config
  return site.config.dig('webawesome', 'transform_documents') if site.config.dig('webawesome', 'transform_documents') != nil

  # Default to true
  true
end

.transform_pages_enabled?(site) ⇒ Boolean

Check if pages transformation is enabled

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
# File 'lib/jekyll/webawesome/code_block_transformer.rb', line 18

def transform_pages_enabled?(site)
  # Check plugin configuration first
  return Jekyll::WebAwesome.configuration.transform_pages if Jekyll::WebAwesome.configuration

  # Check site config
  return site.config.dig('webawesome', 'transform_pages') if site.config.dig('webawesome', 'transform_pages') != nil

  # Default to true
  true
end