Skip to content

Escape < and > in rendered toc to match handlebars' escaping in <title> #1376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2020
Merged
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
20 changes: 18 additions & 2 deletions src/renderer/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::io;
use std::path::Path;

use crate::utils;
Expand Down Expand Up @@ -102,7 +103,7 @@ impl HelperDef for RenderToc {
// Part title
if let Some(title) = item.get("part") {
out.write("<li class=\"part-title\">")?;
out.write(title)?;
write_escaped(out, title)?;
out.write("</li>")?;
continue;
}
Expand Down Expand Up @@ -160,7 +161,7 @@ impl HelperDef for RenderToc {
html::push_html(&mut markdown_parsed_name, parser);

// write to the handlebars template
out.write(&markdown_parsed_name)?;
write_escaped(out, &markdown_parsed_name)?;
}

if path_exists {
Expand Down Expand Up @@ -204,3 +205,18 @@ fn write_li_open_tag(
li.push_str("\">");
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("&lt;")?,
b'>' => out.write("&gt;")?,
_ => unreachable!(),
}
title = &title[next + 1..];
}
out.write(title)?;
Ok(())
}