| 1 |
//! Markdown processing used in mdBook. |
| 2 |
//! |
| 3 |
//! This crate provides functions for processing Markdown in the same way as |
| 4 |
//! [mdBook](https://rust-lang.github.io/mdBook/). The [`pulldown_cmark`] |
| 5 |
//! crate is used as the underlying parser. This crate re-exports |
| 6 |
//! [`pulldown_cmark`] so that you can access its types. |
| 7 |
|
| 8 |
use pulldown_cmark::{Options, Parser}; |
| 9 |
|
| 10 |
#[doc(inline)] |
| 11 |
pub use pulldown_cmark; |
| 12 |
|
| 13 |
/// Options for parsing markdown. |
| 14 |
#[non_exhaustive] |
| 15 |
pub struct MarkdownOptions { |
| 16 |
/// Enables smart punctuation. |
| 17 |
/// |
| 18 |
/// Converts quotes to curly quotes, `...` to `…`, `--` to en-dash, and |
| 19 |
/// `---` to em-dash. |
| 20 |
/// |
| 21 |
/// This is `true` by default. |
| 22 |
pub smart_punctuation: bool, |
| 23 |
/// Enables definition lists. |
| 24 |
/// |
| 25 |
/// This is `true` by default. |
| 26 |
pub definition_lists: bool, |
| 27 |
/// Enables admonitions. |
| 28 |
/// |
| 29 |
/// This is `true` by default. |
| 30 |
pub admonitions: bool, |
| 31 |
} |
| 32 |
|
| 33 |
impl Default for MarkdownOptions { |
| 34 |
fn default() -> MarkdownOptions { |
| 35 |
MarkdownOptions { |
| 36 |
smart_punctuation: true, |
| 37 |
definition_lists: true, |
| 38 |
admonitions: true, |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/// Creates a new pulldown-cmark parser of the given text. |
| 44 |
pub fn new_cmark_parser<'text>(text: &'text str, options: &MarkdownOptions) -> Parser<'text> { |
| 45 |
let mut opts = Options::empty(); |
| 46 |
opts.insert(Options::ENABLE_TABLES); |
| 47 |
opts.insert(Options::ENABLE_FOOTNOTES); |
| 48 |
opts.insert(Options::ENABLE_STRIKETHROUGH); |
| 49 |
opts.insert(Options::ENABLE_TASKLISTS); |
| 50 |
opts.insert(Options::ENABLE_HEADING_ATTRIBUTES); |
| 51 |
if options.smart_punctuation { |
| 52 |
opts.insert(Options::ENABLE_SMART_PUNCTUATION); |
| 53 |
} |
| 54 |
if options.definition_lists { |
| 55 |
opts.insert(Options::ENABLE_DEFINITION_LIST); |
| 56 |
} |
| 57 |
if options.admonitions { |
| 58 |
opts.insert(Options::ENABLE_GFM); |
| 59 |
} |
| 60 |
Parser::new_ext(text, opts) |
| 61 |
} |