-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathlib.rs
39 lines (32 loc) · 1022 Bytes
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! API.
//!
//! JS equivalent: https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/index.js.
#![no_std]
use alloc::string::String;
pub use configure::{IndentOptions, Options};
use markdown::{mdast::Node, message::Message};
use state::{Info, State};
extern crate alloc;
mod association;
mod configure;
mod construct_name;
mod handle;
mod state;
mod r#unsafe;
mod util;
/// Turn an mdast syntax tree into markdown.
pub fn to_markdown(tree: &Node) -> Result<String, Message> {
to_markdown_with_options(tree, &Options::default())
}
/// Turn an mdast syntax tree, with options, into markdown.
pub fn to_markdown_with_options(tree: &Node, options: &Options) -> Result<String, Message> {
let mut state = State::new(options);
let mut result = state.handle(tree, &Info::new("\n", "\n"), None)?;
if !result.is_empty() {
let last_char = result.chars().last().unwrap();
if last_char != '\n' && last_char != '\r' {
result.push('\n');
}
}
Ok(result)
}