From 21c007e2fcf73fe4ef04dd07db7116afed46047a Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Wed, 31 May 2023 21:01:23 +0000
Subject: [PATCH] rendering, link resolution, asset copying

---
 quartz/path.ts |   37 ++++++++++++++++++++++++++++++-------
 1 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/quartz/path.ts b/quartz/path.ts
index ddad387..8967d79 100644
--- a/quartz/path.ts
+++ b/quartz/path.ts
@@ -1,11 +1,20 @@
 import path from 'path'
 
-// Replaces all whitespace with dashes and URI encodes the rest
-export function pathToSlug(fp: string): string {
-  const { dir, name } = path.parse(fp)
-  let slug = path.join('/', dir, name)
-  slug = slug.replace(/\s/g, '-')
-  return slug
+function slugSegment(s: string): string {
+  return s.replace(/\s/g, '-')
+}
+
+export function slugify(s: string): string {
+  const [fp, anchor] = s.split("#", 2)
+  const sluggedAnchor = anchor === undefined ? "" : "#" + slugSegment(anchor)
+  const withoutFileExt = fp.replace(new RegExp(path.extname(fp) + '$'), '')
+  const rawSlugSegments = withoutFileExt.split(path.sep)
+  const slugParts: string = rawSlugSegments
+    .map((segment) => slugSegment(segment))
+    .join(path.posix.sep)
+    // .replace(/index$/, '')
+    .replace(/\/$/, '')
+  return path.normalize(slugParts) + sluggedAnchor
 }
 
 // resolve /a/b/c to ../../
@@ -15,5 +24,19 @@
     fp = fp.slice(0, -"/index".length)
   }
 
-  return "./" + path.relative(fp, path.posix.sep)
+  return fp
+    .split('/')
+    .filter(x => x !== '')
+    .map(_ => '..')
+    .join('/')
 }
+
+export function relativeToRoot(slug: string, fp: string): string {
+  return path.join(resolveToRoot(slug), fp)
+}
+
+export function relative(src: string, dest: string): string {
+  return path.relative(src, dest)
+}
+
+export const QUARTZ = "quartz"

--
Gitblit v1.10.0