From 9c8fec06d2b58e4e3bbe280ddc665a99fcc4878c Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sun, 09 Mar 2025 22:33:15 +0000
Subject: [PATCH] feat: support non-singleton search
---
quartz/components/TableOfContents.tsx | 100 +++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 87 insertions(+), 13 deletions(-)
diff --git a/quartz/components/TableOfContents.tsx b/quartz/components/TableOfContents.tsx
index 531c61d..da6eece 100644
--- a/quartz/components/TableOfContents.tsx
+++ b/quartz/components/TableOfContents.tsx
@@ -1,21 +1,95 @@
-import { QuartzComponentConstructor, QuartzComponentProps } from "./types"
-import style from "./styles/toc.scss"
+import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
+import legacyStyle from "./styles/legacyToc.scss"
+import modernStyle from "./styles/toc.scss"
+import { classNames } from "../util/lang"
-function TableOfContents({ fileData }: QuartzComponentProps) {
+// @ts-ignore
+import script from "./scripts/toc.inline"
+import { i18n } from "../i18n"
+import OverflowList from "./OverflowList"
+
+interface Options {
+ layout: "modern" | "legacy"
+}
+
+const defaultOptions: Options = {
+ layout: "modern",
+}
+
+const TableOfContents: QuartzComponent = ({
+ fileData,
+ displayClass,
+ cfg,
+}: QuartzComponentProps) => {
if (!fileData.toc) {
return null
}
- return <details class="toc" open>
- <summary><h3>Table of Contents</h3></summary>
- <ul>
- {fileData.toc.map(tocEntry => <li key={tocEntry.slug} class={`depth-${tocEntry.depth}`}>
- <a href={`#${tocEntry.slug}`}>{tocEntry.text}</a>
- </li>)}
- </ul>
- </details>
+ return (
+ <div class={classNames(displayClass, "toc")}>
+ <button
+ type="button"
+ class={fileData.collapseToc ? "collapsed toc-header" : "toc-header"}
+ aria-controls="toc-content"
+ aria-expanded={!fileData.collapseToc}
+ >
+ <h3>{i18n(cfg.locale).components.tableOfContents.title}</h3>
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width="24"
+ height="24"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="2"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ class="fold"
+ >
+ <polyline points="6 9 12 15 18 9"></polyline>
+ </svg>
+ </button>
+ <div class={fileData.collapseToc ? "collapsed toc-content" : "toc-content"}>
+ <OverflowList id="toc-ul">
+ {fileData.toc.map((tocEntry) => (
+ <li key={tocEntry.slug} class={`depth-${tocEntry.depth}`}>
+ <a href={`#${tocEntry.slug}`} data-for={tocEntry.slug}>
+ {tocEntry.text}
+ </a>
+ </li>
+ ))}
+ </OverflowList>
+ </div>
+ </div>
+ )
}
+TableOfContents.css = modernStyle
+TableOfContents.afterDOMLoaded = script + OverflowList.afterDOMLoaded("toc-ul")
-TableOfContents.css = style
+const LegacyTableOfContents: QuartzComponent = ({ fileData, cfg }: QuartzComponentProps) => {
+ if (!fileData.toc) {
+ return null
+ }
+ return (
+ <details class="toc" open={!fileData.collapseToc}>
+ <summary>
+ <h3>{i18n(cfg.locale).components.tableOfContents.title}</h3>
+ </summary>
+ <ul>
+ {fileData.toc.map((tocEntry) => (
+ <li key={tocEntry.slug} class={`depth-${tocEntry.depth}`}>
+ <a href={`#${tocEntry.slug}`} data-for={tocEntry.slug}>
+ {tocEntry.text}
+ </a>
+ </li>
+ ))}
+ </ul>
+ </details>
+ )
+}
+LegacyTableOfContents.css = legacyStyle
-export default (() => TableOfContents) satisfies QuartzComponentConstructor
+export default ((opts?: Partial<Options>) => {
+ const layout = opts?.layout ?? defaultOptions.layout
+ return layout === "modern" ? TableOfContents : LegacyTableOfContents
+}) satisfies QuartzComponentConstructor
--
Gitblit v1.10.0