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.
This commit is contained in:
parent
07ed00e8f7
commit
54175698d5
4 changed files with 15 additions and 6 deletions
|
|
@ -77,6 +77,8 @@ pub(crate) struct Element {
|
||||||
pub(crate) attrs: Attributes,
|
pub(crate) attrs: Attributes,
|
||||||
/// True if this tag ends with `/>`.
|
/// True if this tag ends with `/>`.
|
||||||
pub(crate) self_closing: bool,
|
pub(crate) self_closing: bool,
|
||||||
|
/// True if this was raw HTML written in the markdown.
|
||||||
|
pub(crate) was_raw: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Element {
|
impl Element {
|
||||||
|
|
@ -87,6 +89,7 @@ impl Element {
|
||||||
name,
|
name,
|
||||||
attrs: Attributes::new(),
|
attrs: Attributes::new(),
|
||||||
self_closing: false,
|
self_closing: false,
|
||||||
|
was_raw: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -618,6 +621,7 @@ where
|
||||||
name,
|
name,
|
||||||
attrs,
|
attrs,
|
||||||
self_closing: tag.self_closing,
|
self_closing: tag.self_closing,
|
||||||
|
was_raw: true,
|
||||||
};
|
};
|
||||||
fix_html_link(&mut el);
|
fix_html_link(&mut el);
|
||||||
self.push(Node::Element(el));
|
self.push(Node::Element(el));
|
||||||
|
|
@ -842,6 +846,11 @@ where
|
||||||
for heading in headings {
|
for heading in headings {
|
||||||
let node = self.tree.get(heading).unwrap();
|
let node = self.tree.get(heading).unwrap();
|
||||||
let el = node.value().as_element().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") {
|
let href = if let Some(id) = el.attr("id") {
|
||||||
format!("#{id}")
|
format!("#{id}")
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,6 @@ const x = 'some *text* inside';
|
||||||
.bar { background-color: green }
|
.bar { background-color: green }
|
||||||
</style>
|
</style>
|
||||||
<h2 id="manual-headers"><a class="header" href="#manual-headers">Manual headers</a></h2>
|
<h2 id="manual-headers"><a class="header" href="#manual-headers">Manual headers</a></h2>
|
||||||
<h2 id="my header"><a class="header" href="#my header"><a href="#foo">My Header</a></a></h2>
|
<h2 id="my header"><a href="#foo">My Header</a></h2>
|
||||||
|
|
||||||
<h3 id="another-header"><a class="header" href="#another-header">Another header</a></h3>
|
<h3>Another header</h3>
|
||||||
|
|
@ -2,13 +2,13 @@
|
||||||
<p>Test for definition lists manually written in HTML.</p>
|
<p>Test for definition lists manually written in HTML.</p>
|
||||||
<dl>
|
<dl>
|
||||||
|
|
||||||
<dt id="some-tag"><a class="header" href="#some-tag">Some tag</a></dt>
|
<dt>Some tag</dt>
|
||||||
|
|
||||||
|
|
||||||
<dd>Some defintion</dd>
|
<dd>Some defintion</dd>
|
||||||
|
|
||||||
|
|
||||||
<dt class="myclass" id="myid"><a class="header" href="#myid"><a class="option-anchor" href="#foo">Another definition</a></a></dt>
|
<dt class="myclass" id="myid"><a class="option-anchor" href="#foo">Another definition</a></dt>
|
||||||
|
|
||||||
|
|
||||||
<dd class="def-class">A definition.</dd>
|
<dd class="def-class">A definition.</dd>
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@
|
||||||
<p>Test for definition lists manually written in HTML.</p>
|
<p>Test for definition lists manually written in HTML.</p>
|
||||||
<dl>
|
<dl>
|
||||||
|
|
||||||
<dt id="some-tag"><a class="header" href="#some-tag">Some tag</a></dt>
|
<dt>Some tag</dt>
|
||||||
|
|
||||||
|
|
||||||
<dd>Some defintion</dd>
|
<dd>Some defintion</dd>
|
||||||
|
|
||||||
|
|
||||||
<dt class="myclass" id="myid"><a class="header" href="#myid"><a class="option-anchor" href="#foo">Another definition</a></a></dt>
|
<dt class="myclass" id="myid"><a class="option-anchor" href="#foo">Another definition</a></dt>
|
||||||
|
|
||||||
|
|
||||||
<dd class="def-class">A definition.</dd>
|
<dd class="def-class">A definition.</dd>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue