Merge pull request #2913 from ehuss/html-definition-lists

Don't modify headers or dt if the tag is manually written HTML
This commit is contained in:
Eric Huss
2025-10-30 19:10:36 +00:00
committed by GitHub
8 changed files with 67 additions and 1 deletions

View File

@@ -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 {
@@ -87,6 +89,7 @@ impl Element {
name,
attrs: Attributes::new(),
self_closing: false,
was_raw: false,
}
}
@@ -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));
@@ -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 {

View File

@@ -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"],
);
}

View File

@@ -62,4 +62,8 @@ const x = 'some *text* inside';
</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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -1,3 +1,4 @@
# Summary
- [Definition lists](./definition_lists.md)
- [HTML definition lists](./html_definition_lists.md)

View File

@@ -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>