Skip to content

Commit

Permalink
fix: dont escape ampersand twice in title
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed May 21, 2024
1 parent d8c0c0a commit 7ea3572
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 28 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@
"@rollup/plugin-replace": "^5.0.5",
"@types/cross-spawn": "^6.0.6",
"@types/debug": "^4.1.12",
"@types/escape-html": "^1.0.4",
"@types/fs-extra": "^11.0.4",
"@types/lodash.template": "^4.5.3",
"@types/mark.js": "^8.11.12",
Expand All @@ -150,7 +149,6 @@
"cross-spawn": "^7.0.3",
"debug": "^4.3.4",
"esbuild": "^0.21.3",
"escape-html": "^1.0.3",
"execa": "^9.1.0",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
Expand Down
16 changes: 0 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/client/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
type AsyncComponentLoader
} from 'vue'

export { inBrowser } from '../shared'
export { inBrowser, escapeHtml as _escapeHtml } from '../shared'

/**
* Join two paths by resolving the slash collision.
Expand Down
3 changes: 2 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export {
onContentUpdated,
defineClientComponent,
withBase,
getScrollOffset
getScrollOffset,
_escapeHtml
} from './app/utils'

// components
Expand Down
8 changes: 4 additions & 4 deletions src/node/build/render.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { isBooleanAttr } from '@vue/shared'
import escape from 'escape-html'
import fs from 'fs-extra'
import path from 'path'
import { pathToFileURL } from 'url'
import { normalizePath, transformWithEsbuild, type Rollup } from 'vite'
import { version } from '../../../package.json'
import type { SiteConfig } from '../config'
import {
EXTERNAL_URL_RE,
createTitle,
escapeHtml,
mergeHead,
notFoundPageData,
resolveSiteDataByRoute,
Expand All @@ -17,7 +18,6 @@ import {
type PageData,
type SSGContext
} from '../shared'
import { version } from '../../../package.json'

export async function renderPage(
render: (path: string) => Promise<SSGContext>,
Expand Down Expand Up @@ -163,7 +163,7 @@ export async function renderPage(
? ''
: '<meta name="viewport" content="width=device-width,initial-scale=1">'
}
<title>${escape(title)}</title>
<title>${escapeHtml(title)}</title>
${
isDescriptionOverridden(head)
? ''
Expand Down Expand Up @@ -260,7 +260,7 @@ function renderAttrs(attrs: Record<string, string>): string {
return Object.keys(attrs)
.map((key) => {
if (isBooleanAttr(key)) return ` ${key}`
return ` ${key}="${escape(attrs[key] as string)}"`
return ` ${key}="${escapeHtml(attrs[key] as string)}"`
})
.join('')
}
Expand Down
5 changes: 1 addition & 4 deletions src/node/markdown/plugins/restoreEntities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type MarkdownIt from 'markdown-it'
import type StateCore from 'markdown-it/lib/rules_core/state_core.mjs'
import type Token from 'markdown-it/lib/token.mjs'
import { escapeHtml } from '../../shared'

export function restoreEntities(md: MarkdownIt): void {
md.core.ruler.at('text_join', text_join)
Expand Down Expand Up @@ -47,7 +48,3 @@ function getContent(token: Token): string {
? '&amp;'
: token.content
}

function escapeHtml(str: string): string {
return str.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
}
11 changes: 11 additions & 0 deletions src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,14 @@ export function treatAsHtml(filename: string): boolean {
export function escapeRegExp(str: string) {
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
}

/**
* @internal
*/
export function escapeHtml(str: string): string {
return str
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/&(?![\w#]+;)/g, '&amp;')
}

0 comments on commit 7ea3572

Please sign in to comment.