pub trait CustomNode:
Debug
+ Send
+ Sync {
// Required methods
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 write_block(&self, writer: &mut BlockWriterProxy<'_>) -> WriteResult<()> { ... }
fn write_inline(
&self,
writer: &mut InlineWriterProxy<'_>,
) -> WriteResult<()> { ... }
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 dedicated block or inline rendering methods 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 ecow::EcoString;
use cmark_writer_macros::custom_node;
use cmark_writer::error::WriteResult;
use cmark_writer::writer::{HtmlWriteResult, HtmlWriter, InlineWriterProxy};
// 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: EcoString,
color: EcoString,
}
impl HighlightNode {
// Required for CommonMark output
fn write_custom(&self, writer: &mut InlineWriterProxy) -> 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 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 write_block(&self, writer: &mut BlockWriterProxy<'_>) -> WriteResult<()>
fn write_block(&self, writer: &mut BlockWriterProxy<'_>) -> WriteResult<()>
Write the custom node as a block element using the restricted block writer proxy.
Block custom nodes should implement this method to emit valid block-level content.
Sourcefn write_inline(&self, writer: &mut InlineWriterProxy<'_>) -> WriteResult<()>
fn write_inline(&self, writer: &mut InlineWriterProxy<'_>) -> WriteResult<()>
Write the custom node as an inline element using the restricted inline writer proxy.
Inline custom nodes should implement this method to emit valid inline content.
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.