2024-01-25 16:32:45 +00:00
|
|
|
import type { HeadConfig, TransformContext } from 'vitepress'
|
2023-11-12 16:42:57 +00:00
|
|
|
|
|
|
|
export function generateMeta(context: TransformContext, hostname: string) {
|
2024-01-25 16:32:45 +00:00
|
|
|
const head: HeadConfig[] = []
|
|
|
|
const { pageData } = context
|
2023-11-12 16:42:57 +00:00
|
|
|
|
2024-01-25 16:32:45 +00:00
|
|
|
const url = `${hostname}/${pageData.relativePath.replace(/((^|\/)index)?\.md$/, '$2')}`
|
2023-11-12 16:42:57 +00:00
|
|
|
|
2023-11-14 17:18:52 +00:00
|
|
|
head.push(
|
2024-01-25 16:32:45 +00:00
|
|
|
['link', { rel: 'canonical', href: url }],
|
|
|
|
['meta', { property: 'og:url', content: url }],
|
|
|
|
['meta', { name: 'twitter:url', content: url }],
|
|
|
|
['meta', { name: 'twitter:card', content: 'summary_large_image' }],
|
|
|
|
['meta', { property: 'og:title', content: pageData.frontmatter.title }],
|
|
|
|
['meta', { name: 'twitter:title', content: pageData.frontmatter.title }]
|
|
|
|
)
|
2023-11-12 17:48:18 +00:00
|
|
|
if (pageData.frontmatter.description) {
|
2023-11-14 17:18:52 +00:00
|
|
|
head.push(
|
|
|
|
[
|
2024-01-25 16:32:45 +00:00
|
|
|
'meta',
|
2023-11-14 17:18:52 +00:00
|
|
|
{
|
2024-01-25 16:32:45 +00:00
|
|
|
property: 'og:description',
|
|
|
|
content: pageData.frontmatter.description
|
|
|
|
}
|
2023-11-14 17:18:52 +00:00
|
|
|
],
|
|
|
|
[
|
2024-01-25 16:32:45 +00:00
|
|
|
'meta',
|
2023-11-14 17:18:52 +00:00
|
|
|
{
|
2024-01-25 16:32:45 +00:00
|
|
|
name: 'twitter:description',
|
|
|
|
content: pageData.frontmatter.description
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2023-11-12 17:48:18 +00:00
|
|
|
}
|
2023-11-12 16:42:57 +00:00
|
|
|
if (pageData.frontmatter.image) {
|
|
|
|
head.push([
|
2024-01-25 16:32:45 +00:00
|
|
|
'meta',
|
2023-11-12 16:42:57 +00:00
|
|
|
{
|
2024-01-25 16:32:45 +00:00
|
|
|
property: 'og:image',
|
|
|
|
content: `${hostname}/${pageData.frontmatter.image.replace(/^\//, '')}`
|
|
|
|
}
|
|
|
|
])
|
2023-11-12 16:42:57 +00:00
|
|
|
head.push([
|
2024-01-25 16:32:45 +00:00
|
|
|
'meta',
|
2023-11-12 16:42:57 +00:00
|
|
|
{
|
2024-01-25 16:32:45 +00:00
|
|
|
name: 'twitter:image',
|
|
|
|
content: `${hostname}/${pageData.frontmatter.image.replace(/^\//, '')}`
|
|
|
|
}
|
|
|
|
])
|
2023-11-12 16:42:57 +00:00
|
|
|
} else {
|
2024-01-25 16:32:45 +00:00
|
|
|
const url = pageData.filePath.replace('index.md', '').replace('.md', '')
|
|
|
|
const imageUrl = `${url}/__og_image__/og.png`
|
|
|
|
.replaceAll('//', '/')
|
|
|
|
.replace(/^\//, '')
|
2023-11-12 16:42:57 +00:00
|
|
|
|
2023-11-14 17:18:52 +00:00
|
|
|
head.push(
|
2024-01-25 16:32:45 +00:00
|
|
|
['meta', { property: 'og:image', content: `${hostname}/${imageUrl}` }],
|
|
|
|
['meta', { property: 'og:image:width', content: '1200' }],
|
|
|
|
['meta', { property: 'og:image:height', content: '628' }],
|
|
|
|
['meta', { property: 'og:image:type', content: 'image/png' }],
|
|
|
|
[
|
|
|
|
'meta',
|
|
|
|
{ property: 'og:image:alt', content: pageData.frontmatter.title }
|
|
|
|
],
|
|
|
|
['meta', { name: 'twitter:image', content: `${hostname}/${imageUrl}` }],
|
|
|
|
['meta', { name: 'twitter:image:width', content: '1200' }],
|
|
|
|
['meta', { name: 'twitter:image:height', content: '628' }],
|
|
|
|
[
|
|
|
|
'meta',
|
|
|
|
{ name: 'twitter:image:alt', content: pageData.frontmatter.title }
|
|
|
|
]
|
|
|
|
)
|
2023-11-12 16:42:57 +00:00
|
|
|
}
|
|
|
|
if (pageData.frontmatter.tag) {
|
2024-01-25 16:32:45 +00:00
|
|
|
head.push([
|
|
|
|
'meta',
|
|
|
|
{ property: 'article:tag', content: pageData.frontmatter.tag }
|
|
|
|
])
|
2023-11-12 16:42:57 +00:00
|
|
|
}
|
|
|
|
if (pageData.frontmatter.date) {
|
|
|
|
head.push([
|
2024-01-25 16:32:45 +00:00
|
|
|
'meta',
|
2023-11-12 16:42:57 +00:00
|
|
|
{
|
2024-01-25 16:32:45 +00:00
|
|
|
property: 'article:published_time',
|
|
|
|
content: pageData.frontmatter.date
|
|
|
|
}
|
|
|
|
])
|
2023-11-12 16:42:57 +00:00
|
|
|
}
|
|
|
|
if (pageData.lastUpdated && pageData.frontmatter.lastUpdated !== false) {
|
|
|
|
head.push([
|
2024-01-25 16:32:45 +00:00
|
|
|
'meta',
|
2023-11-12 16:42:57 +00:00
|
|
|
{
|
2024-01-25 16:32:45 +00:00
|
|
|
property: 'article:modified_time',
|
|
|
|
content: new Date(pageData.lastUpdated).toISOString()
|
|
|
|
}
|
|
|
|
])
|
2023-11-12 16:42:57 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:32:45 +00:00
|
|
|
return head
|
2023-11-12 16:42:57 +00:00
|
|
|
}
|