Class: Markdown::Merge::LinkReferenceRehydrator
- Inherits:
-
Object
- Object
- Markdown::Merge::LinkReferenceRehydrator
- Defined in:
- lib/markdown/merge/link_reference_rehydrator.rb
Overview
Rehydrates inline links and images to use link reference definitions.
When markdown is processed through to_commonmark, reference-style links
[text][label] are converted to inline links [text](url).
This class reverses that transformation by:
- Parsing link reference definitions from content using LinkParser
- Finding inline links/images using LinkParser's PEG-based parsing
- Replacing inline URLs with reference labels where a definition exists
Uses Parslet-based parsing for robust handling of:
- Emoji in labels (e.g.,
[🖼️galtzo-discord]) - Nested brackets (for linked images like
[![alt][ref]](url)) - Multi-byte UTF-8 characters
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The original content.
-
#problems ⇒ DocumentProblems
readonly
Problems found during rehydration.
-
#rehydration_count ⇒ Integer
readonly
Get count of links/images rehydrated.
Class Method Summary collapse
-
.rehydrate(content) ⇒ String
Rehydrate inline links/images to reference style (class method).
Instance Method Summary collapse
-
#changed? ⇒ Boolean
Check if rehydration made any changes.
-
#duplicate_definitions ⇒ Hash<String, Array<String>>
Get duplicate definitions (multiple labels for same URL).
-
#initialize(content) ⇒ LinkReferenceRehydrator
constructor
Initialize a new rehydrator.
-
#link_definitions ⇒ Hash<String, String>
Get the map of URLs to their preferred label.
-
#rehydrate ⇒ String
Rehydrate inline links and images to use reference definitions.
Constructor Details
#initialize(content) ⇒ LinkReferenceRehydrator
Initialize a new rehydrator.
48 49 50 51 52 53 54 55 56 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 48 def initialize(content) @content = content @problems = DocumentProblems.new @link_definitions = nil @duplicate_definitions = nil @url_to_label = nil @parser = LinkParser.new @rehydration_count = 0 end |
Instance Attribute Details
#content ⇒ String (readonly)
Returns The original content.
30 31 32 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 30 def content @content end |
#problems ⇒ DocumentProblems (readonly)
Returns Problems found during rehydration.
33 34 35 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 33 def problems @problems end |
#rehydration_count ⇒ Integer (readonly)
Get count of links/images rehydrated.
125 126 127 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 125 def rehydration_count @rehydration_count end |
Class Method Details
.rehydrate(content) ⇒ String
Rehydrate inline links/images to reference style (class method).
40 41 42 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 40 def rehydrate(content) new(content).rehydrate end |
Instance Method Details
#changed? ⇒ Boolean
Check if rehydration made any changes.
118 119 120 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 118 def changed? @rehydration_count.positive? end |
#duplicate_definitions ⇒ Hash<String, Array<String>>
Get duplicate definitions (multiple labels for same URL).
69 70 71 72 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 69 def duplicate_definitions build_definition_maps unless @duplicate_definitions @duplicate_definitions end |
#link_definitions ⇒ Hash<String, String>
Get the map of URLs to their preferred label.
61 62 63 64 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 61 def link_definitions build_definition_maps unless @link_definitions @link_definitions end |
#rehydrate ⇒ String
Rehydrate inline links and images to use reference definitions.
Uses a tree-based approach to handle nested structures like linked images
[](link-url). The parser builds a tree of link constructs,
and we process them in leaf-first (post-order) traversal to ensure
inner replacements are applied before outer ones.
For linked images, this means:
- First, the inner image
is replaced with![alt][img-label] - Then, the outer link's text is updated to include the replaced image
- Finally, the outer link
[![alt][img-label]](link-url)is replaced with[![alt][img-label]][link-label]
This is done in a single pass by tracking replacement offsets.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/markdown/merge/link_reference_rehydrator.rb', line 89 def rehydrate build_definition_maps unless @link_definitions record_duplicate_problems return content if @url_to_label.empty? # Use the new tree-based approach # 1. Find all link constructs with proper nesting detection tree = @parser.find_all_link_constructs(content) # 2. Collect all replacements using recursive tree processing # This properly handles nested structures by processing children first # and adjusting parent text to include child replacements replacements = collect_nested_replacements(tree, content) # 3. Apply replacements in reverse position order result = content.dup replacements.sort_by { |r| -r[:start_pos] }.each do |replacement| result = result[0...replacement[:start_pos]] + replacement[:replacement] + result[replacement[:end_pos]..] end result end |