Class: Markdown::Merge::LinkDefinitionNode
- Inherits:
-
Ast::Merge::AstNode
- Object
- Ast::Merge::AstNode
- Markdown::Merge::LinkDefinitionNode
- 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
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The full original line content.
-
#label ⇒ String
readonly
The link label (reference name).
-
#title ⇒ String?
readonly
Optional title.
-
#url ⇒ String
readonly
The URL.
Class Method Summary collapse
-
.link_definition?(line) ⇒ Boolean
Check if a line looks like a link reference definition.
-
.parse(line, line_number:) ⇒ LinkDefinitionNode?
Parse a line and create a LinkDefinitionNode if it’s a link definition.
-
.parser ⇒ LinkParser
Shared parser instance for parsing link definitions.
Instance Method Summary collapse
-
#children ⇒ Array
TreeHaver::Node protocol: children (none for link definitions).
-
#initialize(content, line_number:, label:, url:, title: nil) ⇒ LinkDefinitionNode
constructor
Initialize a new LinkDefinitionNode.
-
#inspect ⇒ Object
For debugging.
-
#signature ⇒ Array
Generate a signature for matching link definitions.
-
#source_position ⇒ Hash
TreeHaver::Node protocol: source_position.
-
#text ⇒ String
TreeHaver::Node protocol: text.
-
#to_commonmark ⇒ String
Convert to commonmark format (just returns the original content).
-
#type ⇒ Symbol
(also: #merge_type)
TreeHaver::Node protocol: type.
Constructor Details
#initialize(content, line_number:, label:, url:, title: nil) ⇒ LinkDefinitionNode
Initialize a new LinkDefinitionNode
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
#content ⇒ String (readonly)
Returns The full original line content.
45 46 47 |
# File 'lib/markdown/merge/link_definition_node.rb', line 45 def content @content end |
#label ⇒ String (readonly)
Returns The link label (reference name).
36 37 38 |
# File 'lib/markdown/merge/link_definition_node.rb', line 36 def label @label end |
#title ⇒ String? (readonly)
Returns Optional title.
42 43 44 |
# File 'lib/markdown/merge/link_definition_node.rb', line 42 def title @title end |
#url ⇒ String (readonly)
Returns The URL.
39 40 41 |
# File 'lib/markdown/merge/link_definition_node.rb', line 39 def url @url end |
Class Method Details
.link_definition?(line) ⇒ Boolean
Check if a line looks like a link reference definition.
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.
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 |
.parser ⇒ LinkParser
Shared parser instance for parsing link definitions
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
#children ⇒ Array
TreeHaver::Node protocol: children (none for link definitions)
135 136 137 |
# File 'lib/markdown/merge/link_definition_node.rb', line 135 def children [] end |
#inspect ⇒ Object
For debugging
152 153 154 |
# File 'lib/markdown/merge/link_definition_node.rb', line 152 def inspect "#<#{self.class.name} [#{@label}]: #{@url}>" end |
#signature ⇒ Array
Generate a signature for matching link definitions. Link definitions are matched by their label (case-insensitive in Markdown).
118 119 120 |
# File 'lib/markdown/merge/link_definition_node.rb', line 118 def signature [:link_definition, @label.downcase] end |
#source_position ⇒ Hash
TreeHaver::Node protocol: source_position
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 |
#text ⇒ String
TreeHaver::Node protocol: text
141 142 143 |
# File 'lib/markdown/merge/link_definition_node.rb', line 141 def text @content end |
#to_commonmark ⇒ String
Convert to commonmark format (just returns the original content)
147 148 149 |
# File 'lib/markdown/merge/link_definition_node.rb', line 147 def to_commonmark "#{@content}\n" end |
#type ⇒ Symbol Also known as: merge_type
TreeHaver::Node protocol: type
106 107 108 |
# File 'lib/markdown/merge/link_definition_node.rb', line 106 def type :link_definition end |