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 |  228 +++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 159 insertions(+), 69 deletions(-)

diff --git a/quartz/path.ts b/quartz/path.ts
index 8ea28c4..8588c21 100644
--- a/quartz/path.ts
+++ b/quartz/path.ts
@@ -1,5 +1,5 @@
-import path from 'path'
-import { slug as slugAnchor } from 'github-slugger'
+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
@@ -15,68 +15,70 @@
 //                    │                        │                                     │
 //                    │        getClientSlug() │                               .href │
 //                    │                        ▼                                     ▼
-//                    │                                                   
-//                    │                  Client Slug                           Relative URL
-// getCanonicalSlug() │     https://test.ca/note/abc#anchor?query=123          ../note/def#anchor
 //                    │
-//                    │   canonicalizeClient() │                                     ▲
-//                    │                        ▼                                     │
-//                    │                                                              │
-//                    └───────────────►  Canonical Slug                              │
-//                                         /note/abc                                 │
-//                                                                                   │
-//                                             ▲                                     │
+//                    │                  Client Slug                    ┌───►  Relative URL
+// getCanonicalSlug() │     https://test.ca/note/abc#anchor?query=123   │      ../note/def#anchor
+//                    │                                                 │
+//                    │   canonicalizeClient() │                        │      ▲     ▲
+//                    │                        ▼                        │      │     │
+//                    │                                  pathToRoot()   │      │     │
+//                    └───────────────►  Canonical Slug ────────────────┘      │     │
+//                                          note/abc                           │     │
+//                                                   ──────────────────────────┘     │
+//                                             ▲             resolveRelative()       │
 //                        canonicalizeServer() │                                     │
 //                                                                                   │
 // HTML File                               Server Slug                               │
-// /note/abc/index.html  ◄─────────────  /note/abc/index                             │
+//  note/abc/index.html  ◄─────────────   note/abc/index                             │
 //                                                                                   │
 //                                             ▲                            ┌────────┴────────┐
-//                           slugifyFilePath() │    transformInternalLink() │                 │
+//                           slugifyFilePath() │            transformLink() │                 │
 //                                             │                            │                 │
 //                                   ┌─────────┴──────────┐           ┌─────┴─────┐  ┌────────┴──────┐
 //                                   │     File Path      │           │ Wikilinks │  │ Markdown Link │
-//                                   │ /note/abc/index.md │           └───────────┘  └───────────────┘
+//                                   │  note/abc/index.md │           └───────────┘  └───────────────┘
 //                                   └────────────────────┘                 ▲                 ▲
 //                                             ▲                            │                 │
 //                                             │            ┌─────────┐     │                 │
 //                                             └────────────┤ MD File ├─────┴─────────────────┘
 //                                                          └─────────┘
 
+export const QUARTZ = "quartz"
+
 /// Utility type to simulate nominal types in TypeScript
 type SlugLike<T> = string & { __brand: T }
 
 /** Client-side slug, usually obtained through `window.location` */
 export type ClientSlug = SlugLike<"client">
 export function isClientSlug(s: string): s is ClientSlug {
-  return /^https?:\/\/.+/.test(s)
+  const res = /^https?:\/\/.+/.test(s)
+  return res
 }
 
 /** Canonical slug, should be used whenever you need to refer to the location of a file/note.
-  * On the client, this is normally stored in `document.body.dataset.slug`
-  */
+ * On the client, this is normally stored in `document.body.dataset.slug`
+ */
 export type CanonicalSlug = SlugLike<"canonical">
 export function isCanonicalSlug(s: string): s is CanonicalSlug {
-  const validStart = s.startsWith("/")
-  const validEnding = s.length === 1 || (!s.endsWith("/") && !s.endsWith("/index"))
-  return !_containsForbiddenCharacters(s) && validStart && validEnding && !_hasFileExtension(s)
+  const validStart = !(s.startsWith(".") || s.startsWith("/"))
+  const validEnding = !(s.endsWith("/") || s.endsWith("/index") || s === "index")
+  return validStart && !_containsForbiddenCharacters(s) && validEnding && !_hasFileExtension(s)
 }
 
 /** A relative link, can be found on `href`s but can also be constructed for
-  * client-side navigation (e.g. search and graph)
-  */
+ * client-side navigation (e.g. search and graph)
+ */
 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")
