From 4e82b0d8ce805ca832e08bafcfe5144e352c2d05 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sat, 13 Jan 2024 17:37:24 +0000
Subject: [PATCH] docs: add sidneys artist handbook to showcase
---
quartz/plugins/transformers/toc.ts | 80 +++++++++++++++++++++++++--------------
1 files changed, 51 insertions(+), 29 deletions(-)
diff --git a/quartz/plugins/transformers/toc.ts b/quartz/plugins/transformers/toc.ts
index 01e0c8d..d0781ec 100644
--- a/quartz/plugins/transformers/toc.ts
+++ b/quartz/plugins/transformers/toc.ts
@@ -2,62 +2,84 @@
import { Root } from "mdast"
import { visit } from "unist-util-visit"
import { toString } from "mdast-util-to-string"
-import { slug as slugAnchor } from 'github-slugger'
+import Slugger from "github-slugger"
+import { wikilinkRegex } from "./ofm"
export interface Options {
- maxDepth: 1 | 2 | 3 | 4 | 5 | 6,
- minEntries: 1,
+ maxDepth: 1 | 2 | 3 | 4 | 5 | 6
+ minEntries: 1
showByDefault: boolean
+ collapseByDefault: boolean
}
const defaultOptions: Options = {
maxDepth: 3,
minEntries: 1,
showByDefault: true,
+ collapseByDefault: false,
}
interface TocEntry {
- depth: number,
- text: string,
+ depth: number
+ text: string
slug: string // this is just the anchor (#some-slug), not the canonical slug
}
-export const TableOfContents: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => {
+const regexMdLinks = new RegExp(/\[([^\[]+)\](\(.*\))/, "g")
+export const TableOfContents: QuartzTransformerPlugin<Partial<Options> | undefined> = (
+ userOpts,
+) => {
const opts = { ...defaultOptions, ...userOpts }
return {
name: "TableOfContents",
markdownPlugins() {
- return [() => {
- return async (tree: Root, file) => {
- const display = file.data.frontmatter?.enableToc ?? opts.showByDefault
- if (display) {
- const toc: TocEntry[] = []
- let highestDepth: number = opts.maxDepth
- visit(tree, 'heading', (node) => {
- if (node.depth <= opts.maxDepth) {
- const text = toString(node)
- highestDepth = Math.min(highestDepth, node.depth)
- toc.push({
- depth: node.depth,
- text,
- slug: slugAnchor(text)
- })
- }
- })
+ return [
+ () => {
+ return async (tree: Root, file) => {
+ const display = file.data.frontmatter?.enableToc ?? opts.showByDefault
+ if (display) {
+ const slugAnchor = new Slugger()
+ const toc: TocEntry[] = []
+ let highestDepth: number = opts.maxDepth
+ visit(tree, "heading", (node) => {
+ if (node.depth <= opts.maxDepth) {
+ let text = toString(node)
- if (toc.length > opts.minEntries) {
- file.data.toc = toc.map(entry => ({ ...entry, depth: entry.depth - highestDepth }))
+ // strip link formatting from toc entries
+ text = text.replace(wikilinkRegex, (_, rawFp, __, rawAlias) => {
+ const fp = rawFp?.trim() ?? ""
+ const alias = rawAlias?.slice(1).trim()
+ return alias ?? fp
+ })
+ text = text.replace(regexMdLinks, "$1")
+
+ highestDepth = Math.min(highestDepth, node.depth)
+ toc.push({
+ depth: node.depth,
+ text,
+ slug: slugAnchor.slug(text),
+ })
+ }
+ })
+
+ if (toc.length > opts.minEntries) {
+ file.data.toc = toc.map((entry) => ({
+ ...entry,
+ depth: entry.depth - highestDepth,
+ }))
+ file.data.collapseToc = opts.collapseByDefault
+ }
}
}
- }
- }]
+ },
+ ]
},
}
}
-declare module 'vfile' {
+declare module "vfile" {
interface DataMap {
toc: TocEntry[]
+ collapseToc: boolean
}
}
-
--
Gitblit v1.10.0