Skip to content

Commit a9cbbf6

Browse files
Filmbostock
andauthored
normalize page paths in preview (#1249)
* normalize page paths in preview closes #1248 * comment; normalize earlier --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent d31bfef commit a9cbbf6

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/preview.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ export class PreviewServer {
172172
} else {
173173
if ((pathname = normalize(pathname)).startsWith("..")) throw new Error("Invalid path: " + pathname);
174174

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

183184
// If this path ends with a slash, then add an implicit /index to the
184-
// end of the path.
185+
// end of the path. Otherwise, remove the .html extension (we use clean
186+
// paths as the internal canonical representation; see normalizePage).
185187
let path = join(root, pathname);
186188
if (pathname.endsWith("/")) {
187189
pathname = join(pathname, "index");
188190
path = join(path, "index");
191+
} else {
192+
pathname = pathname.replace(/\.html$/, "");
189193
}
190194

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

0 commit comments

Comments
 (0)