Skip to content

Commit

Permalink
Support strikethrough and tasklists. (rust-lang#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss authored and Dylan-DPC committed Jun 12, 2019
1 parent 059a017 commit fb84c32
Show file tree
Hide file tree
Showing 6 changed files with 1,279 additions and 161 deletions.
5 changes: 1 addition & 4 deletions src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ fn render_item(
.chain_err(|| "Could not convert HTML path to str")?;
let anchor_base = utils::fs::normalize_path(filepath);

let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
let p = Parser::new_ext(&chapter.content, opts);
let p = utils::new_cmark_parser(&chapter.content);

let mut in_header = false;
let max_section_depth = i32::from(search_config.heading_split_level);
Expand Down
12 changes: 8 additions & 4 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,18 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
render_markdown_with_base(text, curly_quotes, "")
}

pub fn render_markdown_with_base(text: &str, curly_quotes: bool, base: &str) -> String {
let mut s = String::with_capacity(text.len() * 3 / 2);

pub fn new_cmark_parser(text: &str) -> Parser<'_> {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
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 events = p
.map(clean_codeblock_headers)
Expand Down
1 change: 1 addition & 0 deletions tests/dummy_book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Nested Chapter](first/nested.md)
- [Includes](first/includes.md)
- [Recursive](first/recursive.md)
- [Markdown](first/markdown.md)
- [Second Chapter](second.md)
- [Nested Chapter](second/nested.md)

Expand Down
29 changes: 29 additions & 0 deletions tests/dummy_book/src/first/markdown.md
Original file line number Diff line number Diff line change
@@ -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
38 changes: 36 additions & 2 deletions tests/rendered_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ const TOC_TOP_LEVEL: &[&str] = &[
const TOC_SECOND_LEVEL: &[&str] = &[
"1.1. Nested Chapter",
"1.2. Includes",
"2.1. Nested Chapter",
"1.3. Recursive",
"1.4. Markdown",
"2.1. Nested Chapter",
];

/// Make sure you can load the dummy book and build it without panicking.
Expand Down Expand Up @@ -431,6 +432,39 @@ fn no_index_for_print_html() {
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")]
mod search {
use crate::dummy_book::DummyBook;
Expand Down Expand Up @@ -477,7 +511,7 @@ mod search {
assert_eq!(docs[&some_section]["body"], "");
assert_eq!(
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[&conclusion]["body"], "I put &lt;HTML&gt; in here!");
Expand Down
Loading

0 comments on commit fb84c32

Please sign in to comment.