Skip to content

Commit 43c50a6

Browse files
authored
seo: add appropriate og tags for link sharing (#460)
1 parent 0e56654 commit 43c50a6

File tree

2 files changed

+139
-104
lines changed

2 files changed

+139
-104
lines changed

.vitepress/config/shared.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const head: HeadConfig[] = [
2222
],
2323
// Open Graph
2424
["meta", { property: "og:site_name", content: "Oxc" }],
25-
["meta", { property: "og:url", content: "https://github.com/oxc-project" }],
2625
[
2726
"meta",
2827
{
@@ -65,11 +64,11 @@ export const sharedConfig = defineConfig({
6564
base: "/",
6665
head,
6766
lastUpdated: false,
68-
transformHead: ({ pageData }) => {
69-
const head: HeadConfig[] = [];
67+
transformPageData(pageData) {
68+
pageData.frontmatter.head ??= [];
7069

7170
if (pageData.frontmatter.canonical) {
72-
head.push([
71+
pageData.frontmatter.head.push([
7372
"link",
7473
{
7574
rel: "canonical",
@@ -78,7 +77,31 @@ export const sharedConfig = defineConfig({
7877
]);
7978
}
8079

81-
return head;
80+
// Add page-specific Open Graph and Twitter meta tags
81+
const title = pageData.frontmatter.title || pageData.title;
82+
const description = pageData.frontmatter.description || pageData.description
83+
|| "A collection of high-performance JavaScript tools written in Rust";
84+
85+
// Construct the canonical URL for the page
86+
let url = "https://oxc.rs";
87+
if (pageData.relativePath !== "index.md") {
88+
const path = pageData.relativePath.replace(/\.md$/, ".html").replace(/\/index\.html$/, "/");
89+
if (path !== "index.html") {
90+
url += "/" + path.replace(/^\/+/, "");
91+
}
92+
}
93+
94+
if (title) {
95+
pageData.frontmatter.head.push(["meta", { property: "og:title", content: title }]);
96+
pageData.frontmatter.head.push(["meta", { name: "twitter:title", content: title }]);
97+
}
98+
99+
if (description) {
100+
pageData.frontmatter.head.push(["meta", { property: "og:description", content: description }]);
101+
pageData.frontmatter.head.push(["meta", { name: "twitter:description", content: description }]);
102+
}
103+
104+
pageData.frontmatter.head.push(["meta", { property: "og:url", content: url }]);
82105
},
83106
themeConfig: {
84107
siteTitle: "Oxc",

0 commit comments

Comments
 (0)