diff --git a/quartz/plugins/parsers/github/index.ts b/quartz/plugins/parsers/github/index.ts index e69de29bb..edf2c56fd 100644 --- a/quartz/plugins/parsers/github/index.ts +++ b/quartz/plugins/parsers/github/index.ts @@ -0,0 +1,2 @@ +export { GitHubSmartypants } from "./smartypants" +export { GitHubLinkheadings } from "./linkheadings" diff --git a/quartz/plugins/parsers/github/linkheadings.ts b/quartz/plugins/parsers/github/linkheadings.ts index e69de29bb..a802e24e1 100644 --- a/quartz/plugins/parsers/github/linkheadings.ts +++ b/quartz/plugins/parsers/github/linkheadings.ts @@ -0,0 +1,87 @@ +import { QuartzParser } from "../../types" +import { JSResource } from "../../../util/resources" +import { Pluggable } from "unified" +import rehypeSlug from "rehype-slug" +import rehypeAutolinkHeadings from "rehype-autolink-headings" + +interface Options { + enabled: Boolean +} + +const defaultOptions: Options = { + enabled: true, +} + +export const GitHubLinkheadings: QuartzParser> = (userOpts) => { + const opts: Options = { ...defaultOptions, ...userOpts } + return { + name: "GitHubLinkheadings", + textTransform(_ctx, src: string | Buffer) { + if (src instanceof Buffer) { + src = src.toString() + } + return src + }, + markdownPlugins(_ctx) { + const plug: Pluggable = () => {} + return plug + }, + htmlPlugins() { + if (opts.enabled) { + return [ + rehypeSlug, + [ + rehypeAutolinkHeadings, + { + behavior: "append", + properties: { + role: "anchor", + ariaHidden: true, + tabIndex: -1, + "data-no-popover": true, + }, + content: { + type: "element", + tagName: "svg", + properties: { + width: 18, + height: 18, + viewBox: "0 0 24 24", + fill: "none", + stroke: "currentColor", + "stroke-width": "2", + "stroke-linecap": "round", + "stroke-linejoin": "round", + }, + children: [ + { + type: "element", + tagName: "path", + properties: { + d: "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71", + }, + children: [], + }, + { + type: "element", + tagName: "path", + properties: { + d: "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71", + }, + children: [], + }, + ], + }, + }, + ], + ] as Pluggable[] + } else { + return [] as Pluggable[] + } + }, + externalResources() { + const js = {} as JSResource + return js + }, + } +} diff --git a/quartz/plugins/parsers/github/smartypants.ts b/quartz/plugins/parsers/github/smartypants.ts new file mode 100644 index 000000000..a7c385088 --- /dev/null +++ b/quartz/plugins/parsers/github/smartypants.ts @@ -0,0 +1,40 @@ +import { QuartzParser } from "../../types" +import { JSResource } from "../../../util/resources" +import { Pluggable } from "unified" +import remarkGfm from "remark-gfm" +import smartypants from "remark-smartypants" + +interface Options { + enabled: Boolean +} + +const defaultOptions: Options = { + enabled: true, +} + +export const GitHubSmartypants: QuartzParser> = (userOpts) => { + const opts: Options = { ...defaultOptions, ...userOpts } + return { + name: "GitHubSmartypants", + textTransform(_ctx, src: string | Buffer) { + if (src instanceof Buffer) { + src = src.toString() + } + return src + }, + markdownPlugins(_ctx) { + if (opts.enabled) { + return [remarkGfm, smartypants] + } + return [remarkGfm] + }, + htmlPlugins() { + const plug: Pluggable = () => {} + return plug + }, + externalResources() { + const js = {} as JSResource + return js + }, + } +} diff --git a/quartz/plugins/transformers/markdown.ts b/quartz/plugins/transformers/markdown.ts index d35053735..c40e47336 100644 --- a/quartz/plugins/transformers/markdown.ts +++ b/quartz/plugins/transformers/markdown.ts @@ -26,7 +26,7 @@ import { toHast } from "mdast-util-to-hast" import { toHtml } from "hast-util-to-html" import { PhrasingContent } from "mdast-util-find-and-replace/lib" import { capitalize } from "../../util/lang" -import { PluggableList } from "unified" +import { Pluggable, PluggableList } from "unified" import { Node } from "unist" import { VFile } from "vfile" import { BuildVisitor } from "unist-util-visit" @@ -35,6 +35,8 @@ import smartypants from "remark-smartypants" import rehypeSlug from "rehype-slug" import rehypeAutolinkHeadings from "rehype-autolink-headings" +import { GitHubLinkheadings, GitHubSmartypants } from "../parsers/github" + import { ObsidianArrow, ObsidianCallouts, @@ -213,11 +215,15 @@ export const GitHubFlavoredMarkdown: QuartzTransformerPlugin string | Buffer markdownPlugins: (ctx: BuildCtx) => Pluggable - htmlPlugins: () => Pluggable + htmlPlugins: () => Pluggable | Pluggable[] externalResources: () => JSResource | string }