Skip to content

Commit 1deb120

Browse files
committed
feat: add JSON-LD for SEO
1 parent 586d5bc commit 1deb120

File tree

7 files changed

+84
-45
lines changed

7 files changed

+84
-45
lines changed

.vitepress/theme/components/PostHeader.vue

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
<script setup lang='ts'>
22
import { useData } from 'vitepress'
3+
import { onMounted, onBeforeUnmount } from 'vue'
4+
const { page, frontmatter } = useData()
35
4-
const { frontmatter } = useData()
6+
let script: HTMLScriptElement
7+
8+
onMounted(() => {
9+
const lastUpdated = new Date(<number> page.value.lastUpdated).toISOString()
10+
const dataJsonLD = {
11+
"@context": "https://schema.org",
12+
"@type": "BlogPosting",
13+
"headline": frontmatter.value.title,
14+
"datePublished": frontmatter.value.date,
15+
"dateModified": lastUpdated,
16+
}
17+
18+
script = document.createElement('script')
19+
script.setAttribute('type', 'application/ld+json')
20+
script.textContent = JSON.stringify(dataJsonLD)
21+
document.head.appendChild(script)
22+
})
23+
24+
onBeforeUnmount(() => {
25+
if (script && script.parentElement) {
26+
script.parentElement.removeChild(script)
27+
}
28+
})
529
</script>
630

731
<template>

.vitepress/theme/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,14 @@ export default {
2020
app.component('Category', Category)
2121
app.component('Tags', Tags)
2222
app.component('About', About)
23+
24+
// https://github.com/vuejs/vitepress/issues/3100
25+
router.onBeforeRouteChange = () => {
26+
const jsonLdId = 'postJsonLd'
27+
const jsonLd = document.getElementById(jsonLdId)
28+
if (jsonLd) {
29+
jsonLd.parentNode?.removeChild(jsonLd)
30+
}
31+
}
2332
}
2433
}

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2024-04-16
2+
3+
### Features
4+
5+
- add JSON-LD for SEO
6+
17
# 2024-03-26
28

39
### Features

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![](https://img.shields.io/github/license/laplacetw/vitepress-theme-trigger)
22
![Node.js >= 18.0.0](https://img.shields.io/badge/Node.js-%3E%3D18.0.0-green)
3-
![VitePress 1.0.1](https://img.shields.io/badge/VitePress-1.0.1-green)
3+
![VitePress 1.1.0](https://img.shields.io/badge/VitePress-1.1.0-green)
44

55
# vitepress-theme-trigger
66
Blog theme for [VitePress](https://vitepress.dev) with [Tailwind CSS](https://tailwindcss.com).

README_CH.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![](https://img.shields.io/github/license/laplacetw/vitepress-theme-trigger)
22
![Node.js >= 18.0.0](https://img.shields.io/badge/Node.js-%3E%3D18.0.0-green)
3-
![VitePress 1.0.1](https://img.shields.io/badge/VitePress-1.0.1-green)
3+
![VitePress 1.1.0](https://img.shields.io/badge/VitePress-1.1.0-green)
44

55
# vitepress-theme-trigger
66
為建立部落格而生的 [VitePress](https://vitepress.dev) + [Tailwind CSS](https://tailwindcss.com) 主題

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
},
2020
"devDependencies": {
2121
"@heroicons/vue": "^2.0.18",
22-
"autoprefixer": "^10.4.15",
22+
"autoprefixer": "^10.4.16",
2323
"markdown-it-footnote": "^4.0.0",
2424
"markdown-it-mathjax3": "^4.3.2",
2525
"postcss": "8.4.35",
2626
"tailwindcss": "^3.3.3",
27-
"typescript": "^5.1.6",
28-
"vitepress": "1.0.1",
27+
"typescript": "^5.2.2",
28+
"vitepress": "1.1.0",
2929
"vue": "^3.3.4"
3030
}
3131
}

pnpm-lock.yaml

+39-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)