pub trait CustomNode:
Debug
+ Send
+ Sync {
// Required methods
fn write(&self, writer: &mut CommonMarkWriter) -> WriteResult<()>;
fn clone_box(&self) -> Box<dyn CustomNode>;
fn eq_box(&self, other: &dyn CustomNode) -> bool;
fn is_block(&self) -> bool;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
// Provided methods
fn html_write(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> { ... }
fn type_name(&self) -> &'static str { ... }
}Expand description
Trait for implementing custom node behavior for the CommonMark AST.
This trait defines methods that all custom node types must implement.
Users can implement the write method for CommonMark output and
optionally override the html_write method for HTML output.
The recommended way to implement this trait is through the custom_node macro,
which provides a default implementation of most methods and requires users to
implement only the node-specific logic.
§Example
use cmark_writer_macros::custom_node;
use cmark_writer::CommonMarkWriter;
use cmark_writer::writer::HtmlWriter;
use cmark_writer::error::WriteResult;
use cmark_writer::writer::HtmlWriteResult;
// Define a custom node with support for both CommonMark and HTML output
#[derive(Debug, Clone, PartialEq)]
#[custom_node(block=false, html_impl=true)]
struct HighlightNode {
content: String,
color: String,
}
impl HighlightNode {
// Required for CommonMark output
fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
writer.write_str("<span style=\"background-color: ")?;
writer.write_str(&self.color)?;
writer.write_str("\">")?;
writer.write_str(&self.content)?;
writer.write_str("</span>")?;
Ok(())
}
// Optional HTML-specific implementation
fn write_html_custom(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> {
writer.start_tag("span")?;
writer.attribute("style", &format!("background-color: {}", self.color))?;
writer.finish_tag()?;
writer.text(&self.content)?;
writer.end_tag("span")?;
Ok(())
}
}Required Methods§
Sourcefn write(&self, writer: &mut CommonMarkWriter) -> WriteResult<()>
fn write(&self, writer: &mut CommonMarkWriter) -> WriteResult<()>
Write the custom node content to the CommonMarkWriter (for CommonMark output).
When using the custom_node macro, this method delegates to the user-defined
write_custom method that must be implemented on the node type.
Sourcefn clone_box(&self) -> Box<dyn CustomNode>
fn clone_box(&self) -> Box<dyn CustomNode>
Clone the custom node
Sourcefn eq_box(&self, other: &dyn CustomNode) -> bool
fn eq_box(&self, other: &dyn CustomNode) -> bool
Check if two custom nodes are equal
Sourcefn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Convert to mutable Any for type casting
Provided Methods§
Sourcefn html_write(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()>
fn html_write(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()>
Writes the HTML representation of the custom node to the provided HTML writer.
By default, this writes an HTML comment indicating that HTML rendering is not implemented
for this custom node type. When using the custom_node macro with html_impl=true,
this method delegates to the user-defined write_html_custom method.
Users should either:
- Override this method directly, or
- Use the
custom_nodemacro withhtml_impl=trueand implement thewrite_html_custommethod.