rendering, link resolution, asset copying

This commit is contained in:
Jacky Zhao
2023-05-31 17:01:23 -04:00
parent ad6ce0d73f
commit 21c007e2fc
19 changed files with 564 additions and 274 deletions

View File

@ -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 @@ export function resolveToRoot(slug: string): string {
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"