Skip to content

render empty toc if needed #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2023
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
17 changes: 10 additions & 7 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type RenderInternalOptions =
async function render(parseResult: ParseResult, options: RenderOptions & RenderInternalOptions): Promise<string> {
const {root, path, pages, title, preview} = options;
const toc = mergeToc(parseResult.data?.toc, options.toc);
const headers = toc.show ? findHeaders(parseResult) : [];
return String(html`<!DOCTYPE html>
<meta charset="utf-8">${path === "/404" ? html`\n<base href="/">` : ""}
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
Expand Down Expand Up @@ -85,7 +84,7 @@ ${
preview ? `\nopen({hash: ${JSON.stringify(parseResult.hash)}, eval: (body) => (0, eval)(body)});\n` : ""
}${parseResult.cells.map((cell) => `\n${renderDefineCell(cell)}`).join("")}`)}
</script>${pages.length > 0 ? html`\n${await renderSidebar(title, pages, path)}` : ""}${
headers.length > 0 ? html`\n${renderToc(headers, toc.label)}` : ""
toc.show ? html`\n${renderToc(findHeaders(parseResult), toc.label)}` : ""
}
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
Expand Down Expand Up @@ -139,16 +138,20 @@ function findHeaders(parseResult: ParseResult): Header[] {
.filter((d): d is Header => !!d.label && !!d.href);
}

function renderToc(headers: Header[], label = "Contents"): Html {
function renderToc(headers: Header[], label: string): Html {
return html`<aside id="observablehq-toc" data-selector="${tocSelector
.map((selector) => `#observablehq-main ${selector}`)
.join(", ")}">
<nav>
<nav>${
headers.length > 0
? html`
<div>${label}</div>
<ol>${headers.map(
({label, href}) => html`\n<li class="observablehq-secondary-link"><a href="${href}">${label}</a></li>`
)}
</ol>
({label, href}) => html`\n<li class="observablehq-secondary-link"><a href="${href}">${label}</a></li>`
)}
</ol>`
: ""
}
</nav>
</aside>`;
}
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/404/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import "./_observablehq/client.js";

</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="page-not-found" tabindex="-1"><a class="observablehq-header-anchor" href="#page-not-found">Page not found</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/archives/tar.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could set toc: false on all these tests? not so much for this PR but so that all these tests don't depend on the way the toc is rendered in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to add a config file for all the tests? Kinda related to #312. Maybe I can hack the test somehow to make it the default at least for tests. 🤔

Copy link
Contributor

@Fil Fil Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests need it though. We could enumerate them here:

export async function normalizeConfig(spec: any, root: string): Promise<Config> {
-  let {title, pages = await readPages(root), pager = true, toc = true} = spec;
+  let {title, pages = await readPages(root), pager = true, toc = !root.startsWith("test/input/") || root === "test/input/build/config" || root === "test/input/build/subtitle" || … } = spec;

but maybe it's better to do nothing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn’t want to bake test-specific logic into non-test code, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general our method of snapshot testing means widescale changes when the generated HTML changes. I opted out of that for the client init script since it’s minified and impossible to review, but I don’t think we actually need to suppress the TOC output in these test snapshots — it’s truthful to what we want to test here.

Copy link
Contributor

@Fil Fil Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's worth the effort to create test-specific configs, but it's good to know that it is a possibility if this gets annoying.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(For example, if we change whether the TOC is visible by default, or have some more advanced heuristic to decide whether to show it, it’s nice to see that reflected in the snapshots to reaffirm what is changing.)

<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="tar" tabindex="-1"><a class="observablehq-header-anchor" href="#tar">Tar</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/archives/zip.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="zip" tabindex="-1"><a class="observablehq-header-anchor" href="#zip">Zip</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/config/closed/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="a-page%E2%80%A6" tabindex="-1"><a class="observablehq-header-anchor" href="#a-page%E2%80%A6">A page…</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/config/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="index" tabindex="-1"><a class="observablehq-header-anchor" href="#index">Index</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/config/one.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="one" tabindex="-1"><a class="observablehq-header-anchor" href="#one">One</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/config/sub/two.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="two" tabindex="-1"><a class="observablehq-header-anchor" href="#two">Two</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/fetches/foo.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="top" tabindex="-1"><a class="observablehq-header-anchor" href="#top">Top</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/fetches/top.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="top" tabindex="-1"><a class="observablehq-header-anchor" href="#top">Top</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/files/files.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<span><link rel="stylesheet" href="./_file/custom-styles.css">
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/files/subsection/subfiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<span><link rel="stylesheet" href="../_file/custom-styles.css">
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/imports/foo/foo.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="foo" tabindex="-1"><a class="observablehq-header-anchor" href="#foo">Foo</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/missing-file/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="build-test-case" tabindex="-1"><a class="observablehq-header-anchor" href="#build-test-case">Build test case</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/missing-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="build-test-case" tabindex="-1"><a class="observablehq-header-anchor" href="#build-test-case">Build test case</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/multi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="multi-test" tabindex="-1"><a class="observablehq-header-anchor" href="#multi-test">Multi test</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/multi/subsection/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="sub-section" tabindex="-1"><a class="observablehq-header-anchor" href="#sub-section">Sub-Section</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/simple-public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="build-test-case" tabindex="-1"><a class="observablehq-header-anchor" href="#build-test-case">Build test case</a></h1>
Expand Down
4 changes: 4 additions & 0 deletions test/output/build/simple/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
</ol>
</nav>
<script>{/* redacted init script */}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="build-test-case" tabindex="-1"><a class="observablehq-header-anchor" href="#build-test-case">Build test case</a></h1>
Expand Down