Jacky Zhao
2023-05-31 21c007e2fcf73fe4ef04dd07db7116afed46047a
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"