Skip to content

Commit

Permalink
fix: encode urls that conflict w/ vite built-in replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 28, 2021
1 parent 508e9eb commit 3940625
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/node/markdown/plugins/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export const linkPlugin = (
) {
normalizeHref(hrefAttr)
}

// encode vite-specific replace strings in case they appear in URLs
// this also excludes them from build-time replacements (which injects
// <wbr/> and will break URLs)
hrefAttr[1] = hrefAttr[1]
.replace(/\bimport\.meta/g, 'import%2Emeta')
.replace(/\bprocess\.env/g, 'process%2Eenv')
}
return self.renderToken(tokens, idx, options)
}
Expand Down
20 changes: 13 additions & 7 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export function createMarkdownToVueRenderFn(
const md = createMarkdownRenderer(srcDir, options)
pages = pages.map((p) => slash(p.replace(/\.md$/, '')))

const userDefineRegex = userDefines
? new RegExp(
`\\b(${Object.keys(userDefines)
.map((key) => key.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'))
.join('|')})`,
'g'
)
: null

return (
src: string,
file: string,
Expand Down Expand Up @@ -66,14 +75,11 @@ export function createMarkdownToVueRenderFn(
.replace(/\bprocess\.env/g, 'process.<wbr/>env')

// also avoid replacing vite user defines
if (userDefines) {
const regex = new RegExp(
`\\b(${Object.keys(userDefines)
.map((key) => key.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'))
.join('|')})`,
'g'
if (userDefineRegex) {
html = html.replace(
userDefineRegex,
(_) => `${_[0]}<wbr/>${_.slice(1)}`
)
html = html.replace(regex, (_) => `${_[0]}<wbr/>${_.slice(1)}`)
}
}

Expand Down

0 comments on commit 3940625

Please sign in to comment.