Class: DocsKit::MarkdownExport::Inline

Inherits:
Object
  • Object
show all
Defined in:
lib/docs_kit/markdown_export/inline.rb

Overview

The inline pass: a node's descendants flattened to a single run of Markdown text (bold/italic/code/links). Block elements never reach here; an unknown inline element recurses into its children so its text survives.

Constant Summary collapse

WRAP =

Emphasis tags whose Markdown just wraps the inner text in a delimiter. Keeps #render_node a flat dispatch instead of a branch per tag.

{
  "strong" => "**", "b" => "**",
  "em" => "*", "i" => "*",
  "del" => "~~", "s" => "~~"
}.freeze

Instance Method Summary collapse

  • #render_node(node) ⇒ Object

    Dispatch one node.

  • Constructor Details

    #initialize(export) ⇒ Inline

    Returns a new instance of Inline.

    
    
    17
    18
    19
    # File 'lib/docs_kit/markdown_export/inline.rb', line 17
    
    def initialize(export)
      @export = export
    end

    Instance Method Details

    #element(node, name) ⇒ Object

    The non-emphasis inline elements. code keeps its text verbatim; a becomes a link; a decorative "#" span (Section's hover anchor) is dropped; any other wrapper recurses so its text survives.

    
    
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    # File 'lib/docs_kit/markdown_export/inline.rb', line 44
    
    def element(node, name)
      case name
      when "code" then code_span(node.text)
      when "a" then link(node)
      when "img" then image(node)
      when "br" then "  \n"
      when "span" then anchor_decoration?(node) ? "" : render(node)
      else render(node)
      end
    end

    #heading_text(node) ⇒ Object

    A heading's inline text, treating a self-referencing anchor (href="#id", DocsUI::Section's deep-link wrapper) as transparent — its children render directly, so a heading is ## Title, never ## [Title](#id).

    
    
    62
    63
    64
    65
    66
    # File 'lib/docs_kit/markdown_export/inline.rb', line 62
    
    def heading_text(node)
      node.children.map do |child|
        self_anchor?(child) ? render(child) : render_node(child)
      end.join.strip
    end

    #image(node) ⇒ Object

    
    
    55
    56
    57
    # File 'lib/docs_kit/markdown_export/inline.rb', line 55
    
    def image(node)
      "![#{node['alt']}](#{@export.absolutize(node['src'])})"
    end

    #render(node_or_set) ⇒ Object

    Inline-render a node OR a Nokogiri node-set (an

  • 's inline children).

  • 
    
    22
    23
    24
    25
    # File 'lib/docs_kit/markdown_export/inline.rb', line 22
    
    def render(node_or_set)
      nodes = node_or_set.respond_to?(:children) ? node_or_set.children : node_or_set
      nodes.map { |child| render_node(child) }.join
    end

    #render_node(node) ⇒ Object

    Dispatch one node. Text is returned verbatim (Nokogiri decoded entities, so </& are literal — GFM keeps them). Elements map to their inline Markdown; the DocsUI::Section hover-anchor "#" span is dropped.

    
    
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    # File 'lib/docs_kit/markdown_export/inline.rb', line 30
    
    def render_node(node)
      return node.text if node.text?
      return "" unless node.element?
    
      name = node.name
      wrap = WRAP[name]
      return "#{wrap}#{render(node)}#{wrap}" if wrap
    
      element(node, name)
    end