Skip to content

Commit d4b0e6e

Browse files
dragonstylejjallaire
authored andcommitted
Improve toc-depth in html output
The `toc-depth` option will now control how deep html table of contents will display (previously, we always displayed to TOC to a depth of 3). This will control what depth of items are show when the `toc-location` is body. It will control what depth of items will be opened / tracked when scrolling when `toc-location` is left or right.
1 parent 14915c2 commit d4b0e6e

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/config/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ export const kSyntaxDefinition = "syntax-definition";
384384
export const kReferenceDoc = "reference-doc";
385385
export const kHtmlMathMethod = "html-math-method";
386386
export const kToc = "toc";
387+
export const kTocDepth = "toc-depth";
387388
export const kTableOfContents = "table-of-contents";
388389
export const kSectionDivs = "section-divs";
389390
export const kEPubCoverImage = "epub-cover-image";
@@ -499,7 +500,7 @@ export const kPandocDefaultsKeys = [
499500
"extract-media",
500501
kToc,
501502
kTableOfContents,
502-
"toc-depth",
503+
kTocDepth,
503504
kNumberSections,
504505
kNumberOffset,
505506
kShiftHeadingLevelBy,

src/config/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ import {
169169
kTitleBlockPublished,
170170
kTitlePrefix,
171171
kToc,
172+
kTocDepth,
172173
kTocTitleDocument,
173174
kTocTitleWebsite,
174175
kTopLevelDivision,
@@ -401,6 +402,7 @@ export interface FormatPandoc {
401402
[kCss]?: string | string[];
402403
[kToc]?: boolean;
403404
[kTableOfContents]?: boolean;
405+
[kTocDepth]?: number;
404406
[kListings]?: boolean;
405407
[kNumberSections]?: boolean;
406408
[kNumberOffset]?: number[];

src/format/html/format-html-bootstrap.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
kLinkCitations,
1919
kQuartoTemplateParams,
2020
kSectionDivs,
21+
kTocDepth,
2122
kTocLocation,
2223
} from "../../config/constants.ts";
2324
import {
@@ -255,6 +256,9 @@ function bootstrapHtmlPostprocessor(
255256

256257
const tocTarget = doc.getElementById("quarto-toc-target");
257258
if (toc && tocTarget) {
259+
// activate selection behavior for this
260+
toc.classList.add("toc-active");
261+
258262
// add nav-link class to the TOC links
259263
const tocLinks = doc.querySelectorAll('nav[role="doc-toc"] > ul a');
260264
for (let i = 0; i < tocLinks.length; i++) {
@@ -275,11 +279,17 @@ function bootstrapHtmlPostprocessor(
275279
}
276280

277281
// default collapse non-top level TOC nodes
278-
const nestedUls = toc.querySelectorAll("ul ul");
279-
for (let i = 0; i < nestedUls.length; i++) {
280-
const ul = nestedUls[i] as Element;
281-
ul.classList.add("collapse");
282+
const tocDepth = format.pandoc[kTocDepth] || 3;
283+
if (tocDepth > 1) {
284+
const ulSelector = "ul ".repeat(tocDepth - 1).trim();
285+
286+
const nestedUls = toc.querySelectorAll(ulSelector);
287+
for (let i = 0; i < nestedUls.length; i++) {
288+
const ul = nestedUls[i] as Element;
289+
ul.classList.add("collapse");
290+
}
282291
}
292+
283293
toc.remove();
284294
tocTarget.replaceWith(toc);
285295
} else {

src/resources/formats/html/quarto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const sectionChanged = new CustomEvent("quarto-sectionChanged", {
66
});
77

88
window.document.addEventListener("DOMContentLoaded", function (_event) {
9-
const tocEl = window.document.querySelector('nav[role="doc-toc"]');
9+
const tocEl = window.document.querySelector('nav.toc-active[role="doc-toc"]');
1010
const sidebarEl = window.document.getElementById("quarto-sidebar");
1111
const leftTocEl = window.document.getElementById("quarto-sidebar-toc-left");
1212
const marginSidebarEl = window.document.getElementById(

0 commit comments

Comments
 (0)