Cmark::Node
A node is a construct of the abstract syntax tree (AST) for a given markdown content.
At the very minimum every node has a NodeType —available via #type— even when invalid.
However, given that each type of node has a set of required properties
—which if are not set may lead to unintended behavior—
all initializers of Node are protected. Nodes can be created
in a safer manner either with the module NodeMaker, or with the parsing
methods of the moduleCmark (which return a node with NodeType::Document).
Instance methods can be grouped into:
-
Tree traversal
#next#previous#parent#grandparent#first_child#last_child#children
-
Accessors
#type#literal#literal=#heading_level#heading_level=#list_type#list_type=#list_delim#list_delim=#list_start#list_start=#list_tight?#list_tight=#fence_info#fence_info=#fencing_details#fencing_details=#url#url=#title#title=#on_enter#on_enter=#on_exit#on_exit=#user_data#user_data=#start_line#end_line#start_column#end_column#autolink?#table_string_content#table_string_content=#table_columns#table_columns=#table_alignments#table_alignments=#table_row_header?#table_row_header=#tasklist_item?#tasklist_item=#tasklist_item_checked?#tasklist_item_checked=#footnote_reference_index#footnote_definition_count#footnote_definition_literal#footnote_parent_definition_literal
-
Tree manipulation
#unlink#insert_before#insert_after#replace_with#prepend_child#append_child#consolidate_text_nodes
-
Rendering
#render_xml#render_html#render_plaintext#render_commonmark#render_latex#render_man
-
Equality
#==
-
Containment
#can_contain?
NOTE: The Node class is a thin wrapper for the node struct of the
underlying cmark-gfm C library, as such, familiarity with the
GFM Spec is required for advanced
node creation, processing, and rendering.
Instance methods
Adds child to the end of the children of node; raises TreeManipulationError on failure.
Returns true if the node is a Commonmark autolink, false otherwise.
Beware that this method consolidates adjacent child text nodes if the node is a link.
NOTE: When the GFM autolink extension is enabled, this method will not detect
extended www autolink nor
extended autolink path validation.
However, extended email autolink
together with simple autolinks constructed without the use of < and > as delimiters will
be recognized as autolinks.
Returns true node can contain node_type, false otherwise.
Sets the info string of a fenced code block; on failure it raises NodeSetterError.
Returns fencing details of a code block.
Returns nil if called on a node that is not a code block.
Sets fencing details of a code block; on failure it raises NodeSetterError.
Returns the count the footnote definition.
Returns 0 if the node is not a footnote definition.
Returns the definition literal of the footnote.
Returns an empty string if the node is not a footnote definition.
Returns the definition literal of the footnote for this reference.
Returns an empty string if the node is not a footnote reference.
Returns the index of the footnote reference.
Returns 0 if the node is not a footnote reference.
Sets the heading level of node; on failure it raises NodeSetterError.
Inserts sibling after node; raises TreeManipulationError on failure.
Inserts sibling before node; raises TreeManipulationError on failure.
Sets the list delimiter of node; on failure it raises NodeSetterError.
Sets the list delimiter of node; on failure it raises NodeSetterError.
Sets the list tightness of node; on failure it raises NodeSetterError.
Sets the list type of node; on failure it raises NodeSetterError.
Sets the literal string contents of node; on failure it raises NodeSetterError.
Sets the literal on_enter of a custom node; on failure it raises NodeSetterError.
Any children of the node will be rendered after this text.
Returns the literal on_exit of a custom node or an empty string if unset.
Returns nil if called on a node that is not custom.
Sets the literal on_exit of a custom node; on failure it raises NodeSetterError.
Any children of the node will be rendered before this text.
Adds child to the beginning of the children of node; raises TreeManipulationError on failure.
Renders node tree as a commonmark string, including GFM extension nodes, if present.
Renders node tree as an HTML string.
Renders node tree as a LaTeX string.
Renders node tree as a groff man page string.
Renders node tree as a plaintext string.
Replaces node with new_node; raises TreeManipulationError on failure.
Returns the alignments of columns for the table.
Returns an empty array if called on a node that is not a table.
Sets the alignments of columns for the table; on failure it raises NodeSetterError.
It should be called only on table nodes.
Example for a table node with two columns:
node.table_alignments = [Alignment::Center, Alignment::Right]
Returns node table alignment or Alignment::None if called on a node that is not a table cell.
Returns the number of columns for the table.
Returns 0 if called on a node that is not a table.
Sets the number of columns for the table; on failure it raises NodeSetterError.
It must be called only on table and table row nodes that already have defined columns.
Returns the string content of tables and table cells, otherwise an empty string.
With the following table
| foo | bar |
| --- | --- |
| baz | bim |
the string content of table is the header row | foo | bar |. and the
string content of the cells is foo, bar, baz, and bim.
Sets the table string content for tables and table cells.
Beware of setting incorrect string content.
See #table_string_content for more information.
Sets a list item as tasklist item; on failure it raises NodeSetterError.
It must be called only on nodes with NodeType::Item.
Sets the checked status of tasklist item: on failure it raises NodeSetterError.
Returns true is a node is a tasklist_item and is checked, false otherwise.
Sets the title of a link or image node; on failure it raises NodeSetterError.
Sets the URL of node; on failure it raises NodeSetterError.
It must be called only on link or image nodes.
Returns the user data of node, which can then be unboxed.
Example:
node = NodeMaker.text("Hi")
node.user_data.null? # => true
node.user_data = Box.box(:greetings)
user_data = Box(Symbol).unbox(node.user_data)
user_data # => :grettings