61DH61DH
Home
Diary
English
Tools
Photography
Health
Tech
Home
Diary
English
Tools
Photography
Health
Tech
  • 日记

    • 前言
    • Best Buy 与我的电器店记忆:从 2001 到今天的一次“退货又下单”
    • 中国流量异常日记
    • 我的 iPhone 17 Pro 首日开箱记
    • Perplexity 是流氓软件吗?– 一个技术站长的实录与更正
    • 给 VuePress 添加评论系统:Waline 实战指南
    • 从“骨瘤?”到“脂肪瘤”:我的第一次手术日记
    • Coffee Sleeve 是什麼?
    • 一次 Maker 升级到 SKY 的实操:从 Coinbase 到 MetaMask,再谈钱包安全
    • Skyoo「脑力素」广告很火,但靠谱吗?
    • 回国礼物推荐:清单与避坑指南
    • 厨房装修小记:岛台移除后的地板方案
    • 苹果 2025 发布会小记
    • VS Code 中 Markdown 粘贴图片工作流(VuePress)
    • VuePress v2 index.scss 覆盖问题笔记
    • 康师傅酸梅汤:异乡小记
    • 升级笔记:VuePress 从 2.0.0-beta.51 到 ^2.0.0-rc.24
    • 旧日记

💬 给 VuePress 添加评论系统:Waline 实战指南

很多人用 VuePress 2.0 搭建博客后,第一件事就是「加评论」。
VuePress 官方并不自带评论系统,但通过插件我们可以无缝集成 Waline、Giscus 等服务。本文就带你一步步配置 Waline。


1️⃣ 为什么选 Waline

  • 🔧 全功能:评论、表情、访问量统计\
  • 🌍 可自建:部署在 Vercel、Cloudflare Workers、VPS\
  • 🧩 VuePress 官方支持:配合 @vuepress/plugin-comment 使用

相比早期的 Valine(已停止维护),Waline 是更好的继任者。


2️⃣ 部署 Waline 后端

  1. 打开 Waline Vercel 模板 一键部署\

  2. 绑定自定义域名,例如:

    comments.61dh.com
    

    注意 DNS 需要配置成 CNAME 指向 Vercel 提供的专属地址,例如
    a87719c439880e48.vercel-dns-017.com

  3. 等待 Vercel 自动签发 HTTPS 证书,确保能访问:

    https://comments.61dh.com/comment
    

3️⃣ 安装插件依赖

在你的 VuePress 项目根目录执行:

npm i -D @vuepress/plugin-comment @waline/client

4️⃣ 配置 VuePress 插件

.vuepress/config.ts

import { defineUserConfig } from 'vuepress'
import { commentPlugin } from '@vuepress/plugin-comment'

export default defineUserConfig({
  plugins: [
    commentPlugin({
      provider: 'Waline',                        // 评论服务提供者
      serverURL: 'https://comments.61dh.com',    // Waline 后端地址
      comment: true,   // 开启评论
      pageview: true,  // 开启访问量统计
    }),
  ],
})

5️⃣ 自定义 Layout 加入评论组件

默认主题不会自动插入评论框,需要你在自定义 Layout 里手动加。

.vuepress/layouts/Layout.vue

<template>
  <ParentLayout>
    <template #page-bottom>
      <!-- 页面底部注入评论框 -->
      <CommentService />
    </template>
  </ParentLayout>
</template>

<script setup>
import ParentLayout from '@vuepress/theme-default/layouts/Layout.vue'
</script>

6️⃣ 在 client.ts 注册 Layout

关键一步 ------ 必须注册自定义 Layout,否则 VuePress 不会用你写的 Layout.vue。

.vuepress/client.ts

import { defineClientConfig } from 'vuepress/client'
import Layout from './layouts/Layout.vue'

export default defineClientConfig({
  layouts: { Layout },
})

7️⃣ 样式 & 字体优化(可选)

Waline 默认字体对中文友好,但英文比较丑,可以在 .vuepress/styles/index.scss 覆盖:

.waline {
  font-family: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif !important;
}

html.dark .waline {
  --waline-bgcolor: #1e1e1e;
  --waline-color: #ddd;
}

8️⃣ 常见问题

  • HTTP 可以访问,但 HTTPS 不行?
    → 确认 Vercel 域名已绑定,并且 DNS 配置指向 Vercel 提供的专属 CNAME。等待 SSL 签发成功。

  • 评论框不显示?

    • 检查 .vuepress/client.ts 是否正确注册 Layout\
    • 检查 Markdown 页面 frontmatter 是否有 comment: false\
    • 确认浏览器请求了 https://comments.61dh.com/comment
  • 首页看不到评论?
    默认主题首页(home: true)不渲染 page-bottom 插槽,评论只在普通文章页显示。


✅ 总结

  1. 在 Vercel 部署 Waline Server,绑定 https://comments.你的域名\
  2. VuePress 里安装 @vuepress/plugin-comment\
  3. 在 config.ts 配置 provider: "Waline" 和 serverURL\
  4. 在 Layout.vue 用 <CommentService /> 注入\
  5. 在 client.ts 注册 Layout

这样,你的 VuePress 博客就有一个完整的评论系统了 🚀

Last Updated: 2025/9/17 11:23
Contributors: Adam C
Prev
Perplexity 是流氓软件吗?– 一个技术站长的实录与更正
Next
从“骨瘤?”到“脂肪瘤”:我的第一次手术日记