pub use pulldown_cmark::{CodeBlockKind, LinkType, Options, Parser};
pub fn default_options() -> Options {
Options::ENABLE_TABLES
| Options::ENABLE_FOOTNOTES
| Options::ENABLE_STRIKETHROUGH
| Options::ENABLE_TASKLISTS
| Options::ENABLE_GFM
}
pub fn parse(source: &str) -> Parser<'_> {
Parser::new_ext(source, default_options())
}
pub fn parse_with_options(source: &str, options: Options) -> Parser<'_> {
Parser::new_ext(source, options)
}
pub fn code_block_language<'a>(kind: &'a CodeBlockKind<'a>) -> Option<&'a str> {
match kind {
CodeBlockKind::Fenced(info) => {
let info = info.trim();
if info.is_empty() {
None
} else {
Some(info.split_whitespace().next().unwrap_or(info))
}
}
CodeBlockKind::Indented => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_basic() {
let source = "# Hello\n\nWorld";
let events: Vec<_> = parse(source).collect();
assert!(!events.is_empty());
}
#[test]
fn test_code_block_language() {
use pulldown_cmark::CowStr;
let rust = CodeBlockKind::Fenced(CowStr::from("rust"));
assert_eq!(code_block_language(&rust), Some("rust"));
let rust_with_attrs = CodeBlockKind::Fenced(CowStr::from("rust,linenos"));
assert_eq!(code_block_language(&rust_with_attrs), Some("rust,linenos"));
let empty = CodeBlockKind::Fenced(CowStr::from(""));
assert_eq!(code_block_language(&empty), None);
let indented = CodeBlockKind::Indented;
assert_eq!(code_block_language(&indented), None);
}
}