cmark-writer 0.5.0

A CommonMark writer implementation in Rust for serializing AST nodes to CommonMark format
Documentation

cmark-writer

CI Status Crates.io License: MIT Downloads

A CommonMark writer implementation in Rust.

Usage

Basic Example

use cmark_writer::ast::{Node, ListItem};
use cmark_writer::writer::CommonMarkWriter;

// Create a document
let document = Node::Document(vec![
    Node::Heading {
        level: 1,
        content: vec![Node::Text("Hello CommonMark".to_string())],
    },
    Node::Paragraph(vec![
        Node::Text("This is a simple ".to_string()),
        Node::Strong(vec![Node::Text("example".to_string())]),
        Node::Text(".".to_string()),
    ]),
]);

// Render to CommonMark
let mut writer = CommonMarkWriter::new();
writer.write(&document).expect("Failed to write document");
let markdown = writer.into_string();

println!("{}", markdown);

Custom Formatting Options

You can customize the formatting behavior:

use cmark_writer::options::WriterOptions;
use cmark_writer::writer::CommonMarkWriter;
use cmark_writer::ast::Node;

// Create custom options
let options = WriterOptions {
    strict: true,                // Follow CommonMark spec strictly
    hard_break_spaces: false,    // Use backslash for line breaks
    indent_spaces: 2,            // Use 2 spaces for indentation
};

// Create writer with custom options
let mut writer = CommonMarkWriter::with_options(options);
writer.write(&Node::Text("Example".to_string())).unwrap();

API Documentation

Core Types

  • Node - Represents various CommonMark node types
  • ListItem - Represents list items, including task list items
  • CommonMarkWriter - Converts nodes to CommonMark text
  • WriterOptions - Customization options for the writer

Creating Custom Nodes

You can extend the CommonMark syntax with your own custom nodes:

use cmark_writer::ast::{CustomNodeWriter, Node};
use cmark_writer::error::WriteResult;
use cmark_writer::derive_custom_node;

// Define a custom highlight node
#[derive(Debug, Clone, PartialEq)]
struct HighlightNode {
    content: String,
    color: String,
}

// Implement the custom node using the macro
derive_custom_node!(HighlightNode);

// Implement the required methods
impl HighlightNode {
    // Custom node writing logic
    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(())
    }
    
    // Determine if it's a block or inline element
    fn is_block_custom(&self) -> bool {
        false // This is an inline element
    }
}

// Use your custom node
let document = Node::Document(vec![
    Node::Paragraph(vec![
        Node::Text("Here's some text with a ".to_string()),
        Node::Custom(Box::new(HighlightNode {
            content: "highlighted section".to_string(),
            color: "yellow".to_string(),
        })),
        Node::Text(".".to_string()),
    ]),
]);

Development

Building

cargo build

Running Tests

cargo test

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.