From 7bde99b4e2d49e30dad1e0d58ccc34c2e7482005 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Mon, 14 Aug 2023 00:47:18 +0000
Subject: [PATCH] fix: add trailing slash to local serving

---
 quartz/path.ts |   43 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/quartz/path.ts b/quartz/path.ts
index 9af5c7b..8588c21 100644
--- a/quartz/path.ts
+++ b/quartz/path.ts
@@ -1,4 +1,5 @@
 import { slug } from "github-slugger"
+// this file must be isomorphic so it can't use node libs (e.g. path)
 
 // Quartz Paths
 // Things in boxes are not actual types but rather sources which these types can be acquired from
@@ -42,6 +43,8 @@
 //                                             └────────────┤ MD File ├─────┴─────────────────┘
 //                                                          └─────────┘
 
+export const QUARTZ = "quartz"
+
 /// Utility type to simulate nominal types in TypeScript
 type SlugLike<T> = string & { __brand: T }
 
@@ -194,7 +197,43 @@
   return results
 }
 
-export const QUARTZ = "quartz"
+export interface TransformOptions {
+  strategy: "absolute" | "relative" | "shortest"
+  allSlugs: ServerSlug[]
+}
+
+export function transformLink(
+  src: CanonicalSlug,
+  target: string,
+  opts: TransformOptions,
+): RelativeURL {
+  let targetSlug: string = transformInternalLink(target)
+
+  if (opts.strategy === "relative") {
+    return _addRelativeToStart(targetSlug) as RelativeURL
+  } else {
+    targetSlug = _stripSlashes(targetSlug.slice(".".length))
+    let [targetCanonical, targetAnchor] = splitAnchor(targetSlug)
+
+    if (opts.strategy === "shortest") {
+      // if the file name is unique, then it's just the filename
+      const matchingFileNames = opts.allSlugs.filter((slug) => {
+        const parts = slug.split("/")
+        const fileName = parts.at(-1)
+        return targetCanonical === fileName
+      })
+
+      // only match, just use it
+      if (matchingFileNames.length === 1) {
+        const targetSlug = canonicalizeServer(matchingFileNames[0])
+        return (resolveRelative(src, targetSlug) + targetAnchor) as RelativeURL
+      }
+    }
+
+    // if it's not unique, then it's the absolute path from the vault root
+    return joinSegments(pathToRoot(src), targetSlug) as RelativeURL
+  }
+}
 
 function _canonicalize(fp: string): string {
   fp = _trimSuffix(fp, "index")
@@ -221,7 +260,7 @@
 }
 
 function _getFileExtension(s: string): string | undefined {
-  return s.match(/\.[A-Za-z]+$/)?.[0]
+  return s.match(/\.[A-Za-z0-9]+$/)?.[0]
 }
 
 function _isRelativeSegment(s: string): boolean {

--
Gitblit v1.10.0