Skip to content

Commit 865860e

Browse files
authored
Fix index.html clean-URL rewrite mangling alternate-format links on index pages (#14670)
quarto-nav.js rewrites links on page load to clean up index.html URLs with an unanchored regex, so any href merely starting with /index.html got mangled. On a site's index page the "Other Formats" link to index.html.md (the markdown twin from output-file: index.html paired with a markdown format, e.g. nbdev's llms.txt workflow) was rewritten to /.md, which 404s. Anchor the regex to the end of the path while still allowing a trailing query or fragment. - Fix the same unanchored pattern in itemHasNavTarget (navbar/sidebar active-state matching) and in the preview reload-target normalization; preview bundle rebuilt from source - Add a playwright test covering all five href cases (clean URL, fragment, query, markdown twin, path segment), passing in chromium/firefox/webkit and failing against the old regex Fixes #14667.
1 parent 8577ed1 commit 865860e

10 files changed

Lines changed: 69 additions & 4 deletions

File tree

‎news/changelog-1.10.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ All changes included in 1.10:
6060
- ([#13565](https://github.com/quarto-dev/quarto-cli/issues/13565), [#14353](https://github.com/quarto-dev/quarto-cli/issues/14353)): Fix sidebar logo not appearing on secondary sidebars in multi-sidebar website layouts.
6161
- ([#14562](https://github.com/quarto-dev/quarto-cli/issues/14562)): Fix a heading inside `content-hidden when-format="llms-txt"` (visible in HTML) losing its `<section>` wrapper and `id` in a website with `llms-txt` enabled, which broke its table-of-contents link, anchors, and cross-references.
6262
- ([#14563](https://github.com/quarto-dev/quarto-cli/issues/14563)): Fix a fatal error when a shortcode is used inside conditional content (e.g. `content-visible when-format="llms-txt"`) in a website with `llms-txt` enabled.
63+
- ([#14667](https://github.com/quarto-dev/quarto-cli/issues/14667)): Fix the clean-URL rewrite in `quarto-nav.js` breaking links to files whose names merely start with `index.html`, such as the `index.html.md` "Other Formats" links produced when `output-file: index.html` is paired with a markdown format (e.g. by nbdev for llms.txt workflows). The rewrite now only strips `index.html` at the end of the path.
6364

6465
## Commands
6566

‎src/project/types/website/website-shared.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,5 +474,5 @@ export function containsHref(href: string, items: SidebarItem[]) {
474474

475475
export function itemHasNavTarget(item: SidebarItem, href: string) {
476476
return item.href === href ||
477-
item.href === href.replace(/\/index\.html/, "/");
477+
item.href === href.replace(/\/index\.html(?=[?#]|$)/, "/");
478478
}

‎src/resources/projects/website/navigation/quarto-nav.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ window.document.addEventListener("DOMContentLoaded", function () {
274274
for (let i = 0; i < links.length; i++) {
275275
if (links[i].href) {
276276
links[i].dataset.originalHref = links[i].href;
277-
links[i].href = links[i].href.replace(/\/index\.html/, "/");
277+
// only strip index.html at the end of the path (optionally followed
278+
// by a query or fragment) so that files like index.html.md
279+
// (alternate-format twins) are left alone (#14667)
280+
links[i].href = links[i].href.replace(/\/index\.html(?=[?#]|$)/, "/");
278281
}
279282
}
280283

‎src/webui/quarto-preview/dist/quarto-preview.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/webui/quarto-preview/src/server/navigation.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export function navigationHandler() {
88

99
return () => {
1010
const normalizeTarget = (target: string) => {
11-
return target.replace(/\/index\.html/, "/")
11+
// only strip index.html at the end of the path so that files like
12+
// index.html.md (alternate-format twins) are left alone (#14667)
13+
return target.replace(/\/index\.html(?=[?#]|$)/, "/")
1214
};
1315

1416
return (ev: MessageEvent<string>): boolean => {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.quarto/
2+
/_site/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project:
2+
type: website
3+
4+
website:
5+
title: "issue-14667"
6+
7+
format: html
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Home
3+
---
4+
5+
Links exercising the clean-URL rewrite in `quarto-nav.js` (issue #14667).
6+
The rewrite must strip `index.html` only at the end of the path, leaving
7+
names that merely start with `index.html` (like the `index.html.md`
8+
markdown twins generated for llms.txt workflows) untouched.
9+
10+
- [markdown twin](index.html.md){#lnk-md} — must NOT be rewritten
11+
- [clean url](sub/index.html){#lnk-clean} — must become `sub/`
12+
- [fragment](index.html#section){#lnk-frag} — must become `#section` on `./`
13+
- [query](index.html?a=1){#lnk-query} — must become `./?a=1`
14+
- [path segment](index.html/foo){#lnk-segment} — must NOT be rewritten
15+
16+
## Section {#section}
17+
18+
Target for the fragment link.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Sub page
3+
---
4+
5+
Target page for the clean-URL link on the home page.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
// quarto-nav.js rewrites every link on page load to clean up index.html
4+
// URLs. The rewrite must only strip index.html at the end of the path
5+
// (optionally followed by a query or fragment); hrefs that merely start
6+
// with index.html — like the index.html.md markdown twins generated for
7+
// llms.txt workflows — must be left alone (#14667).
8+
test("index.html clean-URL rewrite only strips whole path segments", async ({ page }) => {
9+
await page.goto("./website/issue-14667/_site/index.html");
10+
11+
// data-original-href is stamped by the rewrite loop, so waiting on it
12+
// ensures quarto-nav.js has processed the links
13+
const mdLink = page.locator("#lnk-md");
14+
await expect(mdLink).toHaveAttribute("data-original-href", /\/index\.html\.md$/);
15+
16+
// a link to an index.html-prefixed file must not be rewritten
17+
await expect(mdLink).toHaveAttribute("href", /\/index\.html\.md$/);
18+
19+
// ...while genuine index.html links still get cleaned up
20+
await expect(page.locator("#lnk-clean")).toHaveAttribute("href", /\/sub\/$/);
21+
await expect(page.locator("#lnk-frag")).toHaveAttribute("href", /\/_site\/#section$/);
22+
await expect(page.locator("#lnk-query")).toHaveAttribute("href", /\/_site\/\?a=1$/);
23+
24+
// index.html as an intermediate path segment is not the index page of
25+
// the link's directory, so it must be left alone too
26+
await expect(page.locator("#lnk-segment")).toHaveAttribute("href", /\/index\.html\/foo$/);
27+
});

0 commit comments

Comments
 (0)