8000 Fix sidebar links to use absolute paths to prevent 404 errors by madyankin · Pull Request #2730 · rust-lang/mdBook · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix sidebar links to use absolute paths to prevent 404 errors #2730

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
0