Class: Yard::Fence::KramdownGfmDocument

Inherits:
Kramdown::Document show all
Defined in:
lib/yard/fence/kramdown_gfm_document.rb,
sig/yard/fence.rbs

Constant Summary collapse

UNRENDERED_FENCE_PARAGRAPH =

From implementation: stores original source and forces GFM input by default

Returns:

  • (::Regexp)
/<p>```/
DETAILS_MARKDOWN_1 =

Returns:

  • (::Regexp)
/<details[^>]*markdown=["']1["'][^>]*>/i

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ KramdownGfmDocument

Returns a new instance of KramdownGfmDocument.

Parameters:

  • source (String)
  • options (Hash[Symbol, untyped]) (defaults to: {})


26
27
28
29
30
# File 'lib/yard/fence/kramdown_gfm_document.rb', line 26

def initialize(source, options = {})
  options[:input] = "GFM" unless options.key?(:input)
  @__yard_fence_source = source # Keep original for potential fallback.
  super
end

Instance Method Details

#fallback_improved?(fb_html) ⇒ Boolean

Parameters:

  • fb_html (String)

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/yard/fence/kramdown_gfm_document.rb', line 62

def fallback_improved?(fb_html)
  fb_html.match?(UNRENDERED_FENCE_PARAGRAPH) == false &&
    fb_html.include?("<pre") && fb_html.include?("<code")
end

#needs_fallback?(html) ⇒ Boolean

Parameters:

  • html (String)

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
# File 'lib/yard/fence/kramdown_gfm_document.rb', line 51

def needs_fallback?(html)
  # Obvious failure: raw fenced code rendered as a paragraph
  return true if html.match?(UNRENDERED_FENCE_PARAGRAPH)
  # Edge case: details wrapper present in source but output lacks any code block
  if @__yard_fence_source.match?(DETAILS_MARKDOWN_1)
    has_code_block = html.include?("<pre") && html.include?("<code")
    return !has_code_block
  end
  false
end

#to_htmlString

Public API

Returns:

  • (String)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yard/fence/kramdown_gfm_document.rb', line 36

def to_html
  html = super
  return html if ENV["YARD_FENCE_DISABLE_FALLBACK"] == "1"
  return html unless @__yard_fence_source.include?("```")

  if needs_fallback?(html)
    fallback_options = @options.merge(input: "kramdown")
    fb_html = Kramdown::Document.new(@__yard_fence_source, fallback_options).to_html
    return fb_html if fallback_improved?(fb_html)
  end
  html
end