Skip to content

Commit

Permalink
fix(build): better align server and client side filename sanitization
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Oct 13, 2022
1 parent d17f085 commit 3fd20fe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/client/app/utils.ts
@@ -1,5 +1,5 @@
import { siteDataRef } from './data.js'
import { inBrowser, EXTERNAL_URL_RE } from '../shared.js'
import { inBrowser, EXTERNAL_URL_RE, sanitizeFileName } from '../shared.js'

export { inBrowser }

Expand Down Expand Up @@ -30,6 +30,7 @@ export function pathToFile(path: string): string {
// always force re-fetch content in dev
pagePath += `.md?t=${Date.now()}`
} else {
pagePath = sanitizeFileName(pagePath)
// in production, each .md file is built into a .md.js file following
// the path conversion scheme.
// /foo/bar.html -> ./foo_bar.md
Expand Down
2 changes: 2 additions & 0 deletions src/node/build/bundle.ts
Expand Up @@ -7,6 +7,7 @@ import { slash } from '../utils/slash'
import { SiteConfig } from '../config'
import { APP_PATH } from '../alias'
import { createVitePressPlugin } from '../plugin'
import { sanitizeFileName } from '../shared'
import { buildMPAClient } from './buildMPAClient'

export const okMark = '\x1b[32m✓\x1b[0m'
Expand Down Expand Up @@ -68,6 +69,7 @@ export async function bundle(
// other
preserveEntrySignatures: 'allow-extension',
output: {
sanitizeFileName,
...rollupOptions?.output,
...(ssr
? {
Expand Down
5 changes: 3 additions & 2 deletions src/node/build/render.ts
Expand Up @@ -10,7 +10,8 @@ import {
createTitle,
notFoundPageData,
mergeHead,
EXTERNAL_URL_RE
EXTERNAL_URL_RE,
sanitizeFileName
} from '../shared'
import { slash } from '../utils/slash'
import { SiteConfig, resolveSiteDataByRoute } from '../config'
Expand All @@ -31,7 +32,7 @@ export async function renderPage(
// render page
const content = await render(routePath)

const pageName = page.replace(/\//g, '_')
const pageName = sanitizeFileName(page.replace(/\//g, '_'))
// server build doesn't need hash
const pageServerJsFileName = pageName + '.js'
// for any initial page load, we only need the lean version of the page js
Expand Down
15 changes: 15 additions & 0 deletions src/shared/shared.ts
Expand Up @@ -161,3 +161,18 @@ function hasTag(head: HeadConfig[], tag: HeadConfig) {
export function mergeHead(prev: HeadConfig[], curr: HeadConfig[]) {
return [...prev.filter((tagAttrs) => !hasTag(curr, tagAttrs)), ...curr]
}

// https://github.com/rollup/rollup/blob/fec513270c6ac350072425cc045db367656c623b/src/utils/sanitizeFileName.ts

const INVALID_CHAR_REGEX = /[\u0000-\u001F"#$&*+,:;<=>?[\]^`{|}\u007F]/g
const DRIVE_LETTER_REGEX = /^[a-z]:/i

export function sanitizeFileName(name: string): string {
const match = DRIVE_LETTER_REGEX.exec(name)
const driveLetter = match ? match[0] : ''

return (
driveLetter +
name.slice(driveLetter.length).replace(INVALID_CHAR_REGEX, '_')
)
}

0 comments on commit 3fd20fe

Please sign in to comment.