#[custom_node]Expand description
Custom node attribute macro for implementing the CustomNode trait
This macro automatically implements the CustomNode trait. Users can specify
whether the node is a block element using the block parameter and whether
it implements HTML rendering with the html_impl parameter.
ยงExample
use cmark_writer::writer::{BlockWriterProxy, HtmlWriteResult, HtmlWriter, InlineWriterProxy};
use cmark_writer::{custom_node, WriteResult};
use ecow::EcoString;
// Specified as an inline element with both CommonMark and HTML implementations
#[derive(Debug, Clone, PartialEq)]
#[custom_node(block=false, html_impl=true)]
struct HighlightNode {
content: EcoString,
color: EcoString,
}
impl HighlightNode {
// Required for CommonMark rendering
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 rendering 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(())
}
}
// Only CommonMark implementation, default HTML implementation
#[derive(Debug, Clone, PartialEq)]
#[custom_node(block=true)]
struct AlertNode {
content: EcoString,
}
impl AlertNode {
fn write_custom(&self, writer: &mut BlockWriterProxy) -> WriteResult<()> {
writer.write_str("<div class=\"alert\">")?;
writer.write_str(&self.content)?;
writer.write_str("</div>")?;
Ok(())
}
}