From a74bd2a88bc381713300e2972c3f5fd71a0ce535 Mon Sep 17 00:00:00 2001 From: Neko Ayaka Date: Sun, 14 Jan 2024 00:03:51 +0800 Subject: [PATCH] feat: improve meta tags and support twitter preview card tags Signed-off-by: Neko Ayaka --- docs/.vitepress/config.ts | 64 +++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 5880f57..65403a7 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -22,7 +22,6 @@ export default defineConfig({ }, }, title: "SOC-8", - description: "跨性别和多元性别人群健康照护指南第八版(SOC-8)", base: '/SOC-8/', markdown: { config(md) { @@ -34,7 +33,6 @@ export default defineConfig({ dir, lastUpdated: true, head: [ - ['meta', { property: 'og:title', content: siteTitle }], ['meta', { property: 'og:site_name', content: siteTitle }], ], themeConfig: { @@ -70,6 +68,22 @@ export default defineConfig({ const pageSourceFileStat = statSync(join(dir, context.pageData.filePath)) if (pageSourceFileStat.isDirectory()) { + head.push([ + 'meta', + { + property: 'og:title', + content: siteTitle + } + ]) + + head.push([ + 'meta', + { + name: 'description', + content: '跨性别和多元性别人群健康照护指南第八版(SOC-8)' + } + ]) + return head } @@ -79,25 +93,27 @@ export default defineConfig({ pageSourceFileContent = pageSourceFileContent.replace(/---[\s\S]*?---/, '') // remove markdown heading markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/^(#+)\s+(.*)/gm, '$2') + pageSourceFileContent = pageSourceFileContent.replace(/^(#+)\s+(.*)/gm, ' $2 ') // remove markdown link markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/\[([^\]]+)\]\([^)]+\)/gm, '$1') + pageSourceFileContent = pageSourceFileContent.replace(/\[([^\]]+)\]\([^)]+\)/gm, ' $1 ') // remove markdown image markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/\!\[([^\]]+)\]\([^)]+\)/gm, '$1') + pageSourceFileContent = pageSourceFileContent.replace(/\!\[([^\]]+)\]\([^)]+\)/gm, ' $1 ') + // remove markdown reference link markup but keep the text content + pageSourceFileContent = pageSourceFileContent.replace(/\[.*]/gm, '') // remove markdown bold markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/\*\*([^*]+)\*\*/gm, '$1') - pageSourceFileContent = pageSourceFileContent.replace(/__([^*]+)__/gm, '$1') + pageSourceFileContent = pageSourceFileContent.replace(/\*\*([^*]+)\*\*/gm, ' $1 ') + pageSourceFileContent = pageSourceFileContent.replace(/__([^*]+)__/gm, ' $1 ') // remove markdown italic markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/\*([^*]+)\*/gm, '$1') - pageSourceFileContent = pageSourceFileContent.replace(/_([^*]+)_/gm, '$1') + pageSourceFileContent = pageSourceFileContent.replace(/\*([^*]+)\*/gm, ' $1 ') + pageSourceFileContent = pageSourceFileContent.replace(/_([^*]+)_/gm, ' $1 ') // remove markdown code markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/`([^`]+)`/gm, '$1') + pageSourceFileContent = pageSourceFileContent.replace(/`([^`]+)`/gm, ' $1 ') // remove markdown code block markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/```([^`]+)```/gm, '$1') + pageSourceFileContent = pageSourceFileContent.replace(/```([^`]+)```/gm, ' $1 ') // remove markdown table header markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/\|:?-+:?\|/gm, '|') + pageSourceFileContent = pageSourceFileContent.replace(/\|:?-+:?\|/gm, '') // remove markdown table cell markup but keep the text content - pageSourceFileContent = pageSourceFileContent.replace(/\|([^|]+)\|/gm, '$1') + pageSourceFileContent = pageSourceFileContent.replace(/\|([^|]+)\|/gm, ' $1 ') // remove specific html tags completely const tags = [''] @@ -108,7 +124,7 @@ export default defineConfig({ // remove specific html tags but keep the text content const tagsToKeepContent = ['u', 'Containers', 'img', 'a'] tagsToKeepContent.forEach((tag) => { - pageSourceFileContent = pageSourceFileContent.replace(new RegExp(`<${tag}[^>]*>([\\s\\S]*?)<\\/${tag}>`, 'g'), '$1') + pageSourceFileContent = pageSourceFileContent.replace(new RegExp(`<${tag}[^>]*>([\\s\\S]*?)<\\/${tag}>`, 'g'), ' $1 ') }) // remove all new lines (either \r, \n) @@ -116,22 +132,38 @@ export default defineConfig({ // calculate the first 200 characters of the page content let pageContent = pageSourceFileContent.slice(0, 200) + // trim space + pageContent = pageContent.trim() // if pageSourceFileContent is longer than 200 characters, add ellipsis - if (pageSourceFileContent.length > 200) { + if (pageSourceFileContent.length > 100) { pageContent += '...' } - // add the page content as meta description head.push([ 'meta', { name: 'description', content: pageContent } ]) + head.push([ + 'meta', + { property: 'og:title', content: context.title } + ]) + head.push([ 'meta', { property: 'og:description', content: pageContent } ]) + head.push([ + 'meta', + { property: 'og:title', content: context.title } + ]) + + head.push([ + 'meta', + { property: 'twitter:description', content: pageContent } + ]) + return head } })