Attribute Macro custom_node

Source
#[custom_node]
Expand description

Custom node attribute macro, replaces the original derive_custom_node! macro

This macro automatically implements the CustomNode trait. Users can specify whether the node is a block element using the block parameter.

ยงExample

use cmark_writer_macros::custom_node;

// Specified as an inline element
#[derive(Debug, Clone, PartialEq)]
#[custom_node(block=false)]
struct HighlightNode {
    content: String,
    color: String,
}

impl HighlightNode {
    fn write_custom(&self, writer: &mut dyn CustomNodeWriter) -> 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(())
    }
}

// Specified as a block element
#[derive(Debug, Clone, PartialEq)]
#[custom_node(block=true)]
struct AlertNode {
    content: String,
}

impl AlertNode {
    fn write_custom(&self, writer: &mut dyn CustomNodeWriter) -> WriteResult<()> {
        writer.write_str("<div class=\"alert\">")?;
        writer.write_str(&self.content)?;
        writer.write_str("</div>")?;
        Ok(())
    }
}