From e82ba97a396e4e9d77486b48e6ee8b1dfd1c1b4c Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Mon, 24 Jul 2023 07:07:58 +0000
Subject: [PATCH] actually add processed tag to frontmatter

---
 quartz/plugins/transformers/ofm.ts |   47 ++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index 23b1401..9840ea8 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -9,7 +9,7 @@
 import { JSResource } from "../../resources"
 // @ts-ignore
 import calloutScript from "../../components/scripts/callout.inline.ts"
-import { FilePath, slugifyFilePath } from "../../path"
+import { FilePath, canonicalizeServer, pathToRoot, slugifyFilePath } from "../../path"
 
 export interface Options {
   comments: boolean
@@ -17,6 +17,7 @@
   wikilinks: boolean
   callouts: boolean
   mermaid: boolean
+  parseTags: boolean
 }
 
 const defaultOptions: Options = {
@@ -25,6 +26,7 @@
   wikilinks: true,
   callouts: true,
   mermaid: true,
+  parseTags: true,
 }
 
 const icons = {
@@ -97,22 +99,19 @@
   return s.substring(0, 1).toUpperCase() + s.substring(1)
 }
 
-// Match wikilinks
 // !?               -> optional embedding
 // \[\[             -> open brace
 // ([^\[\]\|\#]+)   -> one or more non-special characters ([,],|, or #) (name)
 // (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link)
 // (|[^\[\]\|\#]+)? -> | then one or more non-special characters (alias)
 const wikilinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)(#[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g")
-
-// Match highlights
 const highlightRegex = new RegExp(/==(.+)==/, "g")
-
-// Match comments
 const commentRegex = new RegExp(/%%(.+)%%/, "g")
-
 // from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
 const calloutRegex = new RegExp(/^\[\!(\w+)\]([+-]?)/)
+// (?:^| )   -> non-capturing group, tag should start be separated by a space or be the start of the line
+// #(\w+)    -> tag itself is # followed by a string of alpha-numeric characters
+const tagRegex = new RegExp(/(?:^| )#(\w+)/, "g")
 
 export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options> | undefined> = (
   userOpts,
@@ -120,7 +119,7 @@
   const opts = { ...defaultOptions, ...userOpts }
   return {
     name: "ObsidianFlavoredMarkdown",
-    textTransform(src) {
+    textTransform(_ctx, src) {
       // pre-transform wikilinks (fix anchors to things that may contain illegal syntax e.g. codeblocks, latex)
       if (opts.wikilinks) {
         src = src.toString()
@@ -317,7 +316,7 @@
               if (node.lang === "mermaid") {
                 node.data = {
                   hProperties: {
-                    className: "mermaid",
+                    className: ["mermaid"],
                   },
                 }
               }
@@ -326,6 +325,36 @@
         })
       }
 
+      if (opts.parseTags) {
+        plugins.push(() => {
+          return (tree: Root, file) => {
+            const slug = canonicalizeServer(file.data.slug!)
+            const base = pathToRoot(slug)
+            findAndReplace(tree, tagRegex, (value: string, tag: string) => {
+              if (file.data.frontmatter) {
+                file.data.frontmatter.tags.push(tag)
+              }
+
+              return {
+                type: "link",
+                url: base + `/tags/${slugAnchor(tag)}`,
+                data: {
+                  hProperties: {
+                    className: ["tag-link"],
+                  },
+                },
+                children: [
+                  {
+                    type: "text",
+                    value,
+                  },
+                ],
+              }
+            })
+          }
+        })
+      }
+
       return plugins
     },
     htmlPlugins() {

--
Gitblit v1.10.0