Class: Markdown::Merge::LinkDefinitionNode

Inherits:
Ast::Merge::AstNode
  • Object
show all
Defined in:
lib/markdown/merge/link_definition_node.rb

Overview

Represents a link reference definition that was consumed by the Markdown parser.

Markdown parsers like Markly (libcmark-gfm) consume link reference definitions during parsing and resolve them into inline links. This means they don’t appear as nodes in the AST. This class represents these “consumed” definitions so they can be preserved during merge operations.

Link reference definitions have the form:

[label]: url "optional title"
[label]: url 'optional title'
[label]: url (optional title)
[label]: <url> "optional title"

Uses LinkParser for robust parsing that handles:

  • Emoji in labels (e.g., ‘[🖼️galtzo-discord]`)

  • Multi-byte UTF-8 characters

  • Nested brackets in labels

Examples:

node = LinkDefinitionNode.new(
  "[ref]: https://example.com",
  line_number: 10,
  label: "ref",
  url: "https://example.com"
)
node.type        # => :link_definition
node.label       # => "ref"
node.url         # => "https://example.com"
node.signature   # => [:link_definition, "ref"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, line_number:, label:, url:, title: nil) ⇒ LinkDefinitionNode

Initialize a new LinkDefinitionNode

Parameters:

  • content (String)

    The full line content

  • line_number (Integer)

    1-based line number

  • label (String)

    The link label

  • url (String)

    The URL

  • title (String, nil) (defaults to: nil)

    Optional title



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/markdown/merge/link_definition_node.rb', line 54

def initialize(content, line_number:, label:, url:, title: nil)
  @content = content
  @label = label
  @url = url
  @title = title

  location = Ast::Merge::AstNode::Location.new(
    start_line: line_number,
    end_line: line_number,
    start_column: 0,
    end_column: content.length,
  )

  super(slice: content, location: location)
end

Instance Attribute Details

#contentString (readonly)

Returns The full original line content.

Returns:

  • (String)

    The full original line content



45
46
47
# File 'lib/markdown/merge/link_definition_node.rb', line 45

def content
  @content
end

#labelString (readonly)

Returns The link label (reference name).

Returns:

  • (String)

    The link label (reference name)



36
37
38
# File 'lib/markdown/merge/link_definition_node.rb', line 36

def label
  @label
end

#titleString? (readonly)

Returns Optional title.

Returns:

  • (String, nil)

    Optional title



42
43
44
# File 'lib/markdown/merge/link_definition_node.rb', line 42

def title
  @title
end

#urlString (readonly)

Returns The URL.

Returns:

  • (String)

    The URL



39
40
41
# File 'lib/markdown/merge/link_definition_node.rb', line 39

def url
  @url
end

Class Method Details

Check if a line looks like a link reference definition.

Parameters:

  • line (String)

    The line to check

Returns:

  • (Boolean)

    true if line matches link definition pattern



99
100
101
# File 'lib/markdown/merge/link_definition_node.rb', line 99

def link_definition?(line)
  !parser.parse_definition_line(line.strip).nil?
end

.parse(line, line_number:) ⇒ LinkDefinitionNode?

Parse a line and create a LinkDefinitionNode if it’s a link definition.

Parameters:

  • line (String)

    The line content

  • line_number (Integer)

    1-based line number

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/markdown/merge/link_definition_node.rb', line 82

def parse(line, line_number:)
  result = parser.parse_definition_line(line.chomp)
  return unless result

  new(
    line.chomp,
    line_number: line_number,
    label: result[:label],
    url: result[:url],
    title: result[:title],
  )
end

.parserLinkParser

Shared parser instance for parsing link definitions

Returns:



73
74
75
# File 'lib/markdown/merge/link_definition_node.rb', line 73

def parser
  @parser ||= LinkParser.new # rubocop:disable ThreadSafety/ClassInstanceVariable
end

Instance Method Details

#childrenArray

TreeHaver::Node protocol: children (none for link definitions)

Returns:

  • (Array)

    Empty array



135
136
137
# File 'lib/markdown/merge/link_definition_node.rb', line 135

def children
  []
end

#inspectObject

For debugging



152
153
154
# File 'lib/markdown/merge/link_definition_node.rb', line 152

def inspect
  "#<#{self.class.name} [#{@label}]: #{@url}>"
end

#signatureArray

Generate a signature for matching link definitions. Link definitions are matched by their label (case-insensitive in Markdown).

Returns:

  • (Array)

    Signature array [:link_definition, lowercase_label]



118
119
120
# File 'lib/markdown/merge/link_definition_node.rb', line 118

def signature
  [:link_definition, @label.downcase]
end

#source_positionHash

TreeHaver::Node protocol: source_position

Returns:

  • (Hash)

    Position info for source extraction



124
125
126
127
128
129
130
131
# File 'lib/markdown/merge/link_definition_node.rb', line 124

def source_position
  {
    start_line: @location.start_line,
    end_line: @location.end_line,
    start_column: @location.start_column,
    end_column: @location.end_column,
  }
end

#textString

TreeHaver::Node protocol: text

Returns:

  • (String)

    The full line content



141
142
143
# File 'lib/markdown/merge/link_definition_node.rb', line 141

def text
  @content
end

#to_commonmarkString

Convert to commonmark format (just returns the original content)

Returns:

  • (String)

    The link definition line



147
148
149
# File 'lib/markdown/merge/link_definition_node.rb', line 147

def to_commonmark
  "#{@content}\n"
end

#typeSymbol Also known as: merge_type

TreeHaver::Node protocol: type

Returns:

  • (Symbol)

    :link_definition



106
107
108
# File 'lib/markdown/merge/link_definition_node.rb', line 106

def type
  :link_definition
end