From 9466c145b1b84299be5f865596b3ebdac248b9fa Mon Sep 17 00:00:00 2001
From: moexiami <1927254+Xiami2012@users.noreply.github.com>
Date: Wed, 01 Jan 2025 18:07:59 +0000
Subject: [PATCH] fix(path): correct handle URI schemes in joinSegments (#1693)

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

diff --git a/quartz/util/path.ts b/quartz/util/path.ts
index dceb89b..d2409e1 100644
--- a/quartz/util/path.ts
+++ b/quartz/util/path.ts
@@ -108,10 +108,10 @@
   el.setAttribute(attr, rebased.pathname + rebased.hash)
 }
 export function normalizeRelativeURLs(el: Element | Document, destination: string | URL) {
-  el.querySelectorAll('[href^="./"], [href^="../"]').forEach((item) =>
+  el.querySelectorAll('[href=""], [href^="./"], [href^="../"]').forEach((item) =>
     _rebaseHtmlElement(item, "href", destination),
   )
-  el.querySelectorAll('[src^="./"], [src^="../"]').forEach((item) =>
+  el.querySelectorAll('[src=""], [src^="./"], [src^="../"]').forEach((item) =>
     _rebaseHtmlElement(item, "src", destination),
   )
 }
@@ -168,6 +168,9 @@
 
 export function splitAnchor(link: string): [string, string] {
   let [fp, anchor] = link.split("#", 2)
+  if (fp.endsWith(".pdf")) {
+    return [fp, anchor === undefined ? "" : `#${anchor}`]
+  }
   anchor = anchor === undefined ? "" : "#" + slugAnchor(anchor)
   return [fp, anchor]
 }
@@ -182,8 +185,13 @@
 export function joinSegments(...args: string[]): string {
   return args
     .filter((segment) => segment !== "")
+    .map((segment, index) =>
+      index === 0
+        ? // Deduplicate but not remove leading slashes for first segment
+          segment.replace(/\/+$/g, "").replace(/^\/\/+/g, "/")
+        : segment.replace(/^\/+|\/+$/g, ""),
+    )
     .join("/")
-    .replace(/\/\/+/g, "/")
 }
 
 export function getAllSegmentPrefixes(tags: string): string[] {

--
Gitblit v1.10.0