【vitePress】基于github快速添加评论功能(giscus)

一.添加评论插件

使用giscus来做vitepress 的评论模块,使用也非常的简单,具体可以参考:giscus 文档,首先安装giscus

复制代码
npm i @giscus/vue

二.giscus操作

打开giscus 文档,如下图所示,填入你的 github 用户名 + 仓库名,勾选你需要的配置

如果显示不成功看是否满足上面三个条件:

1.公开仓库

2.在github安装giscuss app插件

3.打开discussion功能

找到setting

往下翻打开discussion

这样之后就会满足条件,giscus找到下面位置内容,后面要用

三.vitePress配置

.vitepress/theme/目录下创建myLayout.vue组件,添加 giscus 评论组件,

javascript 复制代码
<!--Layout.vue-->
<template>
  <Layout>
    <template #doc-footer-before> </template>
    <template #doc-after>
      <div style="margin-top: 24px">
        <Giscus
          :key="page.filePath"
          repo="*"
          repo-id="*"
          category="*"
          category-id="*"
          mapping="pathname"
          strict="0"
          reactions-enabled="1"
          emit-metadata="0"
          input-position="bottom"
          lang="zh-CN"
          crossorigin="anonymous"
          :theme="isDark ? 'dark' : 'light'"
        />
      </div>
    </template>
  </Layout>
</template>

<script lang="ts" setup>
import Giscus from "@giscus/vue";
import DefaultTheme from "vitepress/theme";
import { watch } from "vue";
import { inBrowser, useData } from "vitepress";

const { isDark, page } = useData();

const { Layout } = DefaultTheme;

watch(isDark, (dark) => {
  if (!inBrowser) return;

  const iframe = document
    .querySelector("giscus-widget")
    ?.shadowRoot?.querySelector("iframe");

  iframe?.contentWindow?.postMessage(
    { giscus: { setConfig: { theme: dark ? "dark" : "light" } } },
    "https://giscus.app"
  );
});
</script>

.vitepress/index.js配置文件中使用自定义布局。

javascript 复制代码
import DefaultTheme from 'vitepress/theme'
import Layout from './myLayout.vue' 
// import Layout from './Layout.vue'

export default {
  Layout,
  extends: DefaultTheme,
  enhanceApp({ app }) {
    //app.component('confetti', confetti)
  },
  
}

四、效果

相关推荐
人工智能训练3 小时前
【极速部署】Ubuntu24.04+CUDA13.0 玩转 VLLM 0.15.0:预编译 Wheel 包 GPU 版安装全攻略
运维·前端·人工智能·python·ai编程·cuda·vllm
会跑的葫芦怪3 小时前
若依Vue 项目多子路径配置
前端·javascript·vue.js
pas1366 小时前
40-mini-vue 实现三种联合类型
前端·javascript·vue.js
摇滚侠6 小时前
2 小时快速入门 ES6 基础视频教程
前端·ecmascript·es6
珑墨7 小时前
【Turbo】使用介绍
前端
军军君017 小时前
Three.js基础功能学习十三:太阳系实例上
前端·javascript·vue.js·学习·3d·前端框架·three
打小就很皮...8 小时前
Tesseract.js OCR 中文识别
前端·react.js·ocr
wuhen_n9 小时前
JavaScript内存管理与执行上下文
前端·javascript
Hi_kenyon9 小时前
理解vue中的ref
前端·javascript·vue.js
落霞的思绪11 小时前
配置React和React-dom为CDN引入
前端·react.js·前端框架