Skip to content

A slightly better TOC #298

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 3 commits into from
Dec 1, 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
2 changes: 1 addition & 1 deletion src/client/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const toc = document.querySelector("#observablehq-toc");
if (toc) {
const highlight = toc.appendChild(document.createElement("div"));
highlight.classList.add("observablehq-secondary-link-highlight");
const headings = Array.from(document.querySelectorAll("#observablehq-main h2")).reverse();
const headings = Array.from(document.querySelectorAll(toc.dataset.selector)).reverse();
const links = toc.querySelectorAll(".observablehq-secondary-link");
const relink = () => {
for (const link of links) {
Expand Down
8 changes: 6 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ interface Header {
href: string;
}

const tocSelector = ["h1:not(:first-of-type)", "h2:not(h1 + h2)"];

function findHeaders(parseResult: ParseResult): Header[] {
return Array.from(parseHTML(parseResult.html).document.querySelectorAll("h2"))
return Array.from(parseHTML(parseResult.html).document.querySelectorAll(tocSelector.join(", ")))
.map((node) => ({label: node.textContent, href: node.firstElementChild?.getAttribute("href")}))
.filter((d): d is Header => !!d.label && !!d.href);
}

function renderToc(headers: Header[], label = "Contents"): Html {
return html`<aside id="observablehq-toc">
return html`<aside id="observablehq-toc" data-selector="${tocSelector
.map((selector) => `#observablehq-main ${selector}`)
.join(", ")}">
<nav>
<div>${label}</div>
<ol>${headers.map(
Expand Down
22 changes: 22 additions & 0 deletions test/input/build/subtitle/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# A title
## A subtitle

The subtitle is not part of the TOC.

## First section

The first section is part of the TOC.

## Second section

The second section is also part of the TOC.

# An intermediate H1
## With its own subtitle

Not part of the TOC either.

## Third section

The third section is part of the TOC.

3 changes: 1 addition & 2 deletions test/output/build/config/toc-override.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
if (initialState) toggle.checked = initialState === "true";
else toggle.indeterminate = true;
}</script>
<aside id="observablehq-toc">
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
<div>dollar&amp;pound</div>
<ol>
<li class="observablehq-secondary-link"><a href="#h2%3A-section-1">H2: Section 1</a></li>
<li class="observablehq-secondary-link"><a href="#h2%3A-section-2">H2: Section 2</a></li>
</ol>
</nav>
Expand Down
3 changes: 1 addition & 2 deletions test/output/build/config/toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
if (initialState) toggle.checked = initialState === "true";
else toggle.indeterminate = true;
}</script>
<aside id="observablehq-toc">
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
<div>On this page</div>
<ol>
<li class="observablehq-secondary-link"><a href="#h2%3A-section-1">H2: Section 1</a></li>
<li class="observablehq-secondary-link"><a href="#h2%3A-section-2">H2: Section 2</a></li>
<li class="observablehq-secondary-link"><a href="#h2-%3Cscript%3Ealert(1)%3C%2Fscript%3E-not-nice">H2 &lt;script&gt;alert(1)&lt;/script&gt; not nice</a></li>
</ol>
Expand Down
60 changes: 60 additions & 0 deletions test/output/build/subtitle/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>A title</title>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Source+Serif+Pro:ital,wght@0,400;0,600;0,700;1,400;1,600;1,700&display=swap">
<link rel="stylesheet" type="text/css" href="./_observablehq/style.css">
<link rel="modulepreload" href="./_observablehq/runtime.js">
<script type="module">

import {define} from "./_observablehq/client.js";


</script>
<input id="observablehq-sidebar-toggle" type="checkbox">
<label id="observablehq-sidebar-backdrop" for="observablehq-sidebar-toggle"></label>
<nav id="observablehq-sidebar">
<ol>
<li class="observablehq-link observablehq-link-active"><a href="./">Home</a></li>
</ol>
<ol>
<li class="observablehq-link observablehq-link-active"><a href="./">A title</a></li>
</ol>
</nav>
<script>{
const toggle = document.querySelector("#observablehq-sidebar-toggle");
const initialState = localStorage.getItem("observablehq-sidebar");
if (initialState) toggle.checked = initialState === "true";
else toggle.indeterminate = true;
}</script>
<aside id="observablehq-toc" data-selector="#observablehq-main h1:not(:first-of-type), #observablehq-main h2:not(h1 + h2)">
<nav>
<div>Contents</div>
<ol>
<li class="observablehq-secondary-link"><a href="#first-section">First section</a></li>
<li class="observablehq-secondary-link"><a href="#second-section">Second section</a></li>
<li class="observablehq-secondary-link"><a href="#an-intermediate-h1">An intermediate H1</a></li>
<li class="observablehq-secondary-link"><a href="#third-section">Third section</a></li>
</ol>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="a-title" tabindex="-1"><a class="observablehq-header-anchor" href="#a-title">A title</a></h1>
<h2 id="a-subtitle" tabindex="-1"><a class="observablehq-header-anchor" href="#a-subtitle">A subtitle</a></h2>
<p>The subtitle is not part of the TOC.</p>
<h2 id="first-section" tabindex="-1"><a class="observablehq-header-anchor" href="#first-section">First section</a></h2>
<p>The first section is part of the TOC.</p>
<h2 id="second-section" tabindex="-1"><a class="observablehq-header-anchor" href="#second-section">Second section</a></h2>
<p>The second section is also part of the TOC.</p>
<h1 id="an-intermediate-h1" tabindex="-1"><a class="observablehq-header-anchor" href="#an-intermediate-h1">An intermediate H1</a></h1>
<h2 id="with-its-own-subtitle" tabindex="-1"><a class="observablehq-header-anchor" href="#with-its-own-subtitle">With its own subtitle</a></h2>
<p>Not part of the TOC either.</p>
<h2 id="third-section" tabindex="-1"><a class="observablehq-header-anchor" href="#third-section">Third section</a></h2>
<p>The third section is part of the TOC.</p>
</main>
<footer id="observablehq-footer">
<div>© 2023 Observable, Inc.</div>
</footer>
</div>