From 7bffc2183ece33e2d84909a5677dac0ae2563ddd Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sat, 12 Aug 2023 07:24:30 +0000
Subject: [PATCH] include home page in search

---
 quartz/plugins/transformers/ofm.ts |   36 ++++++++++++++++++++----------------
 1 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index a9beda4..6083a61 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -96,7 +96,7 @@
 
 function canonicalizeCallout(calloutName: string): keyof typeof callouts {
   let callout = calloutName.toLowerCase() as keyof typeof calloutMapping
-  return calloutMapping[callout]
+  return calloutMapping[callout] ?? calloutName
 }
 
 const capitalize = (s: string): string => {
@@ -108,14 +108,14 @@
 // ([^\[\]\|\#]+)   -> 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")
+const wikilinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)?(#[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g")
 const highlightRegex = new RegExp(/==(.+)==/, "g")
 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")
+const tagRegex = new RegExp(/(?:^| )#([\w-_\/]+)/, "g")
 
 export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options> | undefined> = (
   userOpts,
@@ -161,10 +161,11 @@
       if (opts.wikilinks) {
         src = src.toString()
         return src.replaceAll(wikilinkRegex, (value, ...capture) => {
-          const [fp, rawHeader, rawAlias] = capture
+          const [rawFp, rawHeader, rawAlias] = capture
+          const fp = rawFp ?? ""
           const anchor = rawHeader?.trim().slice(1)
           const displayAnchor = anchor ? `#${slugAnchor(anchor)}` : ""
-          const displayAlias = rawAlias ?? ""
+          const displayAlias = rawAlias ?? rawHeader?.replace("#", "|") ?? ""
           const embedDisplay = value.startsWith("!") ? "!" : ""
           return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]`
         })
@@ -177,14 +178,14 @@
         plugins.push(() => {
           return (tree: Root, _file) => {
             findAndReplace(tree, wikilinkRegex, (value: string, ...capture: string[]) => {
-              let [fp, rawHeader, rawAlias] = capture
-              fp = fp.trim()
+              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 | undefined = path.extname(fp).toLowerCase()
+                const ext: string = path.extname(fp).toLowerCase()
                 const url = slugifyFilePath(fp as FilePath) + ext
                 if ([".png", ".jpg", ".jpeg", ".gif", ".bmp", ".svg"].includes(ext)) {
                   const dims = alias ?? ""
@@ -218,14 +219,13 @@
                     type: "html",
                     value: `<iframe src="${url}"></iframe>`,
                   }
-                } else {
-                  // TODO: this is the node embed case
+                } else if (ext === "") {
+                  // TODO: note embed
                 }
                 // otherwise, fall through to regular link
               }
 
               // internal link
-              // const url = transformInternalLink(fp + anchor)
               const url = fp + anchor
               return {
                 type: "link",
@@ -414,12 +414,16 @@
         js.push({
           script: `
           import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
+          const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
+          mermaid.initialize({ 
+            startOnLoad: false,
+            securityLevel: 'loose',
+            theme: darkMode ? 'dark' : 'default'
+          });
           document.addEventListener('nav', async () => {
-            const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
-            mermaid.initialize({ 
-              securityLevel: 'loose',
-              theme: darkMode ? 'dark' : 'default'
-            });
+            await mermaid.run({
+              querySelector: '.mermaid'
+            })
           });
           `,
           loadTime: "afterDOMReady",

--
Gitblit v1.10.0