mirror of
https://github.com/fmhy/edit.git
synced 2026-02-18 09:11:33 +11:00
comment
This commit is contained in:
parent
99766f9439
commit
228d89ca23
1 changed files with 20 additions and 14 deletions
|
|
@ -1,12 +1,19 @@
|
||||||
import type { HeadConfig, TransformContext } from 'vitepress'
|
import type { HeadConfig, TransformContext } from 'vitepress'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates meta tags for a given page based on the page's frontmatter and file path.
|
||||||
|
* @param context - The transform context containing information about the current page.
|
||||||
|
* @param hostname - The hostname to use for generating URLs.
|
||||||
|
* @returns An array of HeadConfig objects representing the generated meta tags.
|
||||||
|
*/
|
||||||
export function generateMeta(context: TransformContext, hostname: string) {
|
export function generateMeta(context: TransformContext, hostname: string) {
|
||||||
const head: HeadConfig[] = []
|
const head: HeadConfig[] = [] // Initialize an array to store the generated meta tags
|
||||||
const { pageData } = context
|
const { pageData } = context // Get the page data from the transform context
|
||||||
|
|
||||||
|
// Generate the canonical URL for the page
|
||||||
const url = `${hostname}/${pageData.relativePath.replace(/((^|\/)index)?\.md$/, '$2')}`
|
const url = `${hostname}/${pageData.relativePath.replace(/((^|\/)index)?\.md$/, '$2')}`
|
||||||
|
|
||||||
head.push(
|
head.push( // Add meta tags to the head array
|
||||||
['link', { rel: 'canonical', href: url }],
|
['link', { rel: 'canonical', href: url }],
|
||||||
['meta', { property: 'og:url', content: url }],
|
['meta', { property: 'og:url', content: url }],
|
||||||
['meta', { name: 'twitter:url', content: url }],
|
['meta', { name: 'twitter:url', content: url }],
|
||||||
|
|
@ -14,6 +21,8 @@ export function generateMeta(context: TransformContext, hostname: string) {
|
||||||
['meta', { property: 'og:title', content: pageData.frontmatter.title }],
|
['meta', { property: 'og:title', content: pageData.frontmatter.title }],
|
||||||
['meta', { name: 'twitter:title', content: pageData.frontmatter.title }]
|
['meta', { name: 'twitter:title', content: pageData.frontmatter.title }]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// If the page has a description in its frontmatter, add it to the meta tags
|
||||||
if (pageData.frontmatter.description) {
|
if (pageData.frontmatter.description) {
|
||||||
head.push(
|
head.push(
|
||||||
[
|
[
|
||||||
|
|
@ -32,6 +41,8 @@ export function generateMeta(context: TransformContext, hostname: string) {
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the page has an image in its frontmatter, add it to the meta tags
|
||||||
if (pageData.frontmatter.image) {
|
if (pageData.frontmatter.image) {
|
||||||
head.push([
|
head.push([
|
||||||
'meta',
|
'meta',
|
||||||
|
|
@ -48,6 +59,7 @@ export function generateMeta(context: TransformContext, hostname: string) {
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
} else {
|
} else {
|
||||||
|
// If no image is provided, generate a default image based on the page's URL
|
||||||
const url = pageData.filePath.replace('index.md', '').replace('.md', '')
|
const url = pageData.filePath.replace('index.md', '').replace('.md', '')
|
||||||
const imageUrl = `${url}/__og_image__/og.png`
|
const imageUrl = `${url}/__og_image__/og.png`
|
||||||
.replaceAll('//', '/')
|
.replaceAll('//', '/')
|
||||||
|
|
@ -71,12 +83,16 @@ export function generateMeta(context: TransformContext, hostname: string) {
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the page has a tag in its frontmatter, add it to the meta tags
|
||||||
if (pageData.frontmatter.tag) {
|
if (pageData.frontmatter.tag) {
|
||||||
head.push([
|
head.push([
|
||||||
'meta',
|
'meta',
|
||||||
{ property: 'article:tag', content: pageData.frontmatter.tag }
|
{ property: 'article:tag', content: pageData.frontmatter.tag }
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the page has a date in its frontmatter, add it to the meta tags
|
||||||
if (pageData.frontmatter.date) {
|
if (pageData.frontmatter.date) {
|
||||||
head.push([
|
head.push([
|
||||||
'meta',
|
'meta',
|
||||||
|
|
@ -86,15 +102,5 @@ export function generateMeta(context: TransformContext, hostname: string) {
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
if (pageData.lastUpdated && pageData.frontmatter.lastUpdated !== false) {
|
|
||||||
head.push([
|
|
||||||
'meta',
|
|
||||||
{
|
|
||||||
property: 'article:modified_time',
|
|
||||||
content: new Date(pageData.lastUpdated).toISOString()
|
|
||||||
}
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
return head
|
// If the page has a lastUpdated field and it's not explicitly set to false, add the
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue