|
| 1 | +import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types" |
| 2 | +import legacyStyle from "./styles/legacyToc.scss" |
| 3 | +import modernStyle from "./styles/toc.scss" |
| 4 | +import { classNames } from "../util/lang" |
| 5 | + |
| 6 | +// @ts-ignore |
| 7 | +import script from "./scripts/toc.inline" |
| 8 | +import { i18n } from "../i18n" |
| 9 | + |
| 10 | +interface Options { |
| 11 | + layout: "modern" | "legacy" |
| 12 | +} |
| 13 | + |
| 14 | +const defaultOptions: Options = { |
| 15 | + layout: "modern", |
| 16 | +} |
| 17 | + |
| 18 | +const TableOfContents: QuartzComponent = ({ |
| 19 | + fileData, |
| 20 | + displayClass, |
| 21 | + cfg, |
| 22 | +}: QuartzComponentProps) => { |
| 23 | + if (!fileData.toc) { |
| 24 | + return null |
| 25 | + } |
| 26 | + |
| 27 | + return ( |
| 28 | + <div class={classNames(displayClass, "toc")}> |
| 29 | + <button |
| 30 | + type="button" |
| 31 | + id="toc2" |
| 32 | + class={fileData.collapseToc ? "collapsed" : ""} |
| 33 | + aria-controls="toc-content" |
| 34 | + aria-expanded={!fileData.collapseToc} |
| 35 | + > |
| 36 | + <h3>{i18n(cfg.locale).components.tableOfContents.title}</h3> |
| 37 | + <svg |
| 38 | + xmlns="http://www.w3.org/2000/svg" |
| 39 | + width="24" |
| 40 | + height="24" |
| 41 | + viewBox="0 0 24 24" |
| 42 | + fill="none" |
| 43 | + stroke="currentColor" |
| 44 | + stroke-width="2" |
| 45 | + stroke-linecap="round" |
| 46 | + stroke-linejoin="round" |
| 47 | + class="fold" |
| 48 | + > |
| 49 | + <polyline points="6 9 12 15 18 9"></polyline> |
| 50 | + </svg> |
| 51 | + </button> |
| 52 | + <div id="toc-content" class={fileData.collapseToc ? "collapsed" : ""}> |
| 53 | + <ul class="overflow"> |
| 54 | + {fileData.toc.map((tocEntry) => ( |
| 55 | + <li key={tocEntry.slug} class={`depth-${tocEntry.depth}`}> |
| 56 | + <a href={`#${tocEntry.slug}`} data-for={tocEntry.slug}> |
| 57 | + {tocEntry.text} |
| 58 | + </a> |
| 59 | + </li> |
| 60 | + ))} |
| 61 | + </ul> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + ) |
| 65 | +} |
| 66 | +TableOfContents.css = modernStyle |
| 67 | +TableOfContents.afterDOMLoaded = script |
| 68 | + |
| 69 | +const LegacyTableOfContents: QuartzComponent = ({ fileData, cfg }: QuartzComponentProps) => { |
| 70 | + if (!fileData.toc) { |
| 71 | + return null |
| 72 | + } |
| 73 | + return ( |
| 74 | + <details id="toc" open={!fileData.collapseToc}> |
| 75 | + <summary> |
| 76 | + <h3>{i18n(cfg.locale).components.tableOfContents.title}</h3> |
| 77 | + </summary> |
| 78 | + <ul> |
| 79 | + {fileData.toc.map((tocEntry) => ( |
| 80 | + <li key={tocEntry.slug} class={`depth-${tocEntry.depth}`}> |
| 81 | + <a href={`#${tocEntry.slug}`} data-for={tocEntry.slug}> |
| 82 | + {tocEntry.text} |
| 83 | + </a> |
| 84 | + </li> |
| 85 | + ))} |
| 86 | + </ul> |
| 87 | + </details> |
| 88 | + ) |
| 89 | +} |
| 90 | +LegacyTableOfContents.css = legacyStyle |
| 91 | + |
| 92 | +export default ((opts?: Partial<Options>) => { |
| 93 | + const layout = opts?.layout ?? defaultOptions.layout |
| 94 | + return layout === "modern" ? TableOfContents : LegacyTableOfContents |
| 95 | +}) satisfies QuartzComponentConstructor |
0 commit comments