Skip to content

Commit

Permalink
fix: escape experimental allowlist paths (#24)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
userquin and brc-dd authored Oct 15, 2023
1 parent 7fd1eee commit f26e0fb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/pwa-prompt/about.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: About
---

<script setup>
const date = __DATE__
</script>
Expand Down
7 changes: 7 additions & 0 deletions examples/pwa-simple/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export default withPwa(defineConfig({
nav: [
{ text: 'Home', link: '/' },
{ text: 'About', link: '/about', activeMatch: '/about' },
{
text: 'Packages',
items: [
{ text: 'Foo', link: '/packages/foo' },
{ text: 'Bar', link: '/packages/bar' },
],
},
],
},
pwa: {
Expand Down
11 changes: 11 additions & 0 deletions examples/pwa-simple/packages/[pkg].md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Package
---

<script setup>
const date = __DATE__
</script>

# Package `{{ $params.pkg }}`

<pre>Generated at: {{ date }}</pre>
8 changes: 8 additions & 0 deletions examples/pwa-simple/packages/[pkg].paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
paths() {
return [
{ params: { pkg: 'foo' } },
{ params: { pkg: 'bar' } },
]
},
}
16 changes: 12 additions & 4 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ export function withUserConfig<T = DefaultTheme.Config>(config: UserConfig<T>) {
if (typeof allowlist !== 'undefined') {
const base = siteConfig.site.base ?? '/'
for (const page of siteConfig.pages) {
if (page === 'index.md')
allowlist.push(new RegExp(`^${base}(.html)?$`))
else
allowlist.push(new RegExp(`^${base}${page.replace(/\.md$/, '(.html)?')}$`))
const regex = page === 'index.md'
? escapeStringRegexp(base)
: escapeStringRegexp(`${base}${page.replace(/\.md$/, '')}`)
allowlist.push(new RegExp(`^${regex}(\\.html)?$`))
}
}
await api.generateSW()
Expand All @@ -124,3 +124,11 @@ export function withUserConfig<T = DefaultTheme.Config>(config: UserConfig<T>) {

return vitePressConfig
}

function escapeStringRegexp(value: string) {
// Escape characters with special meaning either inside or outside character sets.
// 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.
return value
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d')
}

0 comments on commit f26e0fb

Please sign in to comment.