Class: JekyllCommonMarkCustomRenderer

Inherits:
CommonMarker::HtmlRenderer
  • Object
show all
Defined in:
lib/jekyll-commonmark-ghpages.rb

Overview

A customized version of CommonMarker::HtmlRenderer which:

  • outputs Kramdown-style header IDs.

  • performs syntax highlighting in code blocks.

Instance Method Summary collapse

Instance Method Details

#code_block(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll-commonmark-ghpages.rb', line 27

def code_block(node)
  lang = if node.fence_info && !node.fence_info.empty?
           node.fence_info.split(/[\s,]/)[0]
         end

  content = node.string_content

  if lang && lexer = ::Rouge::Lexer.find_fancy(lang, content)
    block do
      out("<div class=\"language-#{lang} highlighter-rouge\">")
      out(::Rouge::Formatters::HTMLLegacy.new(css_class: 'highlight').format(lexer.lex(content)))
      out("</div>")
    end
    return
  end

  if option_enabled?(:GITHUB_PRE_LANG)
    out("<pre#{sourcepos(node)}")
    out(' lang="', lang, '"') if lang
    out('><code>')
  else
    out("<pre#{sourcepos(node)}><code")
    if lang
      out(' class="language-', lang, '">')
    else
      out('>')
    end
  end
  out(escape_html(content))
  out('</code></pre>')
end

#header(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jekyll-commonmark-ghpages.rb', line 13

def header(node)
  block do
    old_stream = @stream
    @stream = StringIO.new(String.new.force_encoding("utf-8"))
    out(:children)
    content = @stream.string
    @stream = old_stream

    id = generate_id(content)
    out("<h", node.header_level, "#{sourcepos(node)} id=\"#{id}\">",
        content, "</h", node.header_level, ">")
  end
end