Skip to content

Commit

Permalink
fix: Filesystem Routing bug when common prefix ends in filename
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Mar 26, 2021
1 parent 9aed531 commit 06ccfaa
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/route.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,29 @@ function getFilesystemRoute(pageId: string, allPageIds: string[]): string {
}
function removeCommonPrefix(pageId: PageId, allPageIds: PageId[]) {
const relevantPageIds = allPageIds.filter((pageId) => !isErrorPage(pageId))
const commonPrefix = getCommonPrefix(relevantPageIds)
const commonPrefix = getCommonPath(relevantPageIds)
assert(pageId.startsWith(commonPrefix))
return pageId.slice(commonPrefix.length)
}
function getCommonPrefix(strings: string[]): string {
const list = strings.concat().sort()
const first = list[0]
const last = list[list.length - 1]
function getCommonPath(pageIds: string[]): string {
pageIds.forEach((pageId) => {
assertUsage(
!pageId.includes('\\'),
'Your directory names and file names are not allowed to contain the character `\\`'
)
})
const pageIdList = pageIds.concat().sort()
const first = pageIdList[0]
const last = pageIdList[pageIdList.length - 1]
let idx = 0
for (; idx < first.length; idx++) {
if (first[idx] !== last[idx]) break
}
return first.slice(0, idx)
const commonPrefix = first.slice(0, idx)
const pathsPart = commonPrefix.split('/')
assert(pathsPart.length >= 2)
const commonPath = slice(pathsPart, 0, -1).join('/') + '/'
return commonPath
}

/**
Expand Down

0 comments on commit 06ccfaa

Please sign in to comment.