Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/renderer/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ impl HelperDef for RenderToc {
// Hack for windows who tends to use `\` as separator instead of `/`
.replace('\\', "/");

// Add link
// Add link - prepend with / to make it an absolute path from site root
out.write("/")?;
out.write(&tmp)?;
out.write(if is_toc_html {
"\" target=\"_parent\">"
Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ fn check_link_target_fallback() {
);
}

// Checks that sidebar links are absolute paths (start with a forward slash).
#[test]
fn check_sidebar_links_are_absolute() {
let doc = toc_js_html();

// Find all links in the chapter list
let links = doc.find(
Class("chapter")
.descendant(Name("li"))
.descendant(Name("a").and(Class("toggle").not()))
);

// Go through each link and check if its href attribute starts with a slash
for link in links {
if let Some(href) = link.attr("href") {
if !href.is_empty() && !href.starts_with("#") {
// Skip anchor links and empty hrefs
assert!(href.starts_with("/"), "Link '{}' should be an absolute path starting with '/'.", href);
}
}
}
}

// Checks formatting of summary names with inline elements.
#[test]
fn summary_with_markdown_formatting() {
Expand Down
Loading