101 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-01-25 16:32:45 +00:00
import type { HeadConfig, TransformContext } from 'vitepress'
2023-11-12 22:12:57 +05:30
export function generateMeta(context: TransformContext, hostname: string) {
2024-01-25 16:32:45 +00:00
const head: HeadConfig[] = []
const { pageData } = context
2023-11-12 22:12:57 +05:30
2024-01-25 16:32:45 +00:00
const url = `${hostname}/${pageData.relativePath.replace(/((^|\/)index)?\.md$/, '$2')}`
2023-11-12 22:12:57 +05:30
2023-11-14 22:48:52 +05:30
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 23:18:18 +05:30
if (pageData.frontmatter.description) {
2023-11-14 22:48:52 +05:30
head.push(
[
2024-01-25 16:32:45 +00:00
'meta',
2023-11-14 22:48:52 +05:30
{
2024-01-25 16:32:45 +00:00
property: 'og:description',
content: pageData.frontmatter.description
}
2023-11-14 22:48:52 +05:30
],
[
2024-01-25 16:32:45 +00:00
'meta',
2023-11-14 22:48:52 +05:30
{
2024-01-25 16:32:45 +00:00
name: 'twitter:description',
content: pageData.frontmatter.description
}
]
)
2023-11-12 23:18:18 +05:30
}
2023-11-12 22:12:57 +05:30
if (pageData.frontmatter.image) {
head.push([
2024-01-25 16:32:45 +00:00
'meta',
2023-11-12 22:12:57 +05:30
{
2024-01-25 16:32:45 +00:00
property: 'og:image',
content: `${hostname}/${pageData.frontmatter.image.replace(/^\//, '')}`
}
])
2023-11-12 22:12:57 +05:30
head.push([
2024-01-25 16:32:45 +00:00
'meta',
2023-11-12 22:12:57 +05:30
{
2024-01-25 16:32:45 +00:00
name: 'twitter:image',
content: `${hostname}/${pageData.frontmatter.image.replace(/^\//, '')}`
}
])
2023-11-12 22:12:57 +05:30
} 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 22:12:57 +05:30
2023-11-14 22:48:52 +05:30
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 22:12:57 +05:30
}
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 22:12:57 +05:30
}
if (pageData.frontmatter.date) {
head.push([
2024-01-25 16:32:45 +00:00
'meta',
2023-11-12 22:12:57 +05:30
{
2024-01-25 16:32:45 +00:00
property: 'article:published_time',
content: pageData.frontmatter.date
}
])
2023-11-12 22:12:57 +05:30
}
if (pageData.lastUpdated && pageData.frontmatter.lastUpdated !== false) {
head.push([
2024-01-25 16:32:45 +00:00
'meta',
2023-11-12 22:12:57 +05:30
{
2024-01-25 16:32:45 +00:00
property: 'article:modified_time',
content: new Date(pageData.lastUpdated).toISOString()
}
])
2023-11-12 22:12:57 +05:30
}
2024-01-25 16:32:45 +00:00
return head
2023-11-12 22:12:57 +05:30
}