Skip to content

Commit 5417569

Browse files
committed
Don't modify headers or dt if the tag is manually written HTML
This changes it so that header and `<dt>` tags manually written as HTML are not modified (no anchor, no id, etc.). This is to avoid mangling any HTML that the user explicitly crafted. I'm not sure what the fallout from the headers might be, since I'm not 100% sure there aren't uses where the user wanted mdbook to modify manual HTML. However, I don't see any in rust-lang's use.
1 parent 07ed00e commit 5417569

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

crates/mdbook-html/src/html/tree.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pub(crate) struct Element {
7777
pub(crate) attrs: Attributes,
7878
/// True if this tag ends with `/>`.
7979
pub(crate) self_closing: bool,
80+
/// True if this was raw HTML written in the markdown.
81+
pub(crate) was_raw: bool,
8082
}
8183

8284
impl Element {
@@ -87,6 +89,7 @@ impl Element {
8789
name,
8890
attrs: Attributes::new(),
8991
self_closing: false,
92+
was_raw: false,
9093
}
9194
}
9295

@@ -618,6 +621,7 @@ where
618621
name,
619622
attrs,
620623
self_closing: tag.self_closing,
624+
was_raw: true,
621625
};
622626
fix_html_link(&mut el);
623627
self.push(Node::Element(el));
@@ -842,6 +846,11 @@ where
842846
for heading in headings {
843847
let node = self.tree.get(heading).unwrap();
844848
let el = node.value().as_element().unwrap();
849+
// Don't modify tags if they were manually written HTML. The
850+
// user probably had some intent, and we don't want to mess it up.
851+
if el.was_raw {
852+
continue;
853+
}
845854
let href = if let Some(id) = el.attr("id") {
846855
format!("#{id}")
847856
} else {

tests/testsuite/markdown/basic_markdown/expected/html.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ <h2 id="style"><a class="header" href="#style">Style</a></h2>
6464
.bar { background-color: green }
6565
</style>
6666
<h2 id="manual-headers"><a class="header" href="#manual-headers">Manual headers</a></h2>
67-
<h2 id="my header"><a class="header" href="#my header"><a href="#foo">My Header</a></a></h2>
67+
<h2 id="my header"><a href="#foo">My Header</a></h2>
6868

69-
<h3 id="another-header"><a class="header" href="#another-header">Another header</a></h3>
69+
<h3>Another header</h3>

tests/testsuite/markdown/definition_lists/expected/html_definition_lists.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ <h1 id="html-definition-lists"><a class="header" href="#html-definition-lists">H
22
<p>Test for definition lists manually written in HTML.</p>
33
<dl>
44

5-
<dt id="some-tag"><a class="header" href="#some-tag">Some tag</a></dt>
5+
<dt>Some tag</dt>
66

77

88
<dd>Some defintion</dd>
99

1010

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

1313

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

tests/testsuite/markdown/definition_lists/expected_disabled/html_definition_lists.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ <h1 id="html-definition-lists"><a class="header" href="#html-definition-lists">H
22
<p>Test for definition lists manually written in HTML.</p>
33
<dl>
44

5-
<dt id="some-tag"><a class="header" href="#some-tag">Some tag</a></dt>
5+
<dt>Some tag</dt>
66

77

88
<dd>Some defintion</dd>
99

1010

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

1313

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

0 commit comments

Comments
 (0)