Jacky Zhao
2023-05-30 ad6ce0d73fbd015e00e59ec30bda3cc8de777832
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
}
 
// resolve /a/b/c to ../../
export function resolveToRoot(slug: string): string {
  let fp = slug
  if (fp.endsWith("/index")) {
    fp = fp.slice(0, -"/index".length)
  }
 
  return "./" + path.relative(fp, path.posix.sep)
}