feat(pages): add catchall support ([...id].vue) (#254)

This commit is contained in:
Daniel Roe 2021-06-21 13:09:08 +01:00 committed by GitHub
parent 01b2c2c972
commit 1518a2a070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -0,0 +1,5 @@
<template>
<div>
test-{{ $route.params.id }}
</div>
</template>

View File

@ -13,11 +13,13 @@ enum SegmentParserState {
initial,
static,
dynamic,
catchall,
}
enum SegmentTokenType {
static,
dynamic,
catchall,
}
interface SegmentToken {
@ -93,15 +95,17 @@ function getRoutePath (tokens: SegmentToken[]): string {
path +
(token.type === SegmentTokenType.dynamic
? `:${token.value}`
: token.type === SegmentTokenType.catchall
? `:${token.value}(.*)*`
: encodePath(token.value))
)
}, '/')
}
const PARAM_CHAR_RE = /[\w\d_]/
const PARAM_CHAR_RE = /[\w\d_.]/
function parseSegment (segment: string) {
let state = SegmentParserState.initial
let state: SegmentParserState = SegmentParserState.initial
let i = 0
let buffer = ''
@ -119,7 +123,9 @@ function parseSegment (segment: string) {
type:
state === SegmentParserState.static
? SegmentTokenType.static
: SegmentTokenType.dynamic,
: state === SegmentParserState.dynamic
? SegmentTokenType.dynamic
: SegmentTokenType.catchall,
value: buffer
})
@ -149,7 +155,12 @@ function parseSegment (segment: string) {
}
break
case SegmentParserState.catchall:
case SegmentParserState.dynamic:
if (buffer === '...') {
buffer = ''
state = SegmentParserState.catchall
}
if (c === ']') {
consumeBuffer()
state = SegmentParserState.initial