Skip to content
Merged
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
24 changes: 19 additions & 5 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,31 @@ export class PreviewServer {

// If this path is for /index, redirect to the parent directory for a
// tidy path. (This must be done before implicitly adding /index below!)
// Respect precedence of dir/index.md over dir.md in choosing between
// dir/ and dir!
if (basename(path, ".html") === "index") {
res.writeHead(302, {Location: dirname(pathname) + url.search});
res.end();
return;
try {
await stat(join(dirname(path), "index.md"));
res.writeHead(302, {Location: dirname(pathname) + "/" + url.search});
res.end();
return;
} catch (error) {
if (!isEnoent(error)) throw error;
res.writeHead(302, {Location: dirname(pathname) + url.search});
res.end();
return;
}
}

// If this path resolves to a directory, then add an implicit /index to
// the end of the path, assuming that the corresponding index.md exists.
try {
if ((await stat(path)).isDirectory() && (await stat(join(path, "index") + ".md")).isFile()) {
await access(join(path, "index") + ".md", constants.R_OK);
if ((await stat(path)).isDirectory() && (await stat(join(path, "index.md"))).isFile()) {
if (!pathname.endsWith("/")) {
res.writeHead(302, {Location: pathname + "/" + url.search});
res.end();
return;
}
pathname = join(pathname, "index");
path = join(path, "index");
}
Expand Down
4 changes: 3 additions & 1 deletion test/input/build/multi/subsection/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Sub-Section

This is a sub-section of the multi-section page.
This is a sub-section of the multi-section page.

We can link to [this sub-section](./) and to [the parent section](../).
1 change: 1 addition & 0 deletions test/output/build/multi/subsection/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<main id="observablehq-main" class="observablehq">
<h1 id="sub-section" tabindex="-1"><a class="observablehq-header-anchor" href="#sub-section">Sub-Section</a></h1>
<p>This is a sub-section of the multi-section page.</p>
<p>We can link to <a href="./">this sub-section</a> and to <a href="../">the parent section</a>.</p>
</main>
<footer id="observablehq-footer">
<nav><a rel="prev" href="../"><span>Home</span></a></nav>
Expand Down