Jacky Zhao
2023-07-02 e0ebee5aa9b3646de722f139f1d8d15591df538e
quartz/path.ts
@@ -1,19 +1,61 @@
import path from 'path'
import { slug as slugAnchor } from 'github-slugger'
// 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, '-')
}
// on the client, 'index' isn't ever rendered so we should clean it up
export function clientSideSlug(fp: string): string {
  if (fp.endsWith("index")) {
    fp = fp.slice(0, -"index".length)
  }
  return fp
}
export function trimPathSuffix(fp: string): string {
  fp = clientSideSlug(fp)
  let [cleanPath, anchor] = fp.split("#", 2)
  anchor = anchor === undefined ? "" : "#" + anchor
  return cleanPath + anchor
}
export function slugify(s: string): string {
  const [fp, anchor] = s.split("#", 2)
  const sluggedAnchor = anchor === undefined ? "" : "#" + slugAnchor(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(/\/$/, '')
  return path.normalize(slugParts) + sluggedAnchor
}
// resolve /a/b/c to ../../
export function resolveToRoot(slug: string): string {
  let fp = slug
  if (fp.endsWith("/index")) {
    fp = fp.slice(0, -"/index".length)
  let fp = trimPathSuffix(slug)
  if (fp === "") {
    return "."
  }
  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"