From 2f6747b1666316e579c6e7238092ac6a65d00925 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Thu, 17 Aug 2023 05:04:15 +0000
Subject: [PATCH] fix relative path resolution in router and link crawling

---
 quartz/util/path.ts |   17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/quartz/path.ts b/quartz/util/path.ts
similarity index 96%
rename from quartz/path.ts
rename to quartz/util/path.ts
index 8588c21..c1016af 100644
--- a/quartz/path.ts
+++ b/quartz/util/path.ts
@@ -71,7 +71,7 @@
 export type RelativeURL = SlugLike<"relative">
 export function isRelativeURL(s: string): s is RelativeURL {
   const validStart = /^\.{1,2}/.test(s)
-  const validEnding = !(s.endsWith("/") || s.endsWith("/index") || s === "index")
+  const validEnding = !(s.endsWith("/index") || s === "index")
   return validStart && validEnding && !_hasFileExtension(s)
 }
 
@@ -133,6 +133,12 @@
 
 export function transformInternalLink(link: string): RelativeURL {
   let [fplike, anchor] = splitAnchor(decodeURI(link))
+
+  const folderPath =
+    fplike.endsWith("index") ||
+    fplike.endsWith("index.md") ||
+    fplike.endsWith("index.html") ||
+    fplike.endsWith("/")
   let segments = fplike.split("/").filter((x) => x.length > 0)
   let prefix = segments.filter(_isRelativeSegment).join("/")
   let fp = segments.filter((seg) => !_isRelativeSegment(seg)).join("/")
@@ -143,14 +149,13 @@
   }
 
   fp = canonicalizeServer(slugifyFilePath(fp as FilePath))
-  fp = _trimSuffix(fp, "index")
-
-  let joined = joinSegments(_stripSlashes(prefix), _stripSlashes(fp))
-  const res = (_addRelativeToStart(joined) + anchor) as RelativeURL
+  const joined = joinSegments(_stripSlashes(prefix), _stripSlashes(fp))
+  const trail = folderPath ? "/" : ""
+  const res = (_addRelativeToStart(joined) + anchor + trail) as RelativeURL
   return res
 }
 
-// resolve /a/b/c to ../../
+// resolve /a/b/c to ../../..
 export function pathToRoot(slug: CanonicalSlug): RelativeURL {
   let rootPath = slug
     .split("/")

--
Gitblit v1.10.0