Module: YARD::Markdown::DocumentationHelper

Defined in:
lib/yard/markdown/documentation_helper.rb

Overview

Converts YARD docstrings into Markdown-friendly text.

Instance Method Summary collapse

Instance Method Details

#documented_text(object) ⇒ String

Returns the rendered documentation text for an object.

Parameters:

  • object (YARD::CodeObjects::Base)

    Object whose docstring is being rendered.

Returns:

  • (String)

    Converted documentation text or a fallback message.



13
14
15
16
17
18
19
# File 'lib/yard/markdown/documentation_helper.rb', line 13

def documented_text(object)
  text = rdoc_to_md(object.docstring)
  return text unless text.empty?
  return "" unless object.tags.empty?

  "Not documented."
end

#rdoc_to_md(docstring) ⇒ String

Converts an RDoc-formatted docstring to Markdown.

Parameters:

  • docstring (Object)

    Raw docstring content.

Returns:

  • (String)

    Markdown-rendered docstring content.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/yard/markdown/documentation_helper.rb', line 25

def rdoc_to_md(docstring)
  fenced_code_blocks = []
  placeholder = "YARD_MARKDOWN_FENCED_CODE_BLOCK_%d"
  content = docstring.gsub(/^```[^\n]*\n.*?^```[ \t]*$/m) do |block|
    fenced_code_blocks << block
    format(placeholder, fenced_code_blocks.length - 1)
  end

  markdown = RDoc::Markup::ToMarkdown.new.convert(content).rstrip
  fenced_code_blocks.each_with_index do |block, index|
    markdown = markdown.sub(format(placeholder, index), block)
  end

  markdown
end