Skip to content

Commit f26e0fb

Browse files
userquinbrc-dd
andauthored
fix: escape experimental allowlist paths (#24)
* fix: escape experimental allowlist paths * chore: add dynamic routes to examples * chore: show 'generated at' in dynamic routes too * chore: use ts extension for consistency --------- Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 7fd1eee commit f26e0fb

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

examples/pwa-prompt/about.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: About
33
---
4+
45
<script setup>
56
const date = __DATE__
67
</script>

examples/pwa-simple/.vitepress/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export default withPwa(defineConfig({
3232
nav: [
3333
{ text: 'Home', link: '/' },
3434
{ text: 'About', link: '/about', activeMatch: '/about' },
35+
{
36+
text: 'Packages',
37+
items: [
38+
{ text: 'Foo', link: '/packages/foo' },
39+
{ text: 'Bar', link: '/packages/bar' },
40+
],
41+
},
3542
],
3643
},
3744
pwa: {

examples/pwa-simple/packages/[pkg].md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: Package
3+
---
4+
5+
<script setup>
6+
const date = __DATE__
7+
</script>
8+
9+
# Package `{{ $params.pkg }}`
10+
11+
<pre>Generated at: {{ date }}</pre>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
paths() {
3+
return [
4+
{ params: { pkg: 'foo' } },
5+
{ params: { pkg: 'bar' } },
6+
]
7+
},
8+
}

src/integration.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ export function withUserConfig<T = DefaultTheme.Config>(config: UserConfig<T>) {
112112
if (typeof allowlist !== 'undefined') {
113113
const base = siteConfig.site.base ?? '/'
114114
for (const page of siteConfig.pages) {
115-
if (page === 'index.md')
116-
allowlist.push(new RegExp(`^${base}(.html)?$`))
117-
else
118-
allowlist.push(new RegExp(`^${base}${page.replace(/\.md$/, '(.html)?')}$`))
115+
const regex = page === 'index.md'
116+
? escapeStringRegexp(base)
117+
: escapeStringRegexp(`${base}${page.replace(/\.md$/, '')}`)
118+
allowlist.push(new RegExp(`^${regex}(\\.html)?$`))
119119
}
120120
}
121121
await api.generateSW()
@@ -124,3 +124,11 @@ export function withUserConfig<T = DefaultTheme.Config>(config: UserConfig<T>) {
124124

125125
return vitePressConfig
126126
}
127+
128+
function escapeStringRegexp(value: string) {
129+
// Escape characters with special meaning either inside or outside character sets.
130+
// Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
131+
return value
132+
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
133+
.replace(/-/g, '\\x2d')
134+
}

0 commit comments

Comments
 (0)