markdown_ppp/printer/mod.rs
1//! Markdown pretty-printer for formatting AST back to Markdown
2//!
3//! This module provides functionality to render a Markdown Abstract Syntax Tree (AST)
4//! back to formatted Markdown text. The printer supports configurable formatting
5//! options and produces clean, readable Markdown output.
6//!
7//! # Features
8//!
9//! - **Full AST support**: All CommonMark + GFM elements are supported
10//! - **Configurable formatting**: Control line width, indentation, and spacing
11//! - **Pretty-printing**: Intelligent line wrapping and formatting
12//! - **Round-trip capability**: Parse → Render → Parse produces equivalent AST
13//! - **GitHub extensions**: Tables, task lists, alerts, footnotes, strikethrough
14//!
15//! # Basic Usage
16//!
17//! ```rust
18//! use markdown_ppp::ast::*;
19//! use markdown_ppp::printer::{render_markdown, config::Config};
20//!
21//! let doc = Document {
22//! blocks: vec![
23//! Block::Heading(Heading {
24//! kind: HeadingKind::Atx(1),
25//! content: vec![Inline::Text("Hello World".to_string())],
26//! }),
27//! Block::Paragraph(vec![
28//! Inline::Text("This is ".to_string()),
29//! Inline::Strong(vec![Inline::Text("formatted".to_string())]),
30//! Inline::Text(" text.".to_string()),
31//! ]),
32//! ],
33//! };
34//!
35//! let config = Config::default();
36//! let markdown = render_markdown(&doc, config);
37//! println!("{}", markdown);
38//! ```
39//!
40//! # Configuration
41//!
42//! Customize the output format using configuration:
43//!
44//! ```rust
45//! use markdown_ppp::printer::{render_markdown, config::Config};
46//! use markdown_ppp::ast::{Document, Block, Inline};
47//!
48//! let config = Config::default().with_width(120);
49//! let doc = Document { blocks: vec![Block::Paragraph(vec![Inline::Text("Hello".to_string())])] };
50//! let markdown = render_markdown(&doc, config);
51//! ```
52
53mod block;
54mod blockquote;
55
56/// Configuration options for Markdown pretty-printing.
57pub mod config;
58mod github_alert;
59mod heading;
60mod inline;
61mod list;
62mod markdown_syntax_detector;
63mod table;
64mod tests;
65
66use crate::ast::*;
67use pretty::{Arena, DocBuilder};
68use std::rc::Rc;
69
70/// Render a Markdown AST back to formatted Markdown text
71///
72/// This function takes a parsed Markdown document (AST) and renders it back
73/// to clean, well-formatted Markdown text. The output follows consistent
74/// formatting rules and can be customized via configuration options.
75///
76/// # Arguments
77///
78/// * `ast` - The Markdown document AST to render
79/// * `config` - Configuration options controlling the output format
80///
81/// # Returns
82///
83/// A `String` containing the formatted Markdown text
84///
85/// # Examples
86///
87/// Basic rendering:
88/// ```rust
89/// use markdown_ppp::ast::*;
90/// use markdown_ppp::printer::{render_markdown, config::Config};
91///
92/// let doc = Document {
93/// blocks: vec![
94/// Block::Paragraph(vec![
95/// Inline::Text("Hello ".to_string()),
96/// Inline::Strong(vec![Inline::Text("world".to_string())]),
97/// ]),
98/// ],
99/// };
100///
101/// let config = Config::default();
102/// let markdown = render_markdown(&doc, config);
103/// assert!(markdown.contains("**world**"));
104/// ```
105///
106/// With custom width:
107/// ```rust
108/// use markdown_ppp::ast::Document;
109/// use markdown_ppp::printer::{render_markdown, config::Config};
110///
111/// let doc = Document { blocks: vec![] };
112/// let config = Config::default().with_width(60);
113/// let markdown = render_markdown(&doc, config);
114/// ```
115///
116/// # Round-trip Guarantee
117///
118/// For most valid Markdown documents, the following property holds:
119/// ```text
120/// parse(render(parse(input))) ≈ parse(input)
121/// ```
122/// Where ≈ means semantically equivalent AST structures.
123pub fn render_markdown(ast: &Document, config: crate::printer::config::Config) -> String {
124 let config = Rc::new(config);
125 let arena = Arena::new();
126 let doc = ast.to_doc(config.clone(), &arena);
127
128 let mut buf = Vec::new();
129 doc.render(config.width, &mut buf).unwrap();
130 String::from_utf8(buf).unwrap()
131}
132
133trait ToDoc<'a> {
134 fn to_doc(
135 &self,
136 config: Rc<crate::printer::config::Config>,
137 arena: &'a Arena<'a>,
138 ) -> DocBuilder<'a, Arena<'a>, ()>;
139}
140
141impl<'a> ToDoc<'a> for Document {
142 fn to_doc(
143 &self,
144 config: Rc<crate::printer::config::Config>,
145 arena: &'a Arena<'a>,
146 ) -> DocBuilder<'a, Arena<'a>, ()> {
147 self.blocks.to_doc(config, arena)
148 }
149}