Expand description
§cmark-writer
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 typesListItem- Represents list items, including task list itemsCommonMarkWriter- Converts nodes to CommonMark textWriterOptions- 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.
Re-exports§
pub use crate::ast::Alignment;pub use crate::ast::CustomNode;pub use crate::ast::CustomNodeWriter;pub use crate::ast::HtmlAttribute;pub use crate::ast::HtmlElement;pub use crate::ast::ListItem;pub use crate::ast::Node;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.
- macros
- Macros module
- options
- CommonMark formatting options.
- writer
- CommonMark writer implementation.
Macros§
- derive_
custom_ node - Automatically implement the CustomNode trait for custom node types