base_markdown/markdown/
mod.rs1#![allow(clippy::pedantic)]
12
13pub use self::configure::{IndentOptions, LineWrapping, Options};
14use self::state::{Info, State};
15use crate::{message::Message, Document, NodeId};
16use alloc::string::String;
17
18extern crate alloc;
19mod configure;
20mod construct_name;
21mod escape;
22mod handle;
23mod state;
24mod util;
25
26pub fn to_markdown<S: AsRef<str>>(document: &Document<S>) -> Result<String, Message> {
31 to_markdown_with_options(document, &Options::default())
32}
33
34pub fn to_markdown_with_options<S: AsRef<str>>(
39 document: &Document<S>,
40 options: &Options,
41) -> Result<String, Message> {
42 to_markdown_node(document, document.root(), options)
43}
44
45pub fn to_markdown_node<S: AsRef<str>>(
50 document: &Document<S>,
51 node: NodeId,
52 options: &Options,
53) -> Result<String, Message> {
54 if document.node(node).is_none() {
55 return Err(state::invalid_node());
56 }
57 let mut state = State::new(options);
58 state.serialization_root = Some(node);
59 let mut result = state.handle(document, node, &Info::new("\n", "\n"))?;
60 if !result.is_empty() && !result.ends_with(['\r', '\n']) {
61 result.push('\n');
62 }
63 Ok(result)
64}