From c1c8e267225d688898f8a7941eea383fb30af073 Mon Sep 17 00:00:00 2001 From: Emile Bangma Date: Thu, 19 Sep 2024 18:17:33 +0000 Subject: [PATCH] Parsers work, tagpage/folderpage broken --- quartz.config.ts | 4 +- quartz/plugins/parsers/custom/default.ts | 4 +- quartz/plugins/parsers/obsidian/arrows.ts | 5 +- .../parsers/obsidian/blockreference.ts | 4 +- quartz/plugins/parsers/obsidian/callouts.ts | 4 +- quartz/plugins/parsers/obsidian/checkboxes.ts | 4 +- quartz/plugins/parsers/obsidian/comments.ts | 10 +- quartz/plugins/parsers/obsidian/highlights.ts | 5 +- quartz/plugins/parsers/obsidian/mermaid.ts | 4 +- quartz/plugins/parsers/obsidian/tags.ts | 66 ++++---- quartz/plugins/parsers/obsidian/wikilinks.ts | 144 +++++++++--------- quartz/plugins/parsers/obsidian/youtube.ts | 4 +- quartz/plugins/transformers/markdown.ts | 64 ++++++-- quartz/plugins/types.ts | 3 +- 14 files changed, 183 insertions(+), 142 deletions(-) diff --git a/quartz.config.ts b/quartz.config.ts index b6abbb2d3..8ab0ac662 100644 --- a/quartz.config.ts +++ b/quartz.config.ts @@ -77,8 +77,8 @@ const config: QuartzConfig = { Plugin.AliasRedirects(), Plugin.ComponentResources(), Plugin.ContentPage(), - Plugin.FolderPage(), - Plugin.TagPage(), + //Plugin.FolderPage(), + //Plugin.TagPage(), Plugin.ContentIndex({ enableSiteMap: true, enableRSS: true, diff --git a/quartz/plugins/parsers/custom/default.ts b/quartz/plugins/parsers/custom/default.ts index c7e2adfb0..d01ed46a0 100644 --- a/quartz/plugins/parsers/custom/default.ts +++ b/quartz/plugins/parsers/custom/default.ts @@ -23,11 +23,11 @@ export const CustomDefault: QuartzParser> = (userOpts) => { return src }, markdownPlugins(_ctx) { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, _file) => { - const replacements: [RegExp, string | ReplaceFunction][] = [] mdastFindReplace(tree, replacements) } - return plug + return replacements }, htmlPlugins() { const plug: Pluggable = () => {} diff --git a/quartz/plugins/parsers/obsidian/arrows.ts b/quartz/plugins/parsers/obsidian/arrows.ts index db62a0c1b..137ca8038 100644 --- a/quartz/plugins/parsers/obsidian/arrows.ts +++ b/quartz/plugins/parsers/obsidian/arrows.ts @@ -42,9 +42,9 @@ export const ObsidianArrow: QuartzParser> = (userOpts) => { return src }, markdownPlugins(_ctx) { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, _path) => { if (opts.enabled) { - const replacements: [RegExp, string | ReplaceFunction][] = [] replacements.push([ arrowRegex, (value: string, ..._capture: string[]) => { @@ -56,10 +56,9 @@ export const ObsidianArrow: QuartzParser> = (userOpts) => { } }, ]) - mdastFindReplaceInHtml(tree, replacements, opts.inHtml) } } - return plug + return replacements }, htmlPlugins() { const plug: Pluggable = () => {} diff --git a/quartz/plugins/parsers/obsidian/blockreference.ts b/quartz/plugins/parsers/obsidian/blockreference.ts index 64cf392ec..8e9118d4f 100644 --- a/quartz/plugins/parsers/obsidian/blockreference.ts +++ b/quartz/plugins/parsers/obsidian/blockreference.ts @@ -27,11 +27,11 @@ export const ObsidianBlockReference: QuartzParser> = (userOpts) return src }, markdownPlugins(_ctx) { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, _file) => { - const replacements: [RegExp, string | ReplaceFunction][] = [] mdastFindReplace(tree, replacements) } - return plug + return replacements }, htmlPlugins() { const inlineTagTypes = new Set(["p", "li"]) diff --git a/quartz/plugins/parsers/obsidian/callouts.ts b/quartz/plugins/parsers/obsidian/callouts.ts index 6a9f337b5..3c6dd4cc0 100644 --- a/quartz/plugins/parsers/obsidian/callouts.ts +++ b/quartz/plugins/parsers/obsidian/callouts.ts @@ -1,6 +1,7 @@ import { QuartzParser } from "../../types" import { JSResource } from "../../../util/resources" import { Root, BlockContent, DefinitionContent, Paragraph, Html } from "mdast" +import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace" import { visit } from "unist-util-visit" import { Pluggable } from "unified" // @ts-ignore @@ -77,6 +78,7 @@ export const ObsidianCallouts: QuartzParser> = (userOpts) => { return src }, markdownPlugins(_ctx) { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, _path) => { if (opts.enabled) { visit(tree, "blockquote", (node) => { @@ -181,7 +183,7 @@ export const ObsidianCallouts: QuartzParser> = (userOpts) => { }) } } - return plug + return replacements }, htmlPlugins() { const plug: Pluggable = () => {} diff --git a/quartz/plugins/parsers/obsidian/checkboxes.ts b/quartz/plugins/parsers/obsidian/checkboxes.ts index 8236e3875..e1d567251 100644 --- a/quartz/plugins/parsers/obsidian/checkboxes.ts +++ b/quartz/plugins/parsers/obsidian/checkboxes.ts @@ -30,11 +30,11 @@ export const ObsidianCheckboxes: QuartzParser> = (userOpts) => return src }, markdownPlugins(_ctx) { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, _file) => { - const replacements: [RegExp, string | ReplaceFunction][] = [] mdastFindReplaceInHtml(tree, replacements, opts.inHtml) } - return plug + return replacements }, htmlPlugins() { if (opts.enabled) { diff --git a/quartz/plugins/parsers/obsidian/comments.ts b/quartz/plugins/parsers/obsidian/comments.ts index ed9325c30..8329af721 100644 --- a/quartz/plugins/parsers/obsidian/comments.ts +++ b/quartz/plugins/parsers/obsidian/comments.ts @@ -33,13 +33,9 @@ export const ObsidianComments: QuartzParser> = (userOpts) => { return src }, markdownPlugins(_ctx) { - const plug: Pluggable = (tree: Root, _file) => { - if (opts.enabled) { - const replacements: [RegExp, string | ReplaceFunction][] = [] - mdastFindReplaceInHtml(tree, replacements, opts.inHtml) - } - } - return plug + const replacements: [RegExp, string | ReplaceFunction][] = [] + const plug: Pluggable = (tree: Root, _file) => {} + return replacements }, htmlPlugins() { const plug: Pluggable = () => {} diff --git a/quartz/plugins/parsers/obsidian/highlights.ts b/quartz/plugins/parsers/obsidian/highlights.ts index 3000e9091..82ed209a9 100644 --- a/quartz/plugins/parsers/obsidian/highlights.ts +++ b/quartz/plugins/parsers/obsidian/highlights.ts @@ -30,9 +30,9 @@ export const ObsidianHighlights: QuartzParser> = (userOpts) => return src }, markdownPlugins(_ctx) { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, _path) => { if (opts.enabled) { - const replacements: [RegExp, string | ReplaceFunction][] = [] replacements.push([ highlightRegex, (_value: string, ...capture: string[]) => { @@ -43,10 +43,9 @@ export const ObsidianHighlights: QuartzParser> = (userOpts) => } }, ]) - mdastFindReplaceInHtml(tree, replacements, opts.inHtml) } } - return plug + return replacements }, htmlPlugins() { const plug: Pluggable = () => {} diff --git a/quartz/plugins/parsers/obsidian/mermaid.ts b/quartz/plugins/parsers/obsidian/mermaid.ts index c868b8b7b..234500af6 100644 --- a/quartz/plugins/parsers/obsidian/mermaid.ts +++ b/quartz/plugins/parsers/obsidian/mermaid.ts @@ -1,5 +1,6 @@ import { QuartzParser } from "../../types" import { JSResource } from "../../../util/resources" +import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace" import { visit } from "unist-util-visit" import { Root, Code } from "mdast" import { Pluggable } from "unified" @@ -25,6 +26,7 @@ export const ObsidianMermaid: QuartzParser> = (userOpts) => { return src }, markdownPlugins() { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, _file) => { if (opts.enabled) { visit(tree, "code", (node: Code) => { @@ -38,7 +40,7 @@ export const ObsidianMermaid: QuartzParser> = (userOpts) => { }) } } - return plug + return replacements }, htmlPlugins() { const plug: Pluggable = () => {} diff --git a/quartz/plugins/parsers/obsidian/tags.ts b/quartz/plugins/parsers/obsidian/tags.ts index d6257a8c7..13b59d86d 100644 --- a/quartz/plugins/parsers/obsidian/tags.ts +++ b/quartz/plugins/parsers/obsidian/tags.ts @@ -3,6 +3,7 @@ import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util- import { FilePath, pathToRoot, slugTag, slugifyFilePath } from "../../../util/path" import { JSResource } from "../../../util/resources" import { Root } from "mdast" +import path from "path" import { Pluggable } from "unified" import { mdastFindReplaceInHtml } from "../../transformers/markdown" @@ -35,43 +36,44 @@ export const ObsidianTags: QuartzParser> = (userOpts) => { return src }, markdownPlugins(_ctx) { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, file) => { - const base = pathToRoot(file.data.slug!) - const replacements: [RegExp, string | ReplaceFunction][] = [] - replacements.push([ - tagRegex, - (_value: string, tag: string) => { - // Check if the tag only includes numbers and slashes - if (/^[\/\d]+$/.test(tag)) { - return false - } + if (opts.enabled) { + const base = pathToRoot(file.data.slug!) + replacements.push([ + tagRegex, + (_value: string, tag: string) => { + // Check if the tag only includes numbers and slashes + if (/^[\/\d]+$/.test(tag)) { + return false + } - tag = slugTag(tag) - if (file.data.frontmatter) { - const noteTags = file.data.frontmatter.tags ?? [] - file.data.frontmatter.tags = [...new Set([...noteTags, tag])] - } + tag = slugTag(tag) + if (file.data.frontmatter) { + const noteTags = file.data.frontmatter.tags ?? [] + file.data.frontmatter.tags = [...new Set([...noteTags, tag])] + } - return { - type: "link", - url: base + `/tags/${tag}`, - data: { - hProperties: { - className: ["tag-link"], + return { + type: "link", + url: base + `/tags/${tag}`, + data: { + hProperties: { + className: ["tag-link"], + }, }, - }, - children: [ - { - type: "text", - value: tag, - }, - ], - } - }, - ]) - mdastFindReplaceInHtml(tree, replacements, opts.inHtml) + children: [ + { + type: "text", + value: tag, + }, + ], + } + }, + ]) + } } - return plug + return replacements }, htmlPlugins() { const plug: Pluggable = () => {} diff --git a/quartz/plugins/parsers/obsidian/wikilinks.ts b/quartz/plugins/parsers/obsidian/wikilinks.ts index 702410443..7a88b8e11 100644 --- a/quartz/plugins/parsers/obsidian/wikilinks.ts +++ b/quartz/plugins/parsers/obsidian/wikilinks.ts @@ -5,6 +5,7 @@ import { JSResource } from "../../../util/resources" import { Root } from "mdast" import { Pluggable } from "unified" import { mdastFindReplaceInHtml } from "../../transformers/markdown" +import path from "path" interface Options { enabled: Boolean @@ -82,82 +83,85 @@ export const ObsidianWikilinks: QuartzParser> = (userOpts) => { return src }, markdownPlugins(_ctx) { - const plug: Pluggable = (tree: Root, path) => { - const replacements: [RegExp, string | ReplaceFunction][] = [] - replacements.push([ - wikilinkRegex, - (value: string, ...capture: string[]) => { - let [rawFp, rawHeader, rawAlias] = capture - const fp = rawFp?.trim() ?? "" - const anchor = rawHeader?.trim() ?? "" - const alias = rawAlias?.slice(1).trim() + const replacements: [RegExp, string | ReplaceFunction][] = [] + const plug: Pluggable = (tree: Root, file) => { + if (opts.enabled) { + replacements.push([ + wikilinkRegex, + (value: string, ...capture: string[]) => { + let [rawFp, rawHeader, rawAlias] = capture + const fp = rawFp?.trim() ?? "" + const anchor = rawHeader?.trim() ?? "" + const alias = rawAlias?.slice(1).trim() - // embed cases - if (value.startsWith("!")) { - const ext: string = path.extname(fp).toLowerCase() - const url = slugifyFilePath(fp as FilePath) - if ([".png", ".jpg", ".jpeg", ".gif", ".bmp", ".svg", ".webp"].includes(ext)) { - const match = wikilinkImageEmbedRegex.exec(alias ?? "") - const alt = match?.groups?.alt ?? "" - const width = match?.groups?.width ?? "auto" - const height = match?.groups?.height ?? "auto" - return { - type: "image", - url, - data: { - hProperties: { - width, - height, - alt, + // embed cases + if (value.startsWith("!")) { + const ext: string = path.extname(fp).toLowerCase() + const url = slugifyFilePath(fp as FilePath) + if ([".png", ".jpg", ".jpeg", ".gif", ".bmp", ".svg", ".webp"].includes(ext)) { + const match = wikilinkImageEmbedRegex.exec(alias ?? "") + const alt = match?.groups?.alt ?? "" + const width = match?.groups?.width ?? "auto" + const height = match?.groups?.height ?? "auto" + return { + type: "image", + url, + data: { + hProperties: { + width, + height, + alt, + }, }, - }, - } - } else if ([".mp4", ".webm", ".ogv", ".mov", ".mkv"].includes(ext)) { - return { - type: "html", - value: ``, - } - } else if ([".mp3", ".webm", ".wav", ".m4a", ".ogg", ".3gp", ".flac"].includes(ext)) { - return { - type: "html", - value: ``, - } - } else if ([".pdf"].includes(ext)) { - return { - type: "html", - value: ``, - } - } else { - const block = anchor - return { - type: "html", - data: { hProperties: { transclude: true } }, - value: `
Transclude of ${url}${block}
`, + } + } else if ([".mp4", ".webm", ".ogv", ".mov", ".mkv"].includes(ext)) { + return { + type: "html", + value: ``, + } + } else if ( + [".mp3", ".webm", ".wav", ".m4a", ".ogg", ".3gp", ".flac"].includes(ext) + ) { + return { + type: "html", + value: ``, + } + } else if ([".pdf"].includes(ext)) { + return { + type: "html", + value: ``, + } + } else { + const block = anchor + return { + type: "html", + data: { hProperties: { transclude: true } }, + value: `
Transclude of ${url}${block}
`, + } } + + // otherwise, fall through to regular link } - // otherwise, fall through to regular link - } - - // internal link - const url = fp + anchor - return { - type: "link", - url, - children: [ - { - type: "text", - value: alias ?? fp, - }, - ], - } - }, - ]) - mdastFindReplaceInHtml(tree, replacements, opts.inHtml) + // internal link + const url = fp + anchor + return { + type: "link", + url, + children: [ + { + type: "text", + value: alias ?? fp, + }, + ], + } + }, + ]) + } } - return plug + return replacements }, htmlPlugins() { const plug: Pluggable = () => {} diff --git a/quartz/plugins/parsers/obsidian/youtube.ts b/quartz/plugins/parsers/obsidian/youtube.ts index 3da312e04..7a240689e 100644 --- a/quartz/plugins/parsers/obsidian/youtube.ts +++ b/quartz/plugins/parsers/obsidian/youtube.ts @@ -28,11 +28,11 @@ export const ObsidianYouTube: QuartzParser> = (userOpts) => { return src }, markdownPlugins(_ctx) { + const replacements: [RegExp, string | ReplaceFunction][] = [] const plug: Pluggable = (tree: Root, _file) => { - const replacements: [RegExp, string | ReplaceFunction][] = [] mdastFindReplace(tree, replacements) } - return plug + return replacements }, htmlPlugins() { if (opts.enabled) { diff --git a/quartz/plugins/transformers/markdown.ts b/quartz/plugins/transformers/markdown.ts index bb19d51dd..8e3e57144 100644 --- a/quartz/plugins/transformers/markdown.ts +++ b/quartz/plugins/transformers/markdown.ts @@ -184,8 +184,9 @@ export const mdastFindReplaceInHtml = ( } } }) + } else { + mdastFindReplace(tree, replacements) } - mdastFindReplace(tree, replacements) } export const CommonMarkFlavoredMarkdown: QuartzTransformerPlugin> = ( @@ -254,7 +255,9 @@ export const GitHubFlavoredMarkdown: QuartzTransformerPlugin { + return (tree: Root, file) => { + mdastFindReplaceInHtml(tree, replacements, inHtml) + } + }) + /*plugins.push(() => { return (tree: Root, file) => { //const replacements: [RegExp, string | ReplaceFunction][] = [] @@ -305,7 +329,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin { + return (tree: Root, file) => + ObsidianVideo({ enabled: opts.enableVideoEmbed }).markdownPlugins(ctx) + }) + plugins.push(() => { + return (tree: Root, file) => + ObsidianCallouts({ enabled: opts.callouts }).markdownPlugins(ctx) + }) + plugins.push(() => { + return (tree: Root, file) => ObsidianMermaid({ enabled: opts.mermaid }).markdownPlugins(ctx) + }) return plugins }, htmlPlugins() { const plugins: PluggableList = [rehypeRaw] - plugins.push( - ObsidianBlockReference({ enabled: opts.parseBlockReferences }).htmlPlugins() as Pluggable, - ) - plugins.push(ObsidianYouTube({ enabled: opts.enableYouTubeEmbed }).htmlPlugins() as Pluggable) - plugins.push.apply( - ObsidianCheckboxes({ enabled: opts.enableCheckbox }).htmlPlugins() as Pluggable[], - ) + plugins.push(() => { + return (tree: Root, file) => + ObsidianBlockReference({ enabled: opts.parseBlockReferences }).htmlPlugins() as Pluggable + }) + plugins.push(() => { + return (tree: Root, file) => + ObsidianYouTube({ enabled: opts.enableYouTubeEmbed }).htmlPlugins() as Pluggable + }) + /*plugins.push(() => { + return (tree: HtmlRoot, file) => ObsidianCheckboxes({ enabled: opts.enableCheckbox }).htmlPlugins() as Pluggable} + )*/ return plugins }, diff --git a/quartz/plugins/types.ts b/quartz/plugins/types.ts index a621b8487..6017566ab 100644 --- a/quartz/plugins/types.ts +++ b/quartz/plugins/types.ts @@ -1,5 +1,6 @@ import { PluggableList, Pluggable } from "unified" import { JSResource, StaticResources } from "../util/resources" +import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace" import { ProcessedContent } from "./vfile" import { QuartzComponent } from "../components/types" import { FilePath } from "../util/path" @@ -56,7 +57,7 @@ export type QuartzParser = ( export type QuartzParserInstance = { name: string textTransform: (ctx: BuildCtx, src: string | Buffer) => string | Buffer - markdownPlugins: (ctx: BuildCtx) => Pluggable + markdownPlugins: (ctx: BuildCtx) => [RegExp, string | ReplaceFunction][] htmlPlugins: () => Pluggable | Pluggable[] externalResources: () => JSResource | string }