|
|
@ -6,31 +6,31 @@ export function resolveRewrites(
|
|
|
|
pages: string[],
|
|
|
|
pages: string[],
|
|
|
|
userRewrites: UserConfig['rewrites']
|
|
|
|
userRewrites: UserConfig['rewrites']
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
const rewriteEntries = Object.entries(userRewrites || {})
|
|
|
|
const rewriteRules = Object.entries(userRewrites || {}).map(([from, to]) => ({
|
|
|
|
const rewrites = rewriteEntries.length
|
|
|
|
toPath: compile(to),
|
|
|
|
? Object.fromEntries(
|
|
|
|
matchUrl: match(from)
|
|
|
|
pages
|
|
|
|
}))
|
|
|
|
.map((src) => {
|
|
|
|
|
|
|
|
for (const [from, to] of rewriteEntries) {
|
|
|
|
const pageToRewrite: Record<string, string> = {}
|
|
|
|
const dest = rewrite(src, from, to)
|
|
|
|
const rewriteToPage: Record<string, string> = {}
|
|
|
|
if (dest) return [src, dest]
|
|
|
|
if (rewriteRules.length) {
|
|
|
|
|
|
|
|
for (const page of pages) {
|
|
|
|
|
|
|
|
for (const { matchUrl, toPath } of rewriteRules) {
|
|
|
|
|
|
|
|
const res = matchUrl(page)
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
|
|
|
|
const dest = toPath(res.params)
|
|
|
|
|
|
|
|
pageToRewrite[page] = dest
|
|
|
|
|
|
|
|
rewriteToPage[dest] = page
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
.filter((e) => e != null) as [string, string][]
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
: {}
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
map: rewrites,
|
|
|
|
|
|
|
|
inv: Object.fromEntries(Object.entries(rewrites).map((a) => a.reverse()))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function rewrite(src: string, from: string, to: string) {
|
|
|
|
return {
|
|
|
|
const urlMatch = match(from)
|
|
|
|
map: pageToRewrite,
|
|
|
|
const res = urlMatch(src)
|
|
|
|
inv: rewriteToPage
|
|
|
|
if (!res) return false
|
|
|
|
}
|
|
|
|
const toPath = compile(to)
|
|
|
|
|
|
|
|
return toPath(res.params)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const rewritesPlugin = (config: SiteConfig): Plugin => {
|
|
|
|
export const rewritesPlugin = (config: SiteConfig): Plugin => {
|
|
|
|