From 906f91f8eed5e91a7afae95c7002a3e4553d6aae Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Thu, 13 Jul 2023 07:19:35 +0000
Subject: [PATCH] base path refactor, more docs

---
 quartz/plugins/transformers/links.ts |   35 ++++++++++++++++++++++++++---------
 1 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/quartz/plugins/transformers/links.ts b/quartz/plugins/transformers/links.ts
index 17e30f5..dd6f6f2 100644
--- a/quartz/plugins/transformers/links.ts
+++ b/quartz/plugins/transformers/links.ts
@@ -1,5 +1,5 @@
 import { QuartzTransformerPlugin } from "../types"
-import { clientSideSlug, relative, relativeToRoot, slugify, trimPathSuffix } from "../../path"
+import { CanonicalSlug, transformInternalLink } from "../../path"
 import path from "path"
 import { visit } from 'unist-util-visit'
 import isAbsoluteUrl from "is-absolute-url"
@@ -27,18 +27,35 @@
     htmlPlugins() {
       return [() => {
         return (tree, file) => {
-          const curSlug = clientSideSlug(file.data.slug!)
+          const curSlug = file.data.slug!
           const transformLink = (target: string) => {
-            const targetSlug = slugify(decodeURI(target).trim())
+            const targetSlug = transformInternalLink(target) 
             if (opts.markdownLinkResolution === 'relative' && !path.isAbsolute(targetSlug)) {
               return './' + relative(curSlug, targetSlug)
-            } else {
-              return './' + relativeToRoot(curSlug, targetSlug)
+            } else if (opts.markdownLinkResolution === 'shortest') {
+              // https://forum.obsidian.md/t/settings-new-link-format-what-is-shortest-path-when-possible/6748/5
+              const allSlugs = file.data.allSlugs!
+
+              // if the file name is unique, then it's just the filename
+              const matchingFileNames = allSlugs.filter(slug => {
+                const parts = toServerSlug(slug).split(path.posix.sep)
+                const fileName = parts.at(-1)
+                return targetSlug === fileName
+              })
+
+              if (matchingFileNames.length === 1) {
+                const targetSlug = toServerSlug(matchingFileNames[0])
+                return './' + relativeToRoot(curSlug, targetSlug)
+              }
+
+              // if it's not unique, then it's the absolute path from the vault root
+              // (fall-through case)
             }
-            // todo: handle shortest path
+            // treat as absolute
+            return './' + relativeToRoot(curSlug, targetSlug)
           }
 
-          const outgoing: Set<string> = new Set()
+          const outgoing: Set<CanonicalSlug> = new Set()
           visit(tree, 'element', (node, _index, _parent) => {
             // rewrite all links
             if (
@@ -53,7 +70,7 @@
               if (!(isAbsoluteUrl(dest) || dest.startsWith("#"))) {
                 node.properties.href = transformLink(dest)
               }
-              
+
               dest = node.properties.href
               if (dest.startsWith(".")) {
                 const normalizedPath = path.normalize(path.join(curSlug, dest))
@@ -96,6 +113,6 @@
 
 declare module 'vfile' {
   interface DataMap {
-    links: string[]
+    links: CanonicalSlug[]
   }
 }

--
Gitblit v1.10.0