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
9 changes: 9 additions & 0 deletions crates/mdbook-html/src/html/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub(crate) struct Element {
pub(crate) attrs: Attributes,
/// True if this tag ends with `/>`.
pub(crate) self_closing: bool,
/// True if this was raw HTML written in the markdown.
pub(crate) was_raw: bool,
}

impl Element {
Expand All @@ -87,6 +89,7 @@ impl Element {
name,
attrs: Attributes::new(),
self_closing: false,
was_raw: false,
}
}

Expand Down Expand Up @@ -618,6 +621,7 @@ where
name,
attrs,
self_closing: tag.self_closing,
was_raw: true,
};
fix_html_link(&mut el);
self.push(Node::Element(el));
Expand Down Expand Up @@ -842,6 +846,11 @@ where
for heading in headings {
let node = self.tree.get(heading).unwrap();
let el = node.value().as_element().unwrap();
// Don't modify tags if they were manually written HTML. The
// user probably had some intent, and we don't want to mess it up.
if el.was_raw {
continue;
}
let href = if let Some(id) = el.attr("id") {
format!("#{id}")
} else {
Expand Down
4 changes: 4 additions & 0 deletions tests/testsuite/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ fn definition_lists() {
.check_main_file(
"book/definition_lists.html",
file!["markdown/definition_lists/expected_disabled/definition_lists.html"],
)
.check_main_file(
"book/html_definition_lists.html",
file!["markdown/definition_lists/expected_disabled/html_definition_lists.html"],
);
}

Expand Down
6 changes: 5 additions & 1 deletion tests/testsuite/markdown/basic_markdown/expected/html.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ <h2 id="style"><a class="header" href="#style">Style</a></h2>
</style>
<style media="(width &lt; 500px)">
.bar { background-color: green }
</style>
</style>
<h2 id="manual-headers"><a class="header" href="#manual-headers">Manual headers</a></h2>
<h2 id="my header"><a href="#foo">My Header</a></h2>

<h3>Another header</h3>
6 changes: 6 additions & 0 deletions tests/testsuite/markdown/basic_markdown/src/html.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ const x = 'some *text* inside';
<style media="(width < 500px)">
.bar { background-color: green }
</style>

## Manual headers

<h2 id="my header"><a href="#foo">My Header</a></h2>

<h3>Another header</h3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1 id="html-definition-lists"><a class="header" href="#html-definition-lists">HTML definition lists</a></h1>
<p>Test for definition lists manually written in HTML.</p>
<dl>

<dt>Some tag</dt>


<dd>Some defintion</dd>


<dt class="myclass" id="myid"><a class="option-anchor" href="#foo">Another definition</a></dt>


<dd class="def-class">A definition.</dd>

</dl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1 id="html-definition-lists"><a class="header" href="#html-definition-lists">HTML definition lists</a></h1>
<p>Test for definition lists manually written in HTML.</p>
<dl>

<dt>Some tag</dt>


<dd>Some defintion</dd>


<dt class="myclass" id="myid"><a class="option-anchor" href="#foo">Another definition</a></dt>


<dd class="def-class">A definition.</dd>

</dl>
1 change: 1 addition & 0 deletions tests/testsuite/markdown/definition_lists/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Summary

- [Definition lists](./definition_lists.md)
- [HTML definition lists](./html_definition_lists.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# HTML definition lists

Test for definition lists manually written in HTML.

<dl>
<dt>Some tag</dt>
<dd>Some defintion</dd>
<dt class="myclass" id="myid"><a class="option-anchor" href="#foo">Another definition</a></dt>
<dd class="def-class">A definition.</dd>
</dl>