Skip to content

Commit

Permalink
Merge pull request #11 from project-trans/dev/auto-description-improve
Browse files Browse the repository at this point in the history
feat: improve meta tags and support twitter preview card tags
  • Loading branch information
yaoyao-moe authored Jan 13, 2024
2 parents 1139046 + a74bd2a commit 5c9b904
Showing 1 changed file with 48 additions and 16 deletions.
64 changes: 48 additions & 16 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default defineConfig({
},
},
title: "SOC-8",
description: "跨性别和多元性别人群健康照护指南第八版(SOC-8)",
base: '/SOC-8/',
markdown: {
config(md) {
Expand All @@ -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: {
Expand Down Expand Up @@ -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
}

Expand All @@ -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 = ['']
Expand All @@ -108,30 +124,46 @@ 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)
pageSourceFileContent = pageSourceFileContent.replace(/[\r|\n]/gm, '')

// 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
}
})

0 comments on commit 5c9b904

Please sign in to comment.