11<script setup lang="ts">
2- import { onMounted , watch , ref } from " vue" ;
2+ import { onMounted , watch , ref , computed } from " vue" ;
33import { useData , useRoute } from " vitepress" ;
44
5- const { isDark } = useData ();
5+ const { isDark, frontmatter } = useData ();
66const route = useRoute ();
77const container = ref <HTMLElement >();
88
9+ // 首页、文章列表页、归档索引页不显示评论框
10+ const HIDE_COMMENT_PATHS = [" /blog/" , " /blog/posts/" , " /blog/archive/" ];
11+ const shouldShow = computed (() => {
12+ if (frontmatter .value .layout === " home" ) return false ;
13+ if (frontmatter .value .comment === false ) return false ;
14+ if (HIDE_COMMENT_PATHS .includes (route .path )) return false ;
15+ return true ;
16+ });
17+
918const GISCUS_REPO = " fonghehe/blog" ;
1019const GISCUS_REPO_ID = " MDEwOlJlcG9zaXRvcnkyMTUxOTg3MTQ=" ;
1120const GISCUS_CATEGORY = " Announcements" ;
@@ -34,10 +43,18 @@ function loadGiscus() {
3443 container .value .appendChild (script );
3544}
3645
37- onMounted (loadGiscus );
46+ onMounted (() => {
47+ if (shouldShow .value ) loadGiscus ();
48+ });
3849
3950// 路由切换时重新加载(SPA 导航)
40- watch (() => route .path , loadGiscus );
51+ watch (
52+ () => route .path ,
53+ () => {
54+ if (shouldShow .value ) loadGiscus ();
55+ else if (container .value ) container .value .innerHTML = " " ;
56+ },
57+ );
4158
4259// 切换深色/浅色主题时同步通知 giscus iframe
4360watch (isDark , (dark ) => {
@@ -54,7 +71,7 @@ watch(isDark, (dark) => {
5471 </script >
5572
5673<template >
57- <div ref =" container" class =" giscus-wrapper" />
74+ <div v-if = " shouldShow " ref =" container" class =" giscus-wrapper" />
5875</template >
5976
6077<style scoped>
0 commit comments