Class: Markly::Merge::Backend::Node
- Inherits:
-
TreeHaver::Base::Node
- Object
- TreeHaver::Base::Node
- Markly::Merge::Backend::Node
- Defined in:
- lib/markly/merge/backend.rb
Overview
Markly node wrapper
Wraps Markly::Node to provide TreeHaver::Node-compatible interface.
Constant Summary collapse
- TYPE_MAP =
Type normalization map (Markly → canonical)
{ header: "heading", hrule: "thematic_break", html: "html_block", }.freeze
- DEFAULT_SOURCE_POSITION =
Default source position for nodes that don’t have position info
{ start_line: 1, start_column: 1, end_line: 1, end_column: 1, }.freeze
Instance Method Summary collapse
-
#children ⇒ Array<Node>
Get child nodes (Markly uses first_child/next pattern).
-
#end_byte ⇒ Object
-
#end_point ⇒ Object
-
#fence_info ⇒ String?
Get fence info for code blocks.
-
#header_level ⇒ Integer?
Get heading level (1-6).
-
#inner_source_position ⇒ Hash{Symbol => Integer}
private
Get source position from the inner Markly node.
-
#next_sibling ⇒ Node?
Get the next sibling (Markly uses .next).
-
#parent ⇒ Node?
Get the parent node.
-
#prev_sibling ⇒ Node?
Get the previous sibling.
-
#raw_type ⇒ String
Get the raw (non-normalized) type.
-
#start_byte ⇒ Object
Position information.
-
#start_point ⇒ Object
-
#text ⇒ String
Get the text content of this node.
-
#title ⇒ String?
Get title for links/images.
-
#to_commonmark ⇒ Object
Convert node to CommonMark/Markdown/HTML/plaintext.
-
#to_html ⇒ Object
-
#to_markdown ⇒ Object
-
#to_plaintext ⇒ Object
-
#type ⇒ String
Get the node type as a string (normalized).
-
#url ⇒ String?
Get URL for links/images.
Instance Method Details
#children ⇒ Array<Node>
Get child nodes (Markly uses first_child/next pattern)
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/markly/merge/backend.rb', line 270 def children result = [] child = begin inner_node.first_child rescue nil end while child result << Node.new(child, source: source, lines: lines) child = begin child.next rescue nil end end result end |
#end_byte ⇒ Object
295 296 297 298 |
# File 'lib/markly/merge/backend.rb', line 295 def end_byte pos = inner_source_position calculate_byte_offset(pos[:end_line] - 1, pos[:end_column] - 1) end |
#end_point ⇒ Object
305 306 307 308 |
# File 'lib/markly/merge/backend.rb', line 305 def end_point pos = inner_source_position {row: pos[:end_line] - 1, column: pos[:end_column] - 1} end |
#fence_info ⇒ String?
Get fence info for code blocks
342 343 344 345 346 347 348 349 |
# File 'lib/markly/merge/backend.rb', line 342 def fence_info return unless type == "code_block" begin inner_node.fence_info rescue nil end end |
#header_level ⇒ Integer?
Get heading level (1-6)
331 332 333 334 335 336 337 338 |
# File 'lib/markly/merge/backend.rb', line 331 def header_level return unless raw_type == "header" begin inner_node.header_level rescue nil end end |
#inner_source_position ⇒ Hash{Symbol => Integer}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get source position from the inner Markly node
225 226 227 228 229 230 231 |
# File 'lib/markly/merge/backend.rb', line 225 def inner_source_position @inner_source_position ||= if inner_node.respond_to?(:source_position) inner_node.source_position || DEFAULT_SOURCE_POSITION else DEFAULT_SOURCE_POSITION end end |
#next_sibling ⇒ Node?
Get the next sibling (Markly uses .next)
369 370 371 372 373 374 375 376 |
# File 'lib/markly/merge/backend.rb', line 369 def next_sibling sibling = begin inner_node.next rescue nil end sibling ? Node.new(sibling, source: source, lines: lines) : nil end |
#parent ⇒ Node?
Get the parent node
391 392 393 394 395 396 397 398 |
# File 'lib/markly/merge/backend.rb', line 391 def parent p = begin inner_node.parent rescue nil end p ? Node.new(p, source: source, lines: lines) : nil end |
#prev_sibling ⇒ Node?
Get the previous sibling
380 381 382 383 384 385 386 387 |
# File 'lib/markly/merge/backend.rb', line 380 def prev_sibling sibling = begin inner_node.previous rescue nil end sibling ? Node.new(sibling, source: source, lines: lines) : nil end |
#raw_type ⇒ String
Get the raw (non-normalized) type
243 244 245 |
# File 'lib/markly/merge/backend.rb', line 243 def raw_type inner_node.type.to_s end |
#start_byte ⇒ Object
Position information
290 291 292 293 |
# File 'lib/markly/merge/backend.rb', line 290 def start_byte pos = inner_source_position calculate_byte_offset(pos[:start_line] - 1, pos[:start_column] - 1) end |
#start_point ⇒ Object
300 301 302 303 |
# File 'lib/markly/merge/backend.rb', line 300 def start_point pos = inner_source_position {row: pos[:start_line] - 1, column: pos[:start_column] - 1} end |
#text ⇒ String
Get the text content of this node
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/markly/merge/backend.rb', line 250 def text if inner_node.respond_to?(:string_content) content = inner_node.string_content.to_s return content unless content.empty? end if inner_node.respond_to?(:to_plaintext) begin inner_node.to_plaintext rescue children.map(&:text).join end else children.map(&:text).join end end |
#title ⇒ String?
Get title for links/images
361 362 363 364 365 |
# File 'lib/markly/merge/backend.rb', line 361 def title inner_node.title rescue nil end |
#to_commonmark ⇒ Object
Convert node to CommonMark/Markdown/HTML/plaintext
311 312 313 |
# File 'lib/markly/merge/backend.rb', line 311 def to_commonmark inner_node.to_commonmark end |
#to_html ⇒ Object
323 324 325 |
# File 'lib/markly/merge/backend.rb', line 323 def to_html inner_node.to_html end |
#to_markdown ⇒ Object
315 316 317 |
# File 'lib/markly/merge/backend.rb', line 315 def to_markdown inner_node.to_markdown end |
#to_plaintext ⇒ Object
319 320 321 |
# File 'lib/markly/merge/backend.rb', line 319 def to_plaintext inner_node.to_plaintext end |
#type ⇒ String
Get the node type as a string (normalized)
236 237 238 239 |
# File 'lib/markly/merge/backend.rb', line 236 def type raw = inner_node.type.to_s TYPE_MAP[raw.to_sym]&.to_s || raw end |
#url ⇒ String?
Get URL for links/images
353 354 355 356 357 |
# File 'lib/markly/merge/backend.rb', line 353 def url inner_node.url rescue nil end |