Appearance
本项目clone自https://github.com/airene/vitepress-blog-pure项目,他这个的评论有不少问题,比如评论框无法置于顶部。所以我就改了下,使用 giscus :https://giscus.app/ 。原来的评论要在每个MD文档里写入< Comment />,现在修改成没有申明时就自动加载。
本项目clone自vitepress-blog-pure项目,他这个的评论有不少问题,比如评论框无法置于顶部。所以我就改了下,使用 giscus 。
原来的评论要在每个MD文档里写入< Comment / >,现在修改成没有申明时就自动加载。
在你的 index.ts 文件中,你已经定义了一个自定义主题,并且注册了一些全局组件。为了在你的 VitePress 主题中集成 Giscus 评论系统,你需要进行以下修改:
index.ts
确保你已经安装了 vitepress-plugin-comment-with-giscus 插件:
vitepress-plugin-comment-with-giscus
npm install vitepress-plugin-comment-with-giscus
或者使用 yarn:
yarn add vitepress-plugin-comment-with-giscus
在你的 index.ts 文件中,你需要引入 Giscus 插件,并在 setup 函数中进行配置。以下是修改后的代码:
setup
import { Theme } from 'vitepress' import DefaultTheme from 'vitepress/theme' import NewLayout from './components/NewLayout.vue' import Archives from './components/Archives.vue' import Category from './components/Category.vue' import Tags from './components/Tags.vue' import Page from './components/Page.vue' import Comment from './components/CommentGiscus.vue' //引入新定义的评论组件 import { App } from 'vue' // 引入 Vue 的 App 类型 import './custom.css' export default { ...DefaultTheme, Layout: NewLayout, enhanceApp({ app }: { app: App }) { // 全局注册后,在Vue里就能直接使用了 app.component('Tags', Tags) app.component('Category', Category) app.component('Archives', Archives) app.component('Page', Page) app.component('Comment', Comment) } } satisfies Theme
确保在你的 NewLayout.vue 文件中,你已经正确地引入并使用了 Comment 组件。例如:
NewLayout.vue
Comment
<template> <Layout> <template #doc-before> <div style="padding-top: 20px" class="post-info" v-if="!$frontmatter.page"> {{ $frontmatter.date?.substring(0, 10) }} <span v-for="item in $frontmatter.tags" ><a :href="withBase(`/pages/tags.html?tag=${item}`)"> {{ item }}</a></span > </div> </template> <!--新增加的评论--> <template #doc-bottom> <Comment /> </template> </Layout> <Copyright /> </template> <script setup> import DefaultTheme from 'vitepress/theme' import Copyright from './Copyright.vue' import { withBase } from 'vitepress' const { Layout } = DefaultTheme </script>
在./vitepress/theme/components/ 目录下增加文件 CommentGiscus.vue,并添加如下内容:
./vitepress/theme/components/
CommentGiscus.vue
<template> <div id="giscus-container"></div> </template> <script setup> import giscusTalk from 'vitepress-plugin-comment-with-giscus' import { useData, useRoute } from 'vitepress' const { frontmatter } = useData() const route = useRoute() giscusTalk( { repo: 'your-username/your-repo-name', // 仓库名称 repoId: 'your-repo-id', // 仓库 ID category: 'General', // 讨论分类名称 categoryId: 'your-category-id', // 讨论分类 ID mapping: 'pathname', inputPosition: 'top', lang: 'en', lightTheme: 'light', darkTheme: 'transparent_dark' }, { frontmatter, route }, true ) </script>
npm run dev
yarn dev
--- comment: false ---
your-username/your-repo-name
your-repo-id
your-category-id
vitepress-theme-website
通过以上步骤,你就可以在你的 VitePress 主题中成功集成 Giscus 评论系统。
本项目clone自vitepress-blog-pure项目,他这个的评论有不少问题,比如评论框无法置于顶部。所以我就改了下,使用 giscus 。
原来的评论要在每个MD文档里写入< Comment / >,现在修改成没有申明时就自动加载。
在你的
index.ts
文件中,你已经定义了一个自定义主题,并且注册了一些全局组件。为了在你的 VitePress 主题中集成 Giscus 评论系统,你需要进行以下修改:1. 安装 Giscus 插件
确保你已经安装了
vitepress-plugin-comment-with-giscus
插件:或者使用 yarn:
2. 修改
index.ts
在你的
index.ts
文件中,你需要引入 Giscus 插件,并在setup
函数中进行配置。以下是修改后的代码:3. 在布局文件中添加评论组件
确保在你的
NewLayout.vue
文件中,你已经正确地引入并使用了Comment
组件。例如:4. 增加加评论组件CommentGiscus.vue
在
./vitepress/theme/components/
目录下增加文件CommentGiscus.vue
,并添加如下内容:5. 验证和优化
npm run dev
或yarn dev
,查看评论功能是否正常显示。注意事项
your-username/your-repo-name
、your-repo-id
、your-category-id
替换为你的实际 Giscus 配置信息。vitepress-theme-website
),确保它们与 Giscus 插件兼容。通过以上步骤,你就可以在你的 VitePress 主题中成功集成 Giscus 评论系统。