2024-02-14 11:53:44 +07:00
|
|
|
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
|
2023-06-17 09:41:59 +07:00
|
|
|
import legacyStyle from "./styles/legacyToc.scss"
|
|
|
|
import modernStyle from "./styles/toc.scss"
|
2024-01-30 12:51:13 +07:00
|
|
|
import { classNames } from "../util/lang"
|
2023-06-10 13:06:02 +07:00
|
|
|
|
2023-06-18 02:07:40 +07:00
|
|
|
// @ts-ignore
|
|
|
|
import script from "./scripts/toc.inline"
|
2024-02-05 11:57:10 +07:00
|
|
|
import { i18n } from "../i18n"
|
2023-06-18 02:07:40 +07:00
|
|
|
|
2023-06-13 12:41:42 +07:00
|
|
|
interface Options {
|
2023-07-23 07:27:41 +07:00
|
|
|
layout: "modern" | "legacy"
|
2023-06-13 12:41:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultOptions: Options = {
|
2023-07-23 07:27:41 +07:00
|
|
|
layout: "modern",
|
2023-06-10 13:06:02 +07:00
|
|
|
}
|
|
|
|
|
2024-02-14 11:53:44 +07:00
|
|
|
const TableOfContents: QuartzComponent = ({
|
|
|
|
fileData,
|
|
|
|
displayClass,
|
|
|
|
cfg,
|
|
|
|
}: QuartzComponentProps) => {
|
2023-06-18 02:07:40 +07:00
|
|
|
if (!fileData.toc) {
|
|
|
|
return null
|
|
|
|
}
|
2023-06-12 13:46:38 +07:00
|
|
|
|
2023-07-23 07:27:41 +07:00
|
|
|
return (
|
2024-01-30 12:51:13 +07:00
|
|
|
<div class={classNames(displayClass, "toc")}>
|
2023-11-05 02:11:42 +07:00
|
|
|
<button type="button" id="toc" class={fileData.collapseToc ? "collapsed" : ""}>
|
2024-02-05 11:57:10 +07:00
|
|
|
<h3>{i18n(cfg.locale).components.tableOfContents.title}</h3>
|
2023-07-23 07:27:41 +07:00
|
|
|
<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 id="toc-content">
|
|
|
|
<ul class="overflow">
|
|
|
|
{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>
|
|
|
|
</div>
|
2023-06-18 02:07:40 +07:00
|
|
|
</div>
|
2023-07-23 07:27:41 +07:00
|
|
|
)
|
2023-06-18 02:07:40 +07:00
|
|
|
}
|
|
|
|
TableOfContents.css = modernStyle
|
|
|
|
TableOfContents.afterDOMLoaded = script
|
2023-06-17 09:41:59 +07:00
|
|
|
|
2024-02-14 11:53:44 +07:00
|
|
|
const LegacyTableOfContents: QuartzComponent = ({ fileData, cfg }: QuartzComponentProps) => {
|
2023-06-18 02:07:40 +07:00
|
|
|
if (!fileData.toc) {
|
|
|
|
return null
|
2023-06-17 09:41:59 +07:00
|
|
|
}
|
2023-07-23 07:27:41 +07:00
|
|
|
return (
|
2023-11-05 02:11:42 +07:00
|
|
|
<details id="toc" open={!fileData.collapseToc}>
|
2023-07-23 07:27:41 +07:00
|
|
|
<summary>
|
2024-02-05 11:57:10 +07:00
|
|
|
<h3>{i18n(cfg.locale).components.tableOfContents.title}</h3>
|
2023-07-23 07:27:41 +07:00
|
|
|
</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>
|
|
|
|
)
|
2023-06-17 09:41:59 +07:00
|
|
|
}
|
2023-06-18 02:07:40 +07:00
|
|
|
LegacyTableOfContents.css = legacyStyle
|
2023-06-17 09:41:59 +07:00
|
|
|
|
2023-06-18 02:07:40 +07:00
|
|
|
export default ((opts?: Partial<Options>) => {
|
|
|
|
const layout = opts?.layout ?? defaultOptions.layout
|
|
|
|
return layout === "modern" ? TableOfContents : LegacyTableOfContents
|
2023-06-13 12:41:42 +07:00
|
|
|
}) satisfies QuartzComponentConstructor
|