Merge pull request #1376 from dtolnay/html
Escape `<` and `>` in rendered toc to match handlebars' escaping in <title>
This commit is contained in:
commit
07e72757d3
|
@ -1,4 +1,5 @@
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
@ -102,7 +103,7 @@ impl HelperDef for RenderToc {
|
||||||
// Part title
|
// Part title
|
||||||
if let Some(title) = item.get("part") {
|
if let Some(title) = item.get("part") {
|
||||||
out.write("<li class=\"part-title\">")?;
|
out.write("<li class=\"part-title\">")?;
|
||||||
out.write(title)?;
|
write_escaped(out, title)?;
|
||||||
out.write("</li>")?;
|
out.write("</li>")?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -160,7 +161,7 @@ impl HelperDef for RenderToc {
|
||||||
html::push_html(&mut markdown_parsed_name, parser);
|
html::push_html(&mut markdown_parsed_name, parser);
|
||||||
|
|
||||||
// write to the handlebars template
|
// write to the handlebars template
|
||||||
out.write(&markdown_parsed_name)?;
|
write_escaped(out, &markdown_parsed_name)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if path_exists {
|
if path_exists {
|
||||||
|
@ -204,3 +205,18 @@ fn write_li_open_tag(
|
||||||
li.push_str("\">");
|
li.push_str("\">");
|
||||||
out.write(&li)
|
out.write(&li)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_escaped(out: &mut dyn Output, mut title: &str) -> io::Result<()> {
|
||||||
|
let needs_escape: &[char] = &['<', '>'];
|
||||||
|
while let Some(next) = title.find(needs_escape) {
|
||||||
|
out.write(&title[..next])?;
|
||||||
|
match title.as_bytes()[next] {
|
||||||
|
b'<' => out.write("<")?,
|
||||||
|
b'>' => out.write(">")?,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
title = &title[next + 1..];
|
||||||
|
}
|
||||||
|
out.write(title)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue