Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exposing page metadata #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { buildEnd } from "./config/buildEnd";
import { sidebar } from "./config/sidebar";
import { nav } from "./config/navbar";
import { markdown } from "./config/markdown";
import { head } from "./config/head";
import { head, transformPageData } from "./config/head";
import { vite } from "./config/vite";

export default defineConfig({
Expand All @@ -20,6 +20,7 @@ export default defineConfig({
},

head,
transformPageData,

themeConfig: {
logo: "/logo.png",
Expand Down
33 changes: 32 additions & 1 deletion .vitepress/config/head.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DefaultTheme, UserConfig } from "vitepress";
import type { DefaultTheme, PageData, UserConfig } from "vitepress";

const ogDescription =
"Zotero 中文社区,Zotero 中文维护小组,Zotero 插件,Zotero 中文 CSL 样式";
Expand Down Expand Up @@ -32,3 +32,34 @@ export const head: UserConfig["head"] = [
gtag('config', 'G-YHYFX0LRZK');`,
],
];

export const transformPageData = (pageData: PageData) => {
console.log(pageData);
pageData.frontmatter.head ??= [];
pageData.frontmatter.head.push([
"meta",
{
name: "citation_title",
content:
pageData.frontmatter.layout === "home"
? `Zotero 中文社区`
: `${pageData.title} | Zotero 中文社区`,
},
]);
pageData.frontmatter.head.push([
"meta",
{
name: "og:type",
content: pageData.frontmatter.layout === "doc" ? `document` : `website`,
},
]);

const canonicalUrl = `https://zotero-chinese.com/${pageData.relativePath}`
.replace(/index\.md$/, "")
.replace(/\.md$/, ".html");

pageData.frontmatter.head.push([
"link",
{ rel: "canonical", href: canonicalUrl },
]);
};