From b44a79eebaf1664512ef5ff4e3246b3a6a416ff4 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Tue, 19 Dec 2023 19:40:59 +0000
Subject: [PATCH] fix: wikilinks should allow external links (closes #639)

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

diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index d0f2ee5..f8a8393 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -105,6 +105,8 @@
   return calloutMapping[callout] ?? "note"
 }
 
+export const externalLinkRegex = /^https?:\/\//i
+
 // !?               -> optional embedding
 // \[\[             -> open brace
 // ([^\[\]\|\#]+)   -> one or more non-special characters ([,],|, or #) (name)
@@ -158,13 +160,19 @@
         }
 
         src = src.replaceAll(wikilinkRegex, (value, ...capture) => {
-          const [rawFp, rawHeader, rawAlias] = capture
+          const [rawFp, rawHeader, rawAlias]: (string | undefined)[] = capture
+
           const fp = rawFp ?? ""
           const anchor = rawHeader?.trim().replace(/^#+/, "")
           const blockRef = Boolean(anchor?.startsWith("^")) ? "^" : ""
           const displayAnchor = anchor ? `#${blockRef}${slugAnchor(anchor)}` : ""
           const displayAlias = rawAlias ?? rawHeader?.replace("#", "|") ?? ""
           const embedDisplay = value.startsWith("!") ? "!" : ""
+
+          if (rawFp?.match(externalLinkRegex)) {
+            return `${embedDisplay}[${displayAlias.replace(/^\|/, "")}](${rawFp})`
+          }
+
           return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]`
         })
       }

--
Gitblit v1.10.0