mirror of
https://github.com/ryan4yin/nixos-and-flakes-book.git
synced 2025-08-09 12:54:55 +02:00
feat: SEO Improvement - add JSON-LD Scheme
This commit is contained in:
@ -1,10 +1,49 @@
|
|||||||
import { createWriteStream } from "node:fs";
|
import { createWriteStream } from "node:fs";
|
||||||
import { resolve } from "node:path";
|
import { resolve } from "node:path";
|
||||||
import { SitemapStream } from "sitemap";
|
import { SitemapStream } from "sitemap";
|
||||||
import { defineConfig } from "vitepress";
|
import { PageData, defineConfig } from "vitepress";
|
||||||
|
|
||||||
const links = [];
|
const links = [];
|
||||||
|
|
||||||
|
function getJSONLD(pageData: PageData) {
|
||||||
|
console.log(pageData.relativePath);
|
||||||
|
if (pageData.relativePath === "index.md") {
|
||||||
|
return `{
|
||||||
|
"@context":"http://schema.org",
|
||||||
|
"@type":"WebSite",
|
||||||
|
"url":"https:\/\/nixos-and-flakes.thiscute.world\/",
|
||||||
|
"inLanguage":"en",
|
||||||
|
"description":"An unofficial book for beginners",
|
||||||
|
"name":"NixOS & Flakes Book"
|
||||||
|
}`;
|
||||||
|
} else if (pageData.relativePath === "zh/index.md") {
|
||||||
|
return `{
|
||||||
|
"@context":"http://schema.org",
|
||||||
|
"@type":"WebSite",
|
||||||
|
"url":"https:\/\/nixos-and-flakes.thiscute.world\/zh\/",
|
||||||
|
"inLanguage":"zh-CN",
|
||||||
|
"description":"一份非官方的新手指南",
|
||||||
|
"name":"NixOS 与 Flakes"
|
||||||
|
}`;
|
||||||
|
} else {
|
||||||
|
let url = `https:\/\/nixos-and-flakes.thiscute.world\/${pageData.relativePath
|
||||||
|
.replace(/\.md$/, ".html")
|
||||||
|
.replace(/\/index\.html$/, "/")}`;
|
||||||
|
return `{
|
||||||
|
"@context":"http://schema.org",
|
||||||
|
"@type":"BlogPosting",
|
||||||
|
"headline":"NixOS \u0026 Nix Flakes - A Guide for Beginners",
|
||||||
|
"inLanguage":"en",
|
||||||
|
"mainEntityOfPage":{
|
||||||
|
"@type":"WebPage",
|
||||||
|
"@id":"${url}"
|
||||||
|
},
|
||||||
|
"keywords":"NixOS, Nix, Flakes, Linux, Tutorial",
|
||||||
|
"url":"${url}"
|
||||||
|
}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// https://vitepress.dev/reference/site-config
|
// https://vitepress.dev/reference/site-config
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
// sitemap.xml
|
// sitemap.xml
|
||||||
@ -27,6 +66,18 @@ export default defineConfig({
|
|||||||
await new Promise((r) => writeStream.on("finish", r));
|
await new Promise((r) => writeStream.on("finish", r));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// seo meta tags / JSON-LD
|
||||||
|
transformPageData(pageData) {
|
||||||
|
return {
|
||||||
|
frontmatter: {
|
||||||
|
...pageData.frontmatter,
|
||||||
|
head: [
|
||||||
|
["script", { type: "application/ld+json" }, getJSONLD(pageData)],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
// markdown options
|
// markdown options
|
||||||
markdown: {
|
markdown: {
|
||||||
theme: "material-theme-palenight",
|
theme: "material-theme-palenight",
|
||||||
|
Reference in New Issue
Block a user