FMHYedit/.vitepress/hooks/meta.ts

90 lines
2.8 KiB
TypeScript
Raw Normal View History

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