fix: unescape html entities in page title (#1392)

This commit is contained in:
Joaquín Sánchez 2023-01-25 14:12:55 +01:00 committed by GitHub
parent 18af058e4d
commit 3c888d3914
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,10 +17,30 @@ export function setupPageHeader() {
dir: () => localeMap[locale.value] ?? 'auto',
},
titleTemplate: (title) => {
let titleTemplate = title ? `${title} | ` : ''
titleTemplate += t('app_name')
let titleTemplate = title ?? ''
if (titleTemplate.match(/&[a-z0-9#]+;/gi)) {
titleTemplate = unescapeTitleTemplate(titleTemplate, [
['"', ['"', '"']],
['&', ['&', '&']],
['\'', [''', ''']],
['\u003C', ['<', '<']],
['\u003E', ['>', '>']],
])
if (titleTemplate.length > 60)
titleTemplate = `${titleTemplate.slice(0, 60)}...${titleTemplate.endsWith('"') ? '"' : ''}`
if (!titleTemplate.includes('"'))
titleTemplate = `"${titleTemplate}"`
}
else if (titleTemplate.length > 60) {
titleTemplate = `${titleTemplate.slice(0, 60)}...${titleTemplate.endsWith('"') ? '"' : ''}`
}
titleTemplate += ` | ${t('app_name')}`
if (buildInfo.env !== 'release')
titleTemplate += ` (${buildInfo.env})`
return titleTemplate
},
link: process.client && useRuntimeConfig().public.pwaEnabled
@ -32,3 +52,12 @@ export function setupPageHeader() {
: [],
})
}
function unescapeTitleTemplate(titleTemplate: string, replacements: [string, string[]][]) {
let result = titleTemplate
for (const [replacement, entities] of replacements) {
for (const e of entities)
result = result.replaceAll(e, replacement)
}
return result.trim()
}