+  const validEnding = !(s.endsWith("/") || s.endsWith("/index") || s === "index")
   return validStart && validEnding && !_hasFileExtension(s)
 }
 
 /** A server side slug. This is what Quartz uses to emit files so uses index suffixes */
 export type ServerSlug = SlugLike<"server">
 export function isServerSlug(s: string): s is ServerSlug {
-  // must start with forward slash
-  const validStart = s.startsWith("/")
+  const validStart = !(s.startsWith(".") || s.startsWith("/"))
   const validEnding = !s.endsWith("/")
   return validStart && validEnding && !_containsForbiddenCharacters(s) && !_hasFileExtension(s)
 }
@@ -84,85 +86,169 @@
 /** The real file path to a file on disk */
 export type FilePath = SlugLike<"filepath">
 export function isFilePath(s: string): s is FilePath {
-  return s.startsWith("/") && _hasFileExtension(s)
+  const validStart = !s.startsWith(".")
+  return validStart && _hasFileExtension(s)
 }
 
 export function getClientSlug(window: Window): ClientSlug {
-  return window.location.href as ClientSlug
+  const res = window.location.href as ClientSlug
+  return res
 }
 
 export function getCanonicalSlug(window: Window): CanonicalSlug {
-  return window.document.body.dataset.slug! as CanonicalSlug
+  const res = window.document.body.dataset.slug! as CanonicalSlug
+  return res
 }
 
 export function canonicalizeClient(slug: ClientSlug): CanonicalSlug {
   const { pathname } = new URL(slug)
-  let fp = pathname
-  fp = fp.replace(new RegExp(path.extname(fp) + '$'), '')
-  return _canonicalize(fp) as CanonicalSlug
+  let fp = pathname.slice(1)
+  fp = fp.replace(new RegExp(_getFileExtension(fp) + "$"), "")
+  const res = _canonicalize(fp) as CanonicalSlug
+  return res
 }
 
 export function canonicalizeServer(slug: ServerSlug): CanonicalSlug {
   let fp = slug as string
-  return _canonicalize(fp) as CanonicalSlug
+  const res = _canonicalize(fp) as CanonicalSlug
+  return res
 }
 
 export function slugifyFilePath(fp: FilePath): ServerSlug {
-  // strip file extension
-  const withoutFileExt = fp.replace(new RegExp(path.extname(fp) + '$'), '')
-  const slug = withoutFileExt
-    .split(path.sep) // fs can have diff interpretations of /
-    .map((segment) => segment.replace(/\s/g, '-')) // slugify all segments
-    .join('/') // always use / as sep
-    .replace(/\/$/, '') // remove trailing slash
+  fp = _stripSlashes(fp) as FilePath
+  const withoutFileExt = fp.replace(new RegExp(_getFileExtension(fp) + "$"), "")
+  let slug = withoutFileExt
+    .split("/")
+    .map((segment) => segment.replace(/\s/g, "-")) // slugify all segments
+    .join("/") // always use / as sep
+    .replace(/\/$/, "") // remove trailing slash
+
+  // treat _index as index
+  if (_endsWith(slug, "_index")) {
+    slug = slug.replace(/_index$/, "index")
+  }
 
   return slug as ServerSlug
 }
 
 export function transformInternalLink(link: string): RelativeURL {
-  let [fplike, anchor] = link.split("#", 2)
-  let segments = fplike.split("/").filter(x => x.length > 0)
+  let [fplike, anchor] = splitAnchor(decodeURI(link))
+  let segments = fplike.split("/").filter((x) => x.length > 0)
   let prefix = segments.filter(_isRelativeSegment).join("/")
-  let fp = "/" + segments.filter(seg => !_isRelativeSegment(seg)).join("/")
-  fp = canonicalizeServer(slugifyFilePath(fp as FilePath))
+  let fp = segments.filter((seg) => !_isRelativeSegment(seg)).join("/")
 
-  if (fp.endsWith("index")) {
-    fp = fp.slice(0, -"index".length)
+  // implicit markdown
+  if (!_hasFileExtension(fp)) {
+    fp += ".md"
   }
 
-  let joined = [_stripSlashes(prefix), _stripSlashes(fp)].filter(x => x !== "").join("/")
-  anchor = anchor === undefined ? "" : '#' + slugAnchor(anchor)
-  return _addRelativeToStart(joined) + anchor as RelativeURL
+  fp = canonicalizeServer(slugifyFilePath(fp as FilePath))
+  fp = _trimSuffix(fp, "index")
+
+  let joined = joinSegments(_stripSlashes(prefix), _stripSlashes(fp))
+  const res = (_addRelativeToStart(joined) + anchor) as RelativeURL
+  return res
 }
 
 // resolve /a/b/c to ../../
 export function pathToRoot(slug: CanonicalSlug): RelativeURL {
   let rootPath = slug
-    .split('/')
-    .filter(x => x !== '')
-    .map(_ => '..')
-    .join('/')
+    .split("/")
+    .filter((x) => x !== "")
+    .map((_) => "..")
+    .join("/")
 
-  return _addRelativeToStart(rootPath) as RelativeURL
+  const res = _addRelativeToStart(rootPath) as RelativeURL
+  return res
 }
 
-export const QUARTZ = "quartz"
+export function resolveRelative(current: CanonicalSlug, target: CanonicalSlug): RelativeURL {
+  const res = joinSegments(pathToRoot(current), target) as RelativeURL
+  return res
+}
+
+export function splitAnchor(link: string): [string, string] {
+  let [fp, anchor] = link.split("#", 2)
+  anchor = anchor === undefined ? "" : "#" + slugAnchor(anchor)
+  return [fp, anchor]
+}
+
+export function slugAnchor(anchor: string) {
+  return slug(anchor)
+}
+
+export function slugTag(tag: string) {
+  return tag
+    .split("/")
+    .map((tagSegment) => slug(tagSegment))
+    .join("/")
+}
+
+export function joinSegments(...args: string[]): string {
+  return args.filter((segment) => segment !== "").join("/")
+}
+
+export function getAllSegmentPrefixes(tags: string): string[] {
+  const segments = tags.split("/")
+  const results: string[] = []
+  for (let i = 0; i < segments.length; i++) {
+    results.push(segments.slice(0, i + 1).join("/"))
+  }
+  return results
+}
+
+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 {
-  if (fp.endsWith("index")) {
-    fp = fp.slice(0, -"index".length)
-  }
+  fp = _trimSuffix(fp, "index")
+  return _stripSlashes(fp)
+}
 
-  // remove trailing slash
-  if (fp.endsWith("/")) {
-    fp = fp.slice(0, -1)
-  }
+function _endsWith(s: string, suffix: string): boolean {
+  return s === suffix || s.endsWith("/" + suffix)
+}
 
-  if (fp.length === 0) {
-    return "/" as CanonicalSlug
+function _trimSuffix(s: string, suffix: string): string {
+  if (_endsWith(s, suffix)) {
+    s = s.slice(0, -suffix.length)
   }
-
-  return fp
+  return s
 }
 
 function _containsForbiddenCharacters(s: string): boolean {
@@ -170,14 +256,18 @@
 }
 
 function _hasFileExtension(s: string): boolean {
-  return /\.[A-Za-z]+$/.test(s)
+  return _getFileExtension(s) !== undefined
+}
+
+function _getFileExtension(s: string): string | undefined {
+  return s.match(/\.[A-Za-z0-9]+$/)?.[0]
 }
 
 function _isRelativeSegment(s: string): boolean {
   return /^\.{0,2}$/.test(s)
 }
 
-function _stripSlashes(s: string): string {
+export function _stripSlashes(s: string): string {
   if (s.startsWith("/")) {
     s = s.substring(1)
   }
@@ -195,7 +285,7 @@
   }
 
   if (!s.startsWith(".")) {
-    s = "./" + s
+    s = joinSegments(".", s)
   }
 
   return s

--
Gitblit v1.10.0