diff --git a/src/preview.ts b/src/preview.ts index 636767099..ab3e27b1e 100644 --- a/src/preview.ts +++ b/src/preview.ts @@ -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}); @@ -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.