vertigo_cmark/lib.rs
1pub use pulldown_cmark::{self, Options};
2use pulldown_cmark::{Event, Parser};
3use vertigo::DomNode;
4
5mod generate;
6mod policy;
7mod styling;
8pub use policy::{ElementAction, HtmlPolicy, SafeHtml, TrustedHtml, is_safe_url};
9pub use styling::CMarkStyle;
10
11#[cfg(feature = "syntect")]
12mod highlighting;
13
14#[cfg(test)]
15mod tests;
16
17/// Converts a CommonMark string to Vertigo tree with default options and styling.
18///
19/// NOTE: Tables are enabled by default.
20///
21/// Raw HTML and link destinations are filtered by [`SafeHtml`], so the text may come
22/// from anyone — see [`HtmlPolicy`] for other policies.
23pub fn to_vertigo(text: &str) -> DomNode {
24 to_vertigo_opts(text, Options::ENABLE_TABLES)
25}
26
27/// Converts a CommonMark string to Vertigo tree with provided [Options] and default styling.
28///
29/// Raw HTML and link destinations are filtered by [`SafeHtml`].
30pub fn to_vertigo_opts(text: &str, opts: Options) -> DomNode {
31 let parser = Parser::new_ext(text, opts);
32 generate::generate_tree(parser, CMarkStyle::default(), &SafeHtml)
33}
34
35/// Converts a CommonMark string to Vertigo tree with default options and provided [styling](CMarkStyle).
36///
37/// Raw HTML and link destinations are filtered by [`SafeHtml`].
38pub fn to_vertigo_styled(text: &str, style: CMarkStyle) -> DomNode {
39 let parser = Parser::new_ext(text, Options::ENABLE_TABLES);
40 generate::generate_tree(parser, style, &SafeHtml)
41}
42
43/// Converts a CommonMark string to Vertigo tree with provided [Options] and provided [styling](CMarkStyle).
44///
45/// NOTE: If you want highlighted code block, just enable `syntect` feature.
46///
47/// Raw HTML and link destinations are filtered by [`SafeHtml`].
48pub fn to_vertigo_opts_styled(text: &str, opts: Options, style: CMarkStyle) -> DomNode {
49 let parser = Parser::new_ext(text, opts);
50 generate::generate_tree(parser, style, &SafeHtml)
51}
52
53/// Converts a stream of pulldown-cmark [events](Event) to Vertigo tree with provided
54/// [styling](CMarkStyle) and [policy](HtmlPolicy) for raw HTML and link destinations.
55///
56/// Lets the caller adjust the events before rendering — e.g. replace a paragraph with an
57/// embed — and choose the policy: [`SafeHtml`] for text from anyone, [`TrustedHtml`] only for
58/// content you control, or your own.
59///
60/// ```
61/// use vertigo_cmark::{CMarkStyle, SafeHtml, events_to_vertigo, pulldown_cmark::{Event, Parser}};
62///
63/// // soft line breaks as hard ones
64/// let events = Parser::new("First line\nsecond line").map(|event| match event {
65/// Event::SoftBreak => Event::HardBreak,
66/// event => event,
67/// });
68/// let _node = events_to_vertigo(events, CMarkStyle::default(), &SafeHtml);
69/// ```
70pub fn events_to_vertigo<'a, I>(events: I, style: CMarkStyle, policy: &'a dyn HtmlPolicy) -> DomNode
71where
72 I: IntoIterator<Item = Event<'a>>,
73 I::IntoIter: 'a,
74{
75 generate::generate_tree(events.into_iter(), style, policy)
76}