class Fragment
A fragment of a Markly AST document.
Wraps a Markly::Node of type :document and provides rendering helpers.
Used for both slide documents and presenter notes so callers can choose
their output format without the parser pre-committing to one.
Definitions
EXTENSIONS = [:table, :tasklist, :strikethrough, :autolink]
Markly extensions enabled for all slide Markdown rendering.
def initialize(node)
Initialize a fragment from a Markly document node.
Signature
-
parameter
nodeMarkly::Node A document node containing the fragment content.
Implementation
def initialize(node)
@node = node
end
attr :node
Signature
-
attribute
Markly::Node The underlying AST document node.
def initialize_copy(other)
Duplicate this fragment and its underlying AST.
Signature
-
returns
Fragment An independently mutable copy.
Implementation
def initialize_copy(other)
super
@node = other.node.dup
end
def empty?
Whether the fragment has no content.
Signature
-
returns
Boolean
Implementation
def empty?
@node.first_child.nil?
end
def to_html
Render the fragment to HTML.
Signature
-
returns
String The rendered HTML.
Implementation
def to_html
Markly::Renderer::HTML.new(flags: Markly::UNSAFE, extensions: EXTENSIONS).render(@node)
end
def extract(name)
Extract the content belonging to a named H2 placeholder.
The matching heading itself is removed. Following nodes are extracted up to the next heading of the same or a higher level. Lower-level headings remain part of the extracted content.
Signature
-
parameter
nameString The heading name to extract.
-
returns
Fragment | Nil The extracted content, or
nilwhen not found.
Implementation
def extract(name)
heading = find_heading(name)
return unless heading
level = heading.header_level
fragment = Markly::Node.new(:document)
node = heading.next
heading.delete
while node
next_node = node.next
break if node.type == :header && node.header_level <= level
fragment.append_child(node)
node = next_node
end
Fragment.new(fragment)
end
def extract_heading(level = 1)
Extract the first heading at the given level, retaining the heading node.
Signature
-
parameter
levelInteger The Markdown heading level.
-
returns
Fragment | Nil A fragment containing the extracted heading.
Implementation
def extract_heading(level = 1)
heading = nil
@node.each do |node|
if node.type == :header && node.header_level == level
heading = node
break
end
end
return unless heading
fragment = Markly::Node.new(:document)
fragment.append_child(heading)
Fragment.new(fragment)
end
def to_commonmark
Render the fragment back to CommonMark Markdown.
Signature
-
returns
String The CommonMark source.
Implementation
def to_commonmark
@node.to_commonmark
end
def heading_text(level = 1)
Return the plain text of the first heading at the given level.
Signature
-
parameter
levelInteger The Markdown heading level.
-
returns
String | Nil The heading text, or
nilwhen not found.
Implementation
def heading_text(level = 1)
@node.each do |node|
if node.type == :header && node.header_level == level
return node.dup.extract_children.to_plaintext
end
end
end