rendering, link resolution, asset copying
This commit is contained in:
		@@ -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"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user