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

zio/stable
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
1 changed files with 31 additions and 2 deletions

View File

@ -17,10 +17,30 @@ export function setupPageHeader() {
dir: () => localeMap[locale.value] ?? 'auto', dir: () => localeMap[locale.value] ?? 'auto',
}, },
titleTemplate: (title) => { titleTemplate: (title) => {
let titleTemplate = title ? `${title} | ` : '' let titleTemplate = title ?? ''
titleTemplate += t('app_name')
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') if (buildInfo.env !== 'release')
titleTemplate += ` (${buildInfo.env})` titleTemplate += ` (${buildInfo.env})`
return titleTemplate return titleTemplate
}, },
link: process.client && useRuntimeConfig().public.pwaEnabled 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()
}