Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion crates/mdbook-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ pub enum RustEdition {
}

/// Configuration for the HTML renderer.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
#[non_exhaustive]
pub struct HtmlConfig {
Expand Down Expand Up @@ -485,6 +485,35 @@ pub struct HtmlConfig {
pub hash_files: bool,
}

impl Default for HtmlConfig {
fn default() -> HtmlConfig {
HtmlConfig {
theme: None,
default_theme: None,
preferred_dark_theme: None,
smart_punctuation: true,
mathjax_support: false,
additional_css: Vec::new(),
additional_js: Vec::new(),
fold: Fold::default(),
playground: Playground::default(),
code: Code::default(),
print: Print::default(),
no_section_label: false,
search: None,
git_repository_url: None,
git_repository_icon: None,
input_404: None,
site_url: None,
cname: None,
edit_url_template: None,
live_reload_endpoint: None,
redirect: HashMap::new(),
hash_files: false,
}
}
}

impl HtmlConfig {
/// Returns the directory of theme from the provided root directory. If the
/// directory is not present it will append the default directory of "theme"
Expand Down
11 changes: 10 additions & 1 deletion crates/mdbook-markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ pub use pulldown_cmark;
mod tests;

/// Options for parsing markdown.
#[derive(Default)]
#[non_exhaustive]
pub struct MarkdownOptions {
/// Enables smart punctuation.
///
/// Converts quotes to curly quotes, `...` to `…`, `--` to en-dash, and
/// `---` to em-dash.
///
/// This is `true` by default.
pub smart_punctuation: bool,
}

impl Default for MarkdownOptions {
fn default() -> MarkdownOptions {
MarkdownOptions {
smart_punctuation: true,
}
}
}

/// Options for converting markdown to HTML.
#[non_exhaustive]
pub struct HtmlRenderOptions<'a> {
Expand Down
26 changes: 8 additions & 18 deletions crates/mdbook-markdown/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ fn it_can_wrap_tables() {

#[test]
fn it_can_keep_quotes_straight() {
let options = HtmlRenderOptions::new(&Path::new(""));
let mut options = HtmlRenderOptions::new(&Path::new(""));
options.markdown_options.smart_punctuation = false;
assert_eq!(render_markdown("'one'", &options), "<p>'one'</p>\n");
}

Expand All @@ -78,8 +79,7 @@ fn it_can_make_quotes_curly_except_when_they_are_in_code() {
</code></pre>
<p><code>'three'</code> ‘four’</p>
"#;
let mut options = HtmlRenderOptions::new(&Path::new(""));
options.markdown_options.smart_punctuation = true;
let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
}

Expand All @@ -102,9 +102,7 @@ more text with spaces
</code></pre>
<p>more text with spaces</p>
"#;
let mut options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
}

Expand All @@ -117,9 +115,7 @@ fn rust_code_block_properties_are_passed_as_space_delimited_class() {

let expected = r#"<pre><code class="language-rust,no_run,should_panic,property_3"></code></pre>
"#;
let mut options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
}

Expand All @@ -132,9 +128,7 @@ fn rust_code_block_properties_with_whitespace_are_passed_as_space_delimited_clas

let expected = r#"<pre><code class="language-rust,,,,,no_run,,,should_panic,,,,property_3"></code></pre>
"#;
let mut options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
}

Expand All @@ -147,17 +141,13 @@ fn rust_code_block_without_properties_has_proper_html_class() {

let expected = r#"<pre><code class="language-rust"></code></pre>
"#;
let mut options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);

let input = r#"
```rust
```
"#;
let mut options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
}
2 changes: 1 addition & 1 deletion guide/src/format/configuration/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The following configuration options are available:
CSS media query. Defaults to `navy`.
- **smart-punctuation:** Converts quotes to curly quotes, `...` to `…`, `--` to en-dash, and `---` to em-dash.
See [Smart Punctuation](../markdown.md#smart-punctuation).
Defaults to `false`.
Defaults to `true`.
- **mathjax-support:** Adds support for [MathJax](../mathjax.md). Defaults to
`false`.
- **additional-css:** If you need to slightly change the appearance of your book
Expand Down
4 changes: 2 additions & 2 deletions guide/src/format/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ characters:

So, no need to manually enter those Unicode characters!

This feature is disabled by default.
To enable it, see the [`output.html.smart-punctuation`] config option.
This feature is enabled by default.
To disable it, see the [`output.html.smart-punctuation`] config option.

[strikethrough]: https://github.github.com/gfm/#strikethrough-extension-
[tables]: https://github.github.com/gfm/#tables-extension-
Expand Down
24 changes: 12 additions & 12 deletions tests/testsuite/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,33 @@ Carrots</li>
#[test]
fn smart_punctuation() {
BookTest::from_dir("markdown/smart_punctuation")
// Default is off.
// Default is on.
.check_main_file(
"book/smart_punctuation.html",
str![[r##"
<h1 id="smart-punctuation"><a class="header" href="#smart-punctuation">Smart Punctuation</a></h1>
<ul>
<li>En dash: --</li>
<li>Em dash: ---</li>
<li>Ellipsis: ...</li>
<li>Double quote: "quote"</li>
<li>Single quote: 'quote'</li>
<li>En dash: </li>
<li>Em dash: </li>
<li>Ellipsis: </li>
<li>Double quote: quote</li>
<li>Single quote: quote</li>
</ul>
"##]],
)
.run("build", |cmd| {
cmd.env("MDBOOK_OUTPUT__HTML__SMART_PUNCTUATION", "true");
cmd.env("MDBOOK_OUTPUT__HTML__SMART_PUNCTUATION", "false");
})
.check_main_file(
"book/smart_punctuation.html",
str![[r##"
<h1 id="smart-punctuation"><a class="header" href="#smart-punctuation">Smart Punctuation</a></h1>
<ul>
<li>En dash: </li>
<li>Em dash: </li>
<li>Ellipsis: </li>
<li>Double quote: quote</li>
<li>Single quote: quote</li>
<li>En dash: --</li>
<li>Em dash: ---</li>
<li>Ellipsis: ...</li>
<li>Double quote: "quote"</li>
<li>Single quote: 'quote'</li>
</ul>
"##]],
);
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn reasonable_search_index() {
// html.
assert_eq!(
docs[&sneaky]["body"],
"I put &lt;HTML&gt; in here! Sneaky inline event alert(\"inline\");. But regular inline is indexed."
"I put &lt;HTML&gt; in here! Sneaky inline event alert(inline);. But regular inline is indexed."
);
assert_eq!(
docs[&no_headers]["breadcrumbs"],
Expand Down

Large diffs are not rendered by default.

Loading