Crate cmark_writer

Source
Expand description

§cmark-writer

CI Status Crates.io License: MIT Downloads Codecov

A CommonMark writer implementation in Rust.

§Basic Usage

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

// Create a document
let document = Node::Document(vec![
    Node::heading(1, 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 Options

use cmark_writer::options::WriterOptionsBuilder;
use cmark_writer::writer::CommonMarkWriter;

// Use builder pattern for custom options
let options = WriterOptionsBuilder::new()
    .strict(true)
    .hard_break_spaces(false)
    .indent_spaces(2)
    .build();

let mut writer = CommonMarkWriter::with_options(options);

§Table Support

use cmark_writer::ast::{Node, tables::TableBuilder};

// Create tables with the builder pattern
let table = TableBuilder::new()
    .headers(vec![
        Node::Text("Name".to_string()), 
        Node::Text("Age".to_string())
    ])
    .add_row(vec![
        Node::Text("John".to_string()),
        Node::Text("30".to_string()),
    ])
    .add_row(vec![
        Node::Text("Alice".to_string()),
        Node::Text("25".to_string()),
    ])
    .build();

§GitHub Flavored Markdown (GFM)

Enable GFM features by adding to your Cargo.toml:

[dependencies]
cmark-writer = { version = "0.6.2", features = ["gfm"] }

GFM Support:

  • Tables with column alignment
  • Strikethrough text
  • Task lists
  • Extended autolinks
  • HTML element filtering

§Custom Nodes

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

#[derive(Debug, Clone, PartialEq)]
#[custom_node]
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(())
    }
    
    fn is_block_custom(&self) -> bool {
        false
    }
}

§Development

# Build
cargo build

# Run tests
cargo test

§License

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

§Contributing

Contributions are welcome! Feel free to submit a Pull Request.

Re-exports§

pub use crate::ast::CodeBlockType;
pub use crate::ast::CustomNodeWriter;
pub use crate::ast::HeadingType;
pub use crate::ast::HtmlAttribute;
pub use crate::ast::HtmlElement;
pub use crate::ast::ListItem;
pub use crate::ast::Node;
pub use crate::error::CodedError;
pub use crate::error::StructureError;
pub use crate::error::WriteError;
pub use crate::error::WriteResult;
pub use crate::options::WriterOptions;
pub use crate::writer::CommonMarkWriter;

Modules§

ast
Abstract Syntax Tree for CommonMark document structure.
error
Error handling for CommonMark writer.
options
CommonMark formatting options.
writer
CommonMark writer implementation.

Attribute Macros§

coded_error
Custom coded error attribute macro, replaces the coded form errors in the original define_custom_errors! macro
custom_node
Custom node attribute macro, replaces the original derive_custom_node! macro
structure_error
Custom error attribute macro, replaces the struct form errors in the original define_custom_errors! macro