Skip to content

Commit

Permalink
normalize page paths in preview (#1249)
Browse files Browse the repository at this point in the history
* normalize page paths in preview

closes #1248

* comment; normalize earlier

---------

Co-authored-by: Mike Bostock <mbostock@gmail.com>
  • Loading branch information
Fil and mbostock authored Apr 16, 2024
1 parent d31bfef commit a9cbbf6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export class PreviewServer {
} else {
if ((pathname = normalize(pathname)).startsWith("..")) throw new Error("Invalid path: " + pathname);

// Normalize the pathname (e.g., dropping ".html").
// Normalize the pathname (e.g., adding ".html" if cleanUrls is false,
// dropping ".html" if cleanUrls is true) and redirect if necessary.
const normalizedPathname = config.md.normalizeLink(pathname);
if (url.pathname !== normalizedPathname) {
res.writeHead(302, {Location: normalizedPathname + url.search});
Expand All @@ -181,11 +182,14 @@ export class PreviewServer {
}

// If this path ends with a slash, then add an implicit /index to the
// end of the path.
// end of the path. Otherwise, remove the .html extension (we use clean
// paths as the internal canonical representation; see normalizePage).
let path = join(root, pathname);
if (pathname.endsWith("/")) {
pathname = join(pathname, "index");
path = join(path, "index");
} else {
pathname = pathname.replace(/\.html$/, "");
}

// Lastly, serve the corresponding Markdown file, if it exists.
Expand Down

0 comments on commit a9cbbf6

Please sign in to comment.