Support strikethrough and tasklists. (#952)

This commit is contained in:
Eric Huss 2019-06-12 08:02:03 -07:00 committed by Dylan DPC
parent 0c2292b9aa
commit 2497e77bf1
6 changed files with 1252 additions and 134 deletions

View File

@ -81,10 +81,7 @@ fn render_item(
.chain_err(|| "Could not convert HTML path to str")?; .chain_err(|| "Could not convert HTML path to str")?;
let anchor_base = utils::fs::normalize_path(filepath); let anchor_base = utils::fs::normalize_path(filepath);
let mut opts = Options::empty(); let p = utils::new_cmark_parser(&chapter.content);
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
let p = Parser::new_ext(&chapter.content, opts);
let mut in_header = false; let mut in_header = false;
let max_section_depth = i32::from(search_config.heading_split_level); let max_section_depth = i32::from(search_config.heading_split_level);

View File

@ -111,14 +111,18 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
render_markdown_with_base(text, curly_quotes, "") render_markdown_with_base(text, curly_quotes, "")
} }
pub fn render_markdown_with_base(text: &str, curly_quotes: bool, base: &str) -> String { pub fn new_cmark_parser(text: &str) -> Parser<'_> {
let mut s = String::with_capacity(text.len() * 3 / 2);
let mut opts = Options::empty(); let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES); opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES); opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
Parser::new_ext(text, opts)
}
let p = Parser::new_ext(text, opts); pub fn render_markdown_with_base(text: &str, curly_quotes: bool, base: &str) -> String {
let mut s = String::with_capacity(text.len() * 3 / 2);
let p = new_cmark_parser(text);
let mut converter = EventQuoteConverter::new(curly_quotes); let mut converter = EventQuoteConverter::new(curly_quotes);
let events = p let events = p
.map(clean_codeblock_headers) .map(clean_codeblock_headers)

View File

@ -7,6 +7,7 @@
- [Nested Chapter](first/nested.md) - [Nested Chapter](first/nested.md)
- [Includes](first/includes.md) - [Includes](first/includes.md)
- [Recursive](first/recursive.md) - [Recursive](first/recursive.md)
- [Markdown](first/markdown.md)
- [Second Chapter](second.md) - [Second Chapter](second.md)
- [Nested Chapter](second/nested.md) - [Nested Chapter](second/nested.md)

View File

@ -0,0 +1,29 @@
# Markdown tests
Tests for some markdown output.
## Tables
| foo | bar |
| --- | --- |
| baz | bim |
## Footnotes
Footnote example[^1], or with a word[^word].
[^1]: This is a footnote.
[^word]: A longer footnote.
With multiple lines.
Third line.
## Strikethrough
~~strikethrough example~~
## Tasklisks
- [X] Apples
- [X] Broccoli
- [ ] Carrots

View File

@ -29,8 +29,9 @@ const TOC_TOP_LEVEL: &[&str] = &[
const TOC_SECOND_LEVEL: &[&str] = &[ const TOC_SECOND_LEVEL: &[&str] = &[
"1.1. Nested Chapter", "1.1. Nested Chapter",
"1.2. Includes", "1.2. Includes",
"2.1. Nested Chapter",
"1.3. Recursive", "1.3. Recursive",
"1.4. Markdown",
"2.1. Nested Chapter",
]; ];
/// Make sure you can load the dummy book and build it without panicking. /// Make sure you can load the dummy book and build it without panicking.
@ -431,6 +432,39 @@ fn no_index_for_print_html() {
assert_doesnt_contain_strings(index_html, &[r##"noindex"##]); assert_doesnt_contain_strings(index_html, &[r##"noindex"##]);
} }
#[test]
fn markdown_options() {
let temp = DummyBook::new().build().unwrap();
let md = MDBook::load(temp.path()).unwrap();
md.build().unwrap();
let path = temp.path().join("book/first/markdown.html");
assert_contains_strings(
&path,
&[
"<th>foo</th>",
"<th>bar</th>",
"<td>baz</td>",
"<td>bim</td>",
],
);
assert_contains_strings(&path, &[
r##"<sup class="footnote-reference"><a href="#1">1</a></sup>"##,
r##"<sup class="footnote-reference"><a href="#word">2</a></sup>"##,
r##"<div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>"##,
r##"<div class="footnote-definition" id="word"><sup class="footnote-definition-label">2</sup>"##,
]);
assert_contains_strings(&path, &["<del>strikethrough example</del>"]);
assert_contains_strings(
&path,
&[
"<li><input disabled=\"\" type=\"checkbox\" checked=\"\"/>\nApples",
"<li><input disabled=\"\" type=\"checkbox\" checked=\"\"/>\nBroccoli",
"<li><input disabled=\"\" type=\"checkbox\"/>\nCarrots",
],
);
}
#[cfg(feature = "search")] #[cfg(feature = "search")]
mod search { mod search {
use crate::dummy_book::DummyBook; use crate::dummy_book::DummyBook;
@ -477,7 +511,7 @@ mod search {
assert_eq!(docs[&some_section]["body"], ""); assert_eq!(docs[&some_section]["body"], "");
assert_eq!( assert_eq!(
docs[&summary]["body"], docs[&summary]["body"],
"Dummy Book Introduction First Chapter Nested Chapter Includes Recursive Second Chapter Nested Chapter Conclusion" "Dummy Book Introduction First Chapter Nested Chapter Includes Recursive Markdown Second Chapter Nested Chapter Conclusion"
); );
assert_eq!(docs[&summary]["breadcrumbs"], "First Chapter » Summary"); assert_eq!(docs[&summary]["breadcrumbs"], "First Chapter » Summary");
assert_eq!(docs[&conclusion]["body"], "I put &lt;HTML&gt; in here!"); assert_eq!(docs[&conclusion]["body"], "I put &lt;HTML&gt; in here!");

File diff suppressed because it is too large Load Diff