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, initial,
static, static,
dynamic, dynamic,
catchall,
} }
enum SegmentTokenType { enum SegmentTokenType {
static, static,
dynamic, dynamic,
catchall,
} }
interface SegmentToken { interface SegmentToken {
@ -93,15 +95,17 @@ function getRoutePath (tokens: SegmentToken[]): string {
path + path +
(token.type === SegmentTokenType.dynamic (token.type === SegmentTokenType.dynamic
? `:${token.value}` ? `:${token.value}`
: encodePath(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) { function parseSegment (segment: string) {
let state = SegmentParserState.initial let state: SegmentParserState = SegmentParserState.initial
let i = 0 let i = 0
let buffer = '' let buffer = ''
@ -119,7 +123,9 @@ function parseSegment (segment: string) {
type: type:
state === SegmentParserState.static state === SegmentParserState.static
? SegmentTokenType.static ? SegmentTokenType.static
: SegmentTokenType.dynamic, : state === SegmentParserState.dynamic
? SegmentTokenType.dynamic
: SegmentTokenType.catchall,
value: buffer value: buffer
}) })
@ -149,7 +155,12 @@ function parseSegment (segment: string) {
} }
break break
case SegmentParserState.catchall:
case SegmentParserState.dynamic: case SegmentParserState.dynamic:
if (buffer === '...') {
buffer = ''
state = SegmentParserState.catchall
}
if (c === ']') { if (c === ']') {
consumeBuffer() consumeBuffer()
state = SegmentParserState.initial state = SegmentParserState.initial