Jacky Zhao
2023-06-07 dde36fa5589a362b60b7b72eb7793a3f133e159c
quartz/path.ts
@@ -1,4 +1,7 @@
import path from 'path'
import SlugAnchor from 'github-slugger'
const slugAnchor = new SlugAnchor()
function slugSegment(s: string): string {
  return s.replace(/\s/g, '-')
@@ -6,13 +9,12 @@
export function slugify(s: string): string {
  const [fp, anchor] = s.split("#", 2)
  const sluggedAnchor = anchor === undefined ? "" : "#" + slugSegment(anchor)
  const sluggedAnchor = anchor === undefined ? "" : "#" + slugAnchor.slug(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
}
@@ -20,11 +22,15 @@
// resolve /a/b/c to ../../
export function resolveToRoot(slug: string): string {
  let fp = slug
  if (fp.endsWith("/index")) {
    fp = fp.slice(0, -"/index".length)
  if (fp.endsWith("index")) {
    fp = fp.slice(0, -"index".length)
  }
  return fp
  if (fp === "") {
    return "."
  }
  return "./" + fp
    .split('/')
    .filter(x => x !== '')
    .map(_ => '..')