From 14cbbdb8a2f69ebc51cd53a82b50206c543778b0 Mon Sep 17 00:00:00 2001
From: Oskar Manhart <52569953+oskardotglobal@users.noreply.github.com>
Date: Thu, 14 Sep 2023 03:55:59 +0000
Subject: [PATCH] feat: display tag in graph view (#466)

---
 quartz/plugins/transformers/links.ts |   47 +++++++++++++++++++++++++++++++++--------------
 1 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/quartz/plugins/transformers/links.ts b/quartz/plugins/transformers/links.ts
index 10b527c..e050e00 100644
--- a/quartz/plugins/transformers/links.ts
+++ b/quartz/plugins/transformers/links.ts
@@ -1,14 +1,14 @@
 import { QuartzTransformerPlugin } from "../types"
 import {
-  CanonicalSlug,
+  FullSlug,
   RelativeURL,
+  SimpleSlug,
   TransformOptions,
   _stripSlashes,
-  canonicalizeServer,
-  joinSegments,
+  simplifySlug,
   splitAnchor,
   transformLink,
-} from "../../path"
+} from "../../util/path"
 import path from "path"
 import { visit } from "unist-util-visit"
 import isAbsoluteUrl from "is-absolute-url"
@@ -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,9 +95,12 @@
               ) {
                 if (!isAbsoluteUrl(node.properties.src)) {
                   let dest = node.properties.src as RelativeURL
-                  const ext = path.extname(node.properties.src)
-                  dest = node.properties.src = transformLink(curSlug, dest, transformOptions)
-                  node.properties.src = dest + ext
+                  dest = node.properties.src = transformLink(
+                    file.data.slug!,
+                    dest,
+                    transformOptions,
+                  )
+                  node.properties.src = dest
                 }
               }
             })
@@ -96,6 +115,6 @@
 
 declare module "vfile" {
   interface DataMap {
-    links: CanonicalSlug[]
+    links: SimpleSlug[]
   }
 }

--
Gitblit v1.10.0