Skip to content

Commit e3fc3ed

Browse files
committed
feat: add article title display and Giscus comments
1 parent 496c458 commit e3fc3ed

4 files changed

Lines changed: 100 additions & 1 deletion

File tree

docs/.vitepress/theme/ArchiveYear.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ function formatDate(d: string) {
5959
<ul class="posts-in-month">
6060
<li v-for="post in group.posts" :key="post.url" class="post-item">
6161
<time class="post-date">{{ formatDate(post.date) }}</time>
62-
<a :href="withBase(post.url)" class="post-title">{{ post.title }}</a>
62+
<a :href="withBase(post.url)" class="post-title">{{
63+
post.title
64+
}}</a>
6365
</li>
6466
</ul>
6567
</div>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script setup lang="ts">
2+
import { useData } from "vitepress";
3+
4+
const { frontmatter } = useData();
5+
</script>
6+
7+
<template>
8+
<h1 v-if="frontmatter.title" class="article-h1">
9+
{{ frontmatter.title }}
10+
</h1>
11+
</template>
12+
13+
<style scoped>
14+
.article-h1 {
15+
font-size: 1.9rem;
16+
font-weight: 700;
17+
line-height: 1.35;
18+
margin-bottom: 1rem;
19+
color: var(--vp-c-text-1);
20+
letter-spacing: -0.01em;
21+
}
22+
</style>

docs/.vitepress/theme/Giscus.vue

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<script setup lang="ts">
2+
import { onMounted, watch, ref } from "vue";
3+
import { useData, useRoute } from "vitepress";
4+
5+
const { isDark } = useData();
6+
const route = useRoute();
7+
const container = ref<HTMLElement>();
8+
9+
const GISCUS_REPO = "fonghehe/blog";
10+
const GISCUS_REPO_ID = "MDEwOlJlcG9zaXRvcnkyMTUxOTg3MTQ=";
11+
const GISCUS_CATEGORY = "Announcements";
12+
const GISCUS_CATEGORY_ID = "DIC_kwDODNOr-s4C8kR_";
13+
14+
function loadGiscus() {
15+
if (!container.value) return;
16+
container.value.innerHTML = "";
17+
18+
const script = document.createElement("script");
19+
script.src = "https://giscus.app/client.js";
20+
script.setAttribute("data-repo", GISCUS_REPO);
21+
script.setAttribute("data-repo-id", GISCUS_REPO_ID);
22+
script.setAttribute("data-category", GISCUS_CATEGORY);
23+
script.setAttribute("data-category-id", GISCUS_CATEGORY_ID);
24+
script.setAttribute("data-mapping", "pathname");
25+
script.setAttribute("data-strict", "0");
26+
script.setAttribute("data-reactions-enabled", "1");
27+
script.setAttribute("data-emit-metadata", "0");
28+
script.setAttribute("data-input-position", "top");
29+
script.setAttribute("data-theme", isDark.value ? "dark" : "light");
30+
script.setAttribute("data-lang", "zh-CN");
31+
script.setAttribute("data-loading", "lazy");
32+
script.crossOrigin = "anonymous";
33+
script.async = true;
34+
container.value.appendChild(script);
35+
}
36+
37+
onMounted(loadGiscus);
38+
39+
// 路由切换时重新加载(SPA 导航)
40+
watch(() => route.path, loadGiscus);
41+
42+
// 切换深色/浅色主题时同步通知 giscus iframe
43+
watch(isDark, (dark) => {
44+
const iframe = container.value?.querySelector<HTMLIFrameElement>(
45+
"iframe.giscus-frame",
46+
);
47+
if (iframe) {
48+
iframe.contentWindow?.postMessage(
49+
{ giscus: { setConfig: { theme: dark ? "dark" : "light" } } },
50+
"https://giscus.app",
51+
);
52+
}
53+
});
54+
</script>
55+
56+
<template>
57+
<div ref="container" class="giscus-wrapper" />
58+
</template>
59+
60+
<style scoped>
61+
.giscus-wrapper {
62+
margin-top: 3rem;
63+
padding-top: 2rem;
64+
border-top: 1px solid var(--vp-c-divider);
65+
}
66+
</style>

docs/.vitepress/theme/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import { h } from "vue";
12
import DefaultTheme from "vitepress/theme";
23
import PostList from "./PostList.vue";
34
import ArchiveYear from "./ArchiveYear.vue";
5+
import ArticleTitle from "./ArticleTitle.vue";
6+
import Giscus from "./Giscus.vue";
47
import type { Theme } from "vitepress";
58

69
export default {
710
extends: DefaultTheme,
11+
Layout() {
12+
return h(DefaultTheme.Layout, null, {
13+
"doc-before": () => h(ArticleTitle),
14+
"doc-after": () => h(Giscus),
15+
});
16+
},
817
enhanceApp({ app }) {
918
app.component("PostList", PostList);
1019
app.component("ArchiveYear", ArchiveYear);

0 commit comments

Comments
 (0)