#![allow(clippy::result_large_err)]
use std::sync::{Arc, OnceLock};
use tabnas::{AltSpec, Plugin, PluginError, Value, TIN_ZZ};
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme_examples {}
pub mod ast;
pub mod block;
pub mod common;
pub mod commonmark;
pub mod engine_block;
pub mod engine_inline;
mod entities;
pub mod html;
pub mod inline;
pub mod node;
pub mod options;
pub use ast::to_ast;
pub use block::MAX_CONTAINER_NESTING;
pub use html::{render_html, render_html_with, DISALLOWED_TAGS};
pub use inline::MAX_INLINE_NESTING;
pub use node::{
Align, ListData, ListType, MdNode, NodeId, NodeType, SourcePos, TableAlign, Tree, WalkEvent,
Walker,
};
pub use options::{Options, RefDef, RefMap, DEFAULT_OPTIONS};
pub use tabnas::Tabnas;
pub const VERSION: &str = "0.7.6";
pub use tabnas::TabnasError as MarkdownError;
pub const PLUGIN_NAME: &str = "Markdown";
pub fn parse_document(src: &str, options: &Options) -> Value {
to_ast(&commonmark::parse(src, *options), options)
}
pub fn parse_inline(text: &str, options: &Options) -> Vec<Value> {
let doc = parse_document(text, options);
let Some(Value::Array(children)) = engine_block::value_get(&doc, "children") else {
return Vec::new();
};
let Some(first) = children.first() else {
return Vec::new();
};
if !matches!(engine_block::value_get(first, "type"), Some(Value::String(t)) if t == "paragraph")
{
return Vec::new();
}
match engine_block::value_get(first, "children") {
Some(Value::Array(inlines)) => inlines.as_ref().clone(),
_ => Vec::new(),
}
}
pub fn to_html(src: &str, options: &Options) -> String {
render_html_with(&commonmark::parse(src, *options), *options)
}
pub fn parse_tree(src: &str, options: &Options) -> Tree {
commonmark::parse(src, *options)
}
pub fn markdown(tn: &mut Tabnas, options: &Options) -> Result<(), PluginError> {
let opts = *options;
let lb = tn.token(engine_block::LINE_TOKEN);
tn.set_options(|o| {
o.fixed.lex = false;
o.space.lex = false;
o.line.lex = false;
o.text.lex = false;
o.string.lex = false;
o.comment.lex = false;
o.number.lex = false;
o.value.lex = false;
o.lex.empty_result = empty_document();
o.rule.start = "markdown".to_string();
o.rule.exclude = if opts.gfm {
String::new()
} else {
"gfm".to_string()
};
})?;
tn.options.lex.matchers.insert(
engine_block::MD_LINE_MATCHER.to_string(),
tabnas::LexMatcher {
name: engine_block::MD_LINE_MATCHER.to_string(),
order: engine_block::MD_LINE_ORDER,
matcher: None,
imperative: Some(Arc::new(engine_block::make_md_line_matcher(lb))),
factory: None,
},
);
tn.options.parse.named_prepare.insert(
engine_block::MD_PREPARE.to_string(),
tabnas::ParsePrepare::Context(Arc::new(engine_block::make_md_prepare(opts))),
);
let inline_tn = Arc::new(engine_inline::make_inline_tn(&opts));
let run_inlines = move |tree: Tree, refmap: RefMap| {
engine_inline::parse_inlines_engine(&inline_tn, tree, refmap, &opts)
};
tn.define_rule("markdown", |rs| {
rs.clear();
rs.add_open(AltSpec {
p: Some("line".to_string()),
..Default::default()
});
let mut finish = AltSpec {
s: vec![vec![TIN_ZZ]],
..Default::default()
};
finish.add_action(engine_block::make_md_finish_action(opts, run_inlines));
rs.add_close(finish);
rs.add_close(AltSpec::default());
});
tn.define_rule("line", |rs| {
rs.clear();
let mut gfm = AltSpec {
s: vec![vec![lb]],
c_fn: Some(Arc::new(engine_block::md_line_gfm_condition)),
r: Some("line".to_string()),
g: "gfm".to_string(),
..Default::default()
};
gfm.add_action(engine_block::md_line_gfm_action);
rs.add_open(gfm);
let mut base = AltSpec {
s: vec![vec![lb]],
r: Some("line".to_string()),
..Default::default()
};
base.add_action(engine_block::md_line_action);
rs.add_open(base);
rs.add_open(AltSpec::default());
rs.add_close(AltSpec::default());
});
Ok(())
}
fn empty_document() -> Value {
let mut map = indexmap::IndexMap::with_capacity(2);
map.insert("type".to_string(), Value::String("document".to_string()));
map.insert("children".to_string(), Value::array(Vec::new()));
Value::object(map)
}
pub fn plugin() -> Plugin {
Plugin::new(PLUGIN_NAME, |tn, options| {
markdown(tn, &Options::resolve(options))
})
.with_defaults(DEFAULT_OPTIONS.to_value())
}
pub fn make_with(options: &Options) -> Tabnas {
let mut tn = Tabnas::new();
tn.use_plugin(plugin(), Some(options.to_value()))
.expect("the Markdown plugin installs on a bare engine");
tn
}
pub fn make() -> Tabnas {
make_with(&DEFAULT_OPTIONS)
}
pub fn parse(src: &str) -> Result<Value, MarkdownError> {
static DEFAULT: OnceLock<Tabnas> = OnceLock::new();
DEFAULT.get_or_init(make).parse(src)
}
pub fn parse_keep_tree(tn: &Tabnas, src: &str) -> Result<(Value, Option<Tree>), MarkdownError> {
let _ = engine_block::take_kept_tree();
let mut md = indexmap::IndexMap::with_capacity(1);
md.insert("keepTree".to_string(), Value::Bool(true));
let mut meta = indexmap::IndexMap::with_capacity(1);
meta.insert("md".to_string(), Value::object(md));
let ast = tn.parse_with_meta(src, Value::object(meta))?;
Ok((ast, engine_block::take_kept_tree()))
}