From fea352849c6972da4b3b8935eb2e86f6cefc76ed Mon Sep 17 00:00:00 2001
From: Ben Schlegel <ben5.schlegel@gmail.com>
Date: Sat, 16 Sep 2023 17:45:21 +0000
Subject: [PATCH] fix: create deep copy of file passed into tree

---
 quartz/plugins/transformers/links.ts |   42 +++++++++++++++++++++++++++++++-----------
 1 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/quartz/plugins/transformers/links.ts b/quartz/plugins/transformers/links.ts
index d867039..e050e00 100644
--- a/quartz/plugins/transformers/links.ts
+++ b/quartz/plugins/transformers/links.ts
@@ -1,11 +1,11 @@
 import { QuartzTransformerPlugin } from "../types"
 import {
-  CanonicalSlug,
+  FullSlug,
   RelativeURL,
+  SimpleSlug,
   TransformOptions,
   _stripSlashes,
-  canonicalizeServer,
-  joinSegments,
+  simplifySlug,
   splitAnchor,
   transformLink,
 } from "../../util/path"
@@ -33,8 +33,8 @@
       return [
         () => {
           return (tree, file) => {
-            const curSlug = canonicalizeServer(file.data.slug!)
-            const outgoing: Set<CanonicalSlug> = new Set()
+            const curSlug = simplifySlug(file.data.slug!)
+            const outgoing: Set<SimpleSlug> = new Set()
 
             const transformOptions: TransformOptions = {
               strategy: opts.markdownLinkResolution,
@@ -53,16 +53,32 @@
                 node.properties.className.push(isAbsoluteUrl(dest) ? "external" : "internal")
 
                 // don't process external links or intra-document anchors
-                if (!(isAbsoluteUrl(dest) || dest.startsWith("#"))) {
-                  dest = node.properties.href = transformLink(curSlug, dest, transformOptions)
-                  const canonicalDest = path.posix.normalize(joinSegments(curSlug, dest))
+                const isInternal = !(isAbsoluteUrl(dest) || dest.startsWith("#"))
+                if (isInternal) {
+                  dest = node.properties.href = transformLink(
+                    file.data.slug!,
+                    dest,
+                    transformOptions,
+                  )
+
+                  // url.resolve is considered legacy
+                  // WHATWG equivalent https://nodejs.dev/en/api/v18/url/#urlresolvefrom-to
+                  const url = new URL(dest, `https://base.com/${curSlug}`)
+                  const canonicalDest = url.pathname
                   const [destCanonical, _destAnchor] = splitAnchor(canonicalDest)
-                  outgoing.add(destCanonical as CanonicalSlug)
+
+                  // need to decodeURIComponent here as WHATWG URL percent-encodes everything
+                  const simple = decodeURIComponent(
+                    simplifySlug(destCanonical as FullSlug),
+                  ) as SimpleSlug
+                  outgoing.add(simple)
+                  node.properties["data-slug"] = simple
                 }
 
                 // rewrite link internals if prettylinks is on
                 if (
                   opts.prettyLinks &&
+                  isInternal &&
                   node.children.length === 1 &&
                   node.children[0].type === "text" &&
                   !node.children[0].value.startsWith("#")
@@ -79,7 +95,11 @@
               ) {
                 if (!isAbsoluteUrl(node.properties.src)) {
                   let dest = node.properties.src as RelativeURL
-                  dest = node.properties.src = transformLink(curSlug, dest, transformOptions)
+                  dest = node.properties.src = transformLink(
+                    file.data.slug!,
+                    dest,
+                    transformOptions,
+                  )
                   node.properties.src = dest
                 }
               }
@@ -95,6 +115,6 @@
 
 declare module "vfile" {
   interface DataMap {
-    links: CanonicalSlug[]
+    links: SimpleSlug[]
   }
 }

--
Gitblit v1.10.0