Skip to content

Commit 7ea3572

Browse files
committed
fix: dont escape ampersand twice in title
1 parent d8c0c0a commit 7ea3572

File tree

7 files changed

+19
-28
lines changed

7 files changed

+19
-28
lines changed

Diff for: package.json

-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@
133133
"@rollup/plugin-replace": "^5.0.5",
134134
"@types/cross-spawn": "^6.0.6",
135135
"@types/debug": "^4.1.12",
136-
"@types/escape-html": "^1.0.4",
137136
"@types/fs-extra": "^11.0.4",
138137
"@types/lodash.template": "^4.5.3",
139138
"@types/mark.js": "^8.11.12",
@@ -150,7 +149,6 @@
150149
"cross-spawn": "^7.0.3",
151150
"debug": "^4.3.4",
152151
"esbuild": "^0.21.3",
153-
"escape-html": "^1.0.3",
154152
"execa": "^9.1.0",
155153
"fast-glob": "^3.3.2",
156154
"fs-extra": "^11.2.0",

Diff for: pnpm-lock.yaml

-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/client/app/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
type AsyncComponentLoader
1414
} from 'vue'
1515

16-
export { inBrowser } from '../shared'
16+
export { inBrowser, escapeHtml as _escapeHtml } from '../shared'
1717

1818
/**
1919
* Join two paths by resolving the slash collision.

Diff for: src/client/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export {
2121
onContentUpdated,
2222
defineClientComponent,
2323
withBase,
24-
getScrollOffset
24+
getScrollOffset,
25+
_escapeHtml
2526
} from './app/utils'
2627

2728
// components

Diff for: src/node/build/render.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { isBooleanAttr } from '@vue/shared'
2-
import escape from 'escape-html'
32
import fs from 'fs-extra'
43
import path from 'path'
54
import { pathToFileURL } from 'url'
65
import { normalizePath, transformWithEsbuild, type Rollup } from 'vite'
6+
import { version } from '../../../package.json'
77
import type { SiteConfig } from '../config'
88
import {
99
EXTERNAL_URL_RE,
1010
createTitle,
11+
escapeHtml,
1112
mergeHead,
1213
notFoundPageData,
1314
resolveSiteDataByRoute,
@@ -17,7 +18,6 @@ import {
1718
type PageData,
1819
type SSGContext
1920
} from '../shared'
20-
import { version } from '../../../package.json'
2121

2222
export async function renderPage(
2323
render: (path: string) => Promise<SSGContext>,
@@ -163,7 +163,7 @@ export async function renderPage(
163163
? ''
164164
: '<meta name="viewport" content="width=device-width,initial-scale=1">'
165165
}
166-
<title>${escape(title)}</title>
166+
<title>${escapeHtml(title)}</title>
167167
${
168168
isDescriptionOverridden(head)
169169
? ''
@@ -260,7 +260,7 @@ function renderAttrs(attrs: Record<string, string>): string {
260260
return Object.keys(attrs)
261261
.map((key) => {
262262
if (isBooleanAttr(key)) return ` ${key}`
263-
return ` ${key}="${escape(attrs[key] as string)}"`
263+
return ` ${key}="${escapeHtml(attrs[key] as string)}"`
264264
})
265265
.join('')
266266
}

Diff for: src/node/markdown/plugins/restoreEntities.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type MarkdownIt from 'markdown-it'
22
import type StateCore from 'markdown-it/lib/rules_core/state_core.mjs'
33
import type Token from 'markdown-it/lib/token.mjs'
4+
import { escapeHtml } from '../../shared'
45

56
export function restoreEntities(md: MarkdownIt): void {
67
md.core.ruler.at('text_join', text_join)
@@ -47,7 +48,3 @@ function getContent(token: Token): string {
4748
? '&amp;'
4849
: token.content
4950
}
50-
51-
function escapeHtml(str: string): string {
52-
return str.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
53-
}

Diff for: src/shared/shared.ts

+11
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,14 @@ export function treatAsHtml(filename: string): boolean {
219219
export function escapeRegExp(str: string) {
220220
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
221221
}
222+
223+
/**
224+
* @internal
225+
*/
226+
export function escapeHtml(str: string): string {
227+
return str
228+
.replace(/</g, '&lt;')
229+
.replace(/>/g, '&gt;')
230+
.replace(/"/g, '&quot;')
231+
.replace(/&(?![\w#]+;)/g, '&amp;')
232+
}

0 commit comments

Comments
 (0)