treat _index as index

This commit is contained in:
Jacky Zhao 2023-07-15 23:33:06 -07:00
parent 361c5c9ffb
commit 0c5ca34c4b
3 changed files with 20 additions and 10 deletions

View File

@ -1,6 +1,5 @@
- fixes - fixes
- changing `_index` files
- typography - typography
- CLI - CLI
- update - update

View File

@ -122,6 +122,7 @@ describe('transforms', () => {
describe('slugifyFilePath', () => { describe('slugifyFilePath', () => {
asserts([ asserts([
["content/index.md", "content/index"], ["content/index.md", "content/index"],
["content/_index.md", "content/index"],
["/content/index.md", "content/index"], ["/content/index.md", "content/index"],
["content/cool.png", "content/cool"], ["content/cool.png", "content/cool"],
["index.md", "index"], ["index.md", "index"],

View File

@ -134,12 +134,17 @@ export function slugifyFilePath(fp: FilePath): ServerSlug {
conditionCheck(slugifyFilePath.name, 'pre', fp, isFilePath) conditionCheck(slugifyFilePath.name, 'pre', fp, isFilePath)
fp = _stripSlashes(fp) as FilePath fp = _stripSlashes(fp) as FilePath
const withoutFileExt = fp.replace(new RegExp(_getFileExtension(fp) + '$'), '') const withoutFileExt = fp.replace(new RegExp(_getFileExtension(fp) + '$'), '')
const slug = withoutFileExt let slug = withoutFileExt
.split('/') .split('/')
.map((segment) => segment.replace(/\s/g, '-')) // slugify all segments .map((segment) => segment.replace(/\s/g, '-')) // slugify all segments
.join('/') // always use / as sep .join('/') // always use / as sep
.replace(/\/$/, '') // remove trailing slash .replace(/\/$/, '') // remove trailing slash
// treat _index as index
if (_endsWith(slug, "_index")) {
slug = slug.replace(/_index$/, "index")
}
conditionCheck(slugifyFilePath.name, 'post', slug, isServerSlug) conditionCheck(slugifyFilePath.name, 'post', slug, isServerSlug)
return slug as ServerSlug return slug as ServerSlug
} }
@ -156,10 +161,7 @@ export function transformInternalLink(link: string): RelativeURL {
} }
fp = canonicalizeServer(slugifyFilePath(fp as FilePath)) fp = canonicalizeServer(slugifyFilePath(fp as FilePath))
fp = _trimSuffix(fp, "index")
if (fp.endsWith("index")) {
fp = fp.slice(0, -"index".length)
}
let joined = joinSegments(_stripSlashes(prefix), _stripSlashes(fp)) let joined = joinSegments(_stripSlashes(prefix), _stripSlashes(fp))
const res = _addRelativeToStart(joined) + anchor as RelativeURL const res = _addRelativeToStart(joined) + anchor as RelativeURL
@ -202,13 +204,21 @@ export function joinSegments(...args: string[]): string {
export const QUARTZ = "quartz" export const QUARTZ = "quartz"
function _canonicalize(fp: string): string { function _canonicalize(fp: string): string {
if (fp.endsWith("index")) { fp = _trimSuffix(fp, "index")
fp = fp.slice(0, -"index".length)
}
return _stripSlashes(fp) return _stripSlashes(fp)
} }
function _endsWith(s: string, suffix: string): boolean {
return s === suffix || s.endsWith("/" + suffix)
}
function _trimSuffix(s: string, suffix: string): string {
if (_endsWith(s, suffix)) {
s = s.slice(0, -(suffix.length))
}
return s
}
function _containsForbiddenCharacters(s: string): boolean { function _containsForbiddenCharacters(s: string): boolean {
return s.includes(" ") || s.includes("#") || s.includes("?") return s.includes(" ") || s.includes("#") || s.includes("?")
} }