pub struct MarkdownIt {
pub block: BlockParser,
pub inline: InlineParser,
pub link_formatter: Box<dyn LinkFormatter>,
pub ext: MarkdownItExtSet,
pub max_indent: i32,
/* private fields */
}Expand description
Main parser struct, created once and reused for parsing multiple documents.
Fields§
§block: BlockParserBlock-level tokenizer.
inline: InlineParserInline-level tokenizer.
link_formatter: Box<dyn LinkFormatter>Link validator and formatter.
ext: MarkdownItExtSetStorage for custom data used in plugins.
max_indent: i32Maximum allowed indentation for syntax blocks default i32::MAX, indented code blocks will set this to 4
Implementations§
Source§impl MarkdownIt
impl MarkdownIt
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/ferris/main.rs (line 8)
6fn main() {
7 // create markdown parser
8 let md = &mut markdown_it::MarkdownIt::new();
9
10 // add commonmark syntax, you almost always want to do that
11 markdown_it::plugins::cmark::add(md);
12
13 // add custom three rules described above
14 inline_rule::add(md);
15 block_rule::add(md);
16 core_rule::add(md);
17
18 // and now you can use it
19 let html = md.parse(r#"
20(\/) hello world (\/)
21(\/)-------------(\/)
22 "#).render();
23
24 print!("{html}");
25
26 assert_eq!(html.trim(), r#"
27<p><span class="ferris-inline">🦀</span> hello world <span class="ferris-inline">🦀</span></p>
28<div class="ferris-block"><img src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Original_Ferris.svg"></div>
29<footer class="ferris-counter">There are 3 crabs lurking in this document.</footer>
30 "#.trim());
31}More examples
examples/syntect/main.rs (line 9)
1fn main() {
2 #[cfg(not(feature = "syntect"))]
3 {
4 eprintln!("Run this example with the `syntect` feature enabled.");
5 eprintln!("such as: cargo run --example syntect --features syntect");
6 return;
7 }
8
9 let mut md_inline = markdown_it::MarkdownIt::new();
10 markdown_it::plugins::cmark::add(&mut md_inline);
11 markdown_it::plugins::extra::syntect::add(&mut md_inline);
12
13 let mut md_classed = markdown_it::MarkdownIt::new();
14 markdown_it::plugins::cmark::add(&mut md_classed);
15 markdown_it::plugins::extra::syntect::add(&mut md_classed);
16 markdown_it::plugins::extra::syntect::set_to_classed(&mut md_classed);
17
18 let input_inline = r#"
19parse with inline mode:
20
21```rust {2}
22fn main() {
23 println!("Ciallo world!");
24}
25```
26"#;
27 let input_classed = r#"
28parse with classed mode:
29
30```rust {2}
31fn main() {
32 println!("Ciallo world!");
33}
34```
35"#;
36
37 let highlighted_line_css = r#"
38.syntect-line-highlighted {
39 background-color: #fffbdd;
40 border-left: 4px solid #f9c513;
41 padding-left: 12px;
42}
43"#;
44
45 let html = format!(
46 r#"<!DOCTYPE html>
47<html>
48<head>
49 <meta charset="utf-8">
50 <title>Code highlight example</title>
51 <style>
52 body {{
53 font-family: sans-serif;
54 max-width: 800px;
55 margin: 40px auto;
56 padding: 20px;
57 }}
58 h2 {{
59 margin-top: 40px;
60 }}
61 pre {{
62 background-color: #f6f8fa;
63 padding: 16px 0;
64 border-radius: 6px;
65 font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
66 font-size: 14px;
67 line-height: 1.5;
68 overflow: auto;
69 }}
70 .syntect-line {{
71 display: block;
72 padding: 0 16px;
73 }}
74 {}
75 {}
76 </style>
77</head>
78<body>
79 <h2>Inline Mode</h2>
80 {}
81 <h2>Classed Mode</h2>
82 {}
83</body>
84</html>
85"#,
86 markdown_it::plugins::extra::syntect::theme_css(&md_classed).unwrap_or_default(),
87 highlighted_line_css,
88 md_inline.parse(input_inline).render(),
89 md_classed.parse(input_classed).render(),
90 );
91
92 let path = "examples/syntect/demo.html";
93 std::fs::write(path, html).expect("write file failed");
94 println!("success write to '{}'", path);
95}Sourcepub fn parse(&self, src: &str) -> Node
pub fn parse(&self, src: &str) -> Node
Examples found in repository?
examples/ferris/main.rs (lines 19-22)
6fn main() {
7 // create markdown parser
8 let md = &mut markdown_it::MarkdownIt::new();
9
10 // add commonmark syntax, you almost always want to do that
11 markdown_it::plugins::cmark::add(md);
12
13 // add custom three rules described above
14 inline_rule::add(md);
15 block_rule::add(md);
16 core_rule::add(md);
17
18 // and now you can use it
19 let html = md.parse(r#"
20(\/) hello world (\/)
21(\/)-------------(\/)
22 "#).render();
23
24 print!("{html}");
25
26 assert_eq!(html.trim(), r#"
27<p><span class="ferris-inline">🦀</span> hello world <span class="ferris-inline">🦀</span></p>
28<div class="ferris-block"><img src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Original_Ferris.svg"></div>
29<footer class="ferris-counter">There are 3 crabs lurking in this document.</footer>
30 "#.trim());
31}More examples
examples/syntect/main.rs (line 88)
1fn main() {
2 #[cfg(not(feature = "syntect"))]
3 {
4 eprintln!("Run this example with the `syntect` feature enabled.");
5 eprintln!("such as: cargo run --example syntect --features syntect");
6 return;
7 }
8
9 let mut md_inline = markdown_it::MarkdownIt::new();
10 markdown_it::plugins::cmark::add(&mut md_inline);
11 markdown_it::plugins::extra::syntect::add(&mut md_inline);
12
13 let mut md_classed = markdown_it::MarkdownIt::new();
14 markdown_it::plugins::cmark::add(&mut md_classed);
15 markdown_it::plugins::extra::syntect::add(&mut md_classed);
16 markdown_it::plugins::extra::syntect::set_to_classed(&mut md_classed);
17
18 let input_inline = r#"
19parse with inline mode:
20
21```rust {2}
22fn main() {
23 println!("Ciallo world!");
24}
25```
26"#;
27 let input_classed = r#"
28parse with classed mode:
29
30```rust {2}
31fn main() {
32 println!("Ciallo world!");
33}
34```
35"#;
36
37 let highlighted_line_css = r#"
38.syntect-line-highlighted {
39 background-color: #fffbdd;
40 border-left: 4px solid #f9c513;
41 padding-left: 12px;
42}
43"#;
44
45 let html = format!(
46 r#"<!DOCTYPE html>
47<html>
48<head>
49 <meta charset="utf-8">
50 <title>Code highlight example</title>
51 <style>
52 body {{
53 font-family: sans-serif;
54 max-width: 800px;
55 margin: 40px auto;
56 padding: 20px;
57 }}
58 h2 {{
59 margin-top: 40px;
60 }}
61 pre {{
62 background-color: #f6f8fa;
63 padding: 16px 0;
64 border-radius: 6px;
65 font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
66 font-size: 14px;
67 line-height: 1.5;
68 overflow: auto;
69 }}
70 .syntect-line {{
71 display: block;
72 padding: 0 16px;
73 }}
74 {}
75 {}
76 </style>
77</head>
78<body>
79 <h2>Inline Mode</h2>
80 {}
81 <h2>Classed Mode</h2>
82 {}
83</body>
84</html>
85"#,
86 markdown_it::plugins::extra::syntect::theme_css(&md_classed).unwrap_or_default(),
87 highlighted_line_css,
88 md_inline.parse(input_inline).render(),
89 md_classed.parse(input_classed).render(),
90 );
91
92 let path = "examples/syntect/demo.html";
93 std::fs::write(path, html).expect("write file failed");
94 println!("success write to '{}'", path);
95}Sourcepub fn add_rule<T: CoreRule>(
&mut self,
) -> RuleBuilder<'_, fn(&mut Node, &MarkdownIt)>
pub fn add_rule<T: CoreRule>( &mut self, ) -> RuleBuilder<'_, fn(&mut Node, &MarkdownIt)>
pub fn has_rule<T: CoreRule>(&mut self) -> bool
pub fn remove_rule<T: CoreRule>(&mut self)
Trait Implementations§
Source§impl Debug for MarkdownIt
impl Debug for MarkdownIt
Auto Trait Implementations§
impl !Freeze for MarkdownIt
impl !RefUnwindSafe for MarkdownIt
impl Send for MarkdownIt
impl Sync for MarkdownIt
impl Unpin for MarkdownIt
impl UnsafeUnpin for MarkdownIt
impl !UnwindSafe for MarkdownIt
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.