File tree Expand file tree Collapse file tree 4 files changed +30
-5
lines changed Expand file tree Collapse file tree 4 files changed +30
-5
lines changed Original file line number Diff line number Diff line change 1
1
import { siteDataRef } from './data.js'
2
- import { inBrowser , EXTERNAL_URL_RE } from '../shared.js'
2
+ import { inBrowser , EXTERNAL_URL_RE , sanitizeFileName } from '../shared.js'
3
3
4
4
export { inBrowser }
5
5
@@ -36,14 +36,18 @@ export function pathToFile(path: string): string {
36
36
if ( inBrowser ) {
37
37
const base = import . meta. env . BASE_URL
38
38
pagePath =
39
- ( pagePath . slice ( base . length ) . replace ( / \/ / g, '_' ) || 'index' ) + '.md'
39
+ sanitizeFileName (
40
+ pagePath . slice ( base . length ) . replace ( / \/ / g, '_' ) || 'index'
41
+ ) + '.md'
40
42
// client production build needs to account for page hash, which is
41
43
// injected directly in the page's html
42
44
const pageHash = __VP_HASH_MAP__ [ pagePath . toLowerCase ( ) ]
43
45
pagePath = `${ base } assets/${ pagePath } .${ pageHash } .js`
44
46
} else {
45
47
// ssr build uses much simpler name mapping
46
- pagePath = `./${ pagePath . slice ( 1 ) . replace ( / \/ / g, '_' ) } .md.js`
48
+ pagePath = `./${ sanitizeFileName (
49
+ pagePath . slice ( 1 ) . replace ( / \/ / g, '_' )
50
+ ) } .md.js`
47
51
}
48
52
}
49
53
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { slash } from '../utils/slash'
7
7
import { SiteConfig } from '../config'
8
8
import { APP_PATH } from '../alias'
9
9
import { createVitePressPlugin } from '../plugin'
10
+ import { sanitizeFileName } from '../shared'
10
11
import { buildMPAClient } from './buildMPAClient'
11
12
12
13
export const okMark = '\x1b[32m✓\x1b[0m'
@@ -68,6 +69,7 @@ export async function bundle(
68
69
// other
69
70
preserveEntrySignatures : 'allow-extension' ,
70
71
output : {
72
+ sanitizeFileName,
71
73
...rollupOptions ?. output ,
72
74
...( ssr
73
75
? {
Original file line number Diff line number Diff line change @@ -10,7 +10,8 @@ import {
10
10
createTitle ,
11
11
notFoundPageData ,
12
12
mergeHead ,
13
- EXTERNAL_URL_RE
13
+ EXTERNAL_URL_RE ,
14
+ sanitizeFileName
14
15
} from '../shared'
15
16
import { slash } from '../utils/slash'
16
17
import { SiteConfig , resolveSiteDataByRoute } from '../config'
@@ -31,7 +32,7 @@ export async function renderPage(
31
32
// render page
32
33
const content = await render ( routePath )
33
34
34
- const pageName = page . replace ( / \/ / g, '_' )
35
+ const pageName = sanitizeFileName ( page . replace ( / \/ / g, '_' ) )
35
36
// server build doesn't need hash
36
37
const pageServerJsFileName = pageName + '.js'
37
38
// for any initial page load, we only need the lean version of the page js
Original file line number Diff line number Diff line change @@ -161,3 +161,21 @@ function hasTag(head: HeadConfig[], tag: HeadConfig) {
161
161
export function mergeHead ( prev : HeadConfig [ ] , curr : HeadConfig [ ] ) {
162
162
return [ ...prev . filter ( ( tagAttrs ) => ! hasTag ( curr , tagAttrs ) ) , ...curr ]
163
163
}
164
+
165
+ // https://github.com/rollup/rollup/blob/fec513270c6ac350072425cc045db367656c623b/src/utils/sanitizeFileName.ts
166
+
167
+ const INVALID_CHAR_REGEX = / [ \u0000 - \u001F " # $ & * + , : ; < = > ? [ \] ^ ` { | } \u007F ] / g
168
+ const DRIVE_LETTER_REGEX = / ^ [ a - z ] : / i
169
+
170
+ export function sanitizeFileName ( name : string ) : string {
171
+ const match = DRIVE_LETTER_REGEX . exec ( name )
172
+ const driveLetter = match ? match [ 0 ] : ''
173
+
174
+ return (
175
+ driveLetter +
176
+ name
177
+ . slice ( driveLetter . length )
178
+ . replace ( INVALID_CHAR_REGEX , '_' )
179
+ . replace ( / ( ^ | \/ ) _ + (? = [ ^ / ] * $ ) / , '$1' )
180
+ )
181
+ }
You can’t perform that action at this time.
0 commit comments