Skip to content

Commit

Permalink
feat: add JSON-LD for SEO
Browse files Browse the repository at this point in the history
  • Loading branch information
laplacetw committed Apr 15, 2024
1 parent 586d5bc commit 1bace3f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 45 deletions.
19 changes: 18 additions & 1 deletion .vitepress/theme/components/PostHeader.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
<script setup lang='ts'>
import { useData } from 'vitepress'
import { onMounted } from 'vue'
const { page, frontmatter } = useData()
const { frontmatter } = useData()
onMounted(() => {
const lastUpdated = new Date(<number> page.value.lastUpdated).toISOString()
const dataJsonLD = {
"@context": "https://schema.org",
"@type": "BlogPosting",
"headline": frontmatter.value.title,
"datePublished": frontmatter.value.date,
"dateModified": lastUpdated,
}
const script = document.createElement('script')
script.id = 'postJsonLd'
script.setAttribute('type', 'application/ld+json')
script.textContent = JSON.stringify(dataJsonLD)
document.head.appendChild(script)
})
</script>

<template>
Expand Down
9 changes: 9 additions & 0 deletions .vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,14 @@ export default {
app.component('Category', Category)
app.component('Tags', Tags)
app.component('About', About)

// https://github.com/vuejs/vitepress/issues/3100
router.onBeforeRouteChange = () => {
const jsonLdId = 'postJsonLd'
const jsonLd = document.getElementById(jsonLdId)
if (jsonLd) {
jsonLd.parentNode?.removeChild(jsonLd)
}
}
}
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2024-04-16

### Features
- bump to vitepress@1.1.0
- add JSON-LD for SEO

# 2024-03-26

### Features
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![](https://img.shields.io/github/license/laplacetw/vitepress-theme-trigger)
![Node.js >= 18.0.0](https://img.shields.io/badge/Node.js-%3E%3D18.0.0-green)
![VitePress 1.0.1](https://img.shields.io/badge/VitePress-1.0.1-green)
![VitePress 1.1.0](https://img.shields.io/badge/VitePress-1.1.0-green)

# vitepress-theme-trigger
Blog theme for [VitePress](https://vitepress.dev) with [Tailwind CSS](https://tailwindcss.com).
Expand Down
2 changes: 1 addition & 1 deletion README_CH.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![](https://img.shields.io/github/license/laplacetw/vitepress-theme-trigger)
![Node.js >= 18.0.0](https://img.shields.io/badge/Node.js-%3E%3D18.0.0-green)
![VitePress 1.0.1](https://img.shields.io/badge/VitePress-1.0.1-green)
![VitePress 1.1.0](https://img.shields.io/badge/VitePress-1.1.0-green)

# vitepress-theme-trigger
為建立部落格而生的 [VitePress](https://vitepress.dev) + [Tailwind CSS](https://tailwindcss.com) 主題
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
},
"devDependencies": {
"@heroicons/vue": "^2.0.18",
"autoprefixer": "^10.4.15",
"autoprefixer": "^10.4.16",
"markdown-it-footnote": "^4.0.0",
"markdown-it-mathjax3": "^4.3.2",
"postcss": "8.4.35",
"tailwindcss": "^3.3.3",
"typescript": "^5.1.6",
"vitepress": "1.0.1",
"typescript": "^5.2.2",
"vitepress": "1.1.0",
"vue": "^3.3.4"
}
}
78 changes: 39 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1bace3f

Please sign in to comment.