简体中文
外观
约 2032 字大约 7 分钟
2026-09-01
评论系统默认全局关闭,由 commentConfig.ts 统一管理。当前支持 Twikoo(自建/托管后端)、Giscus(基于 GitHub Discussions 的零后端方案)与 Waline(自建/托管服务端,支持匿名评论、登录、表情与文章表情反应)。遵循零额外负担原则:未开启时不产生任何外部请求、零额外 DOM 占用与零包体积膨胀。
commentConfig.ts
提示
按需懒加载策略 建议保持 lazy: true。评论客户端脚本仅在访客滚动至文章底部评论区域时才会按需下载并初始化,保证首屏极速加载与零多余网络开销。
lazy: true
export const commentConfig = withUserConfig("comment", { enable: false, // 全局总开关 provider: "none", // "none" | "twikoo" | "giscus" | "waline" lazy: true, // 视口懒加载 twikoo: { envId: "", scriptUrl: "https://cdn.jsdelivr.net/npm/twikoo@1.7.20/dist/twikoo.min.js", lang: "auto", placeholder: "Share your thoughts...", }, giscus: { repo: "", repoId: "", category: "Announcements", categoryId: "", mapping: "pathname", strict: false, reactionsEnabled: true, emitMetadata: false, inputPosition: "bottom", theme: { light: "light", dark: "dark" }, lang: "auto", scriptUrl: "https://giscus.app/client.js", }, waline: { serverURL: "", scriptUrl: "https://cdn.jsdelivr.net/npm/@waline/client@3.16.1/dist/waline.umd.js", styleUrl: "https://cdn.jsdelivr.net/npm/@waline/client@3.16.1/dist/waline.css", lang: "auto", locale: { placeholder: "Share your thoughts..." }, emoji: true, commentSorting: "latest", meta: ["nick", "mail", "link"], requiredMeta: [], login: "enable", wordLimit: 0, pageSize: 10, imageUploader: true, highlighter: true, texRenderer: false, noCopyright: false, noRss: true, recaptchaV3Key: "", turnstileKey: "", reaction: true, }, })
enable
boolean
false
provider
"none"
"twikoo"
"giscus"
"waline"
lazy
true
twikoo.envId
string
""
twikoo.scriptUrl
twikoo.lang
"auto"
auto
twikoo.placeholder
giscus.repo
owner/repo
giscus.repoId
giscus.categoryId
giscus.category
"Announcements"
giscus.mapping
"pathname"
pathname
url
title
og:title
specific
number
giscus.strict
giscus.reactionsEnabled
giscus.emitMetadata
giscus.inputPosition
"top"
"bottom"
giscus.theme.light
.dark
"light"
"dark"
giscus.lang
giscus.scriptUrl
waline.serverURL
waline.scriptUrl
.styleUrl
waline.lang
en
zh-CN
jp
waline.locale
{ placeholder }
placeholder
waline.emoji
waline.commentSorting
"latest"
"oldest"
"hottest"
waline.meta
.requiredMeta
["nick","mail","link"]
[]
requiredMeta
waline.login
"enable"
"disable"
"force"
waline.wordLimit
[min, max]
0
waline.pageSize
10
waline.imageUploader
.highlighter
.texRenderer
waline.noCopyright
.noRss
waline.recaptchaV3Key
.turnstileKey
waline.reaction
部署 Twikoo 服务端并获取环境 ID。支持腾讯云 CloudBase、Vercel、Railway 或私有部署。
修改配置:把 enable 设为 true,provider 设为 "twikoo",并填入 envId:
envId
export const commentConfig = withUserConfig("comment", { enable: true, provider: "twikoo", lazy: true, twikoo: { envId: "https://your-twikoo.vercel.app", scriptUrl: "https://cdn.jsdelivr.net/npm/twikoo@1.7.20/dist/twikoo.min.js", lang: "auto", placeholder: "说点什么吧……", }, })
重新构建部署,文章页底部即出现评论组件。
Giscus 基于 GitHub Discussions,无需部署任何后端,评论数据存储在你自己的公开仓库中。
准备仓库:创建一个公开 GitHub 仓库,在 Settings → General → Features 中开启 Discussions;安装 giscus App 并授权给该仓库。
获取 ID:打开 giscus.app,选择仓库与 Discussion 分类(推荐 Announcements,仅维护者可开新讨论),记下生成的 data-repo-id 与 data-category-id。
data-repo-id
data-category-id
修改配置:把 enable 设为 true,provider 设为 "giscus",填入三必填字段:
export const commentConfig = withUserConfig("comment", { enable: true, provider: "giscus", lazy: true, giscus: { repo: "owner/repo", repoId: "R_xxxxxxxxxx", categoryId: "DIC_xxxxxxxxxx", // 可选:明暗主题跟随站点自动切换 theme: { light: "light", dark: "transparent_dark" }, }, })
重新构建部署,文章页底部即出现 Giscus 评论区,明暗主题随站点切换自动同步。
Waline 的评论区沿用主题为 Twikoo 设计的线条样式,评论框中的表情显示为图标,文章表情反应显示在评论区顶部。
部署 Waline 服务端:按 Waline 官方文档 部署(Vercel、Deta、Railway 或私有部署),记下服务端地址。
修改配置:把 enable 设为 true,provider 设为 "waline",填入 serverURL:
serverURL
export const commentConfig = withUserConfig("comment", { enable: true, provider: "waline", lazy: true, waline: { serverURL: "https://comments.example.com", lang: "auto", locale: { placeholder: "说点什么吧……" }, // 可选:要求填写昵称与邮箱 requiredMeta: ["nick", "mail"], }, })
重新构建部署,文章页底部即出现 Waline 评论区;客户端 JS 与 CSS 在评论区进入视口后才加载。
笔记
配置以 JSON 形式写入页面,因此 imageUploader、highlighter、texRenderer 只接受 true / false,填写 Waline 原生的自定义函数会在构建时报错。主题不采集浏览量(Waline 的 pageview 始终关闭)。自托管客户端或升级版本时,scriptUrl 与 styleUrl 须保持同一版本。
imageUploader
highlighter
texRenderer
pageview
scriptUrl
styleUrl
配置解析器在以下任一情况返回 null(评论完全不渲染,零 DOM):
null
repo
repoId
categoryId
条件满足时组件才动态加载。配合 lazy: true,评论脚本在读者滚动到文章底部时才请求——不影响首屏性能。
frontmatter 中设置 comment: false 可对单篇文章关闭评论(全局开关需已启用):
comment: false
--- title: 这篇不开放评论 comment: false ---
关闭后 FAB 的「直达评论」按钮对该文章也零 DOM(见悬浮控制流)。
中文博客(本地化评论)
{ enable: true, provider: "twikoo", lazy: true, twikoo: { envId: "https://your-twikoo.vercel.app", lang: "zh-CN", placeholder: "友善发言,理性讨论", }, }
零后端 GitHub 评论
{ enable: true, provider: "giscus", lazy: true, giscus: { repo: "owner/repo", repoId: "R_xxxxxxxxxx", categoryId: "DIC_xxxxxxxxxx", mapping: "pathname", lang: "auto", }, }
自建 CDN 脚本
twikoo: { envId: "…", scriptUrl: "https://your-cdn.example.com/twikoo.min.js", },
Waline 实名评论
{ enable: true, provider: "waline", lazy: true, waline: { serverURL: "https://comments.example.com", login: "force", requiredMeta: ["nick", "mail"], wordLimit: [0, 500], turnstileKey: "0x4AAAAAAA…", }, }
评论组件不出现
按解析规则逐层检查:enable → provider → 必填字段非空(twikoo 的 envId / scriptUrl,giscus 的 repo / repoId / categoryId,waline 的 serverURL / scriptUrl / styleUrl,且 serverURL 须为 http(s) 地址)。任一缺失即静默关闭(设计行为,不报错)。
评论加载拖慢页面
确认 lazy: true(默认已开)。开启后脚本仅在评论区域进入视口时加载。
envId 用什么格式
两种都可以:完整 URL(如 https://your-twikoo.vercel.app,Vercel/Railway 部署常见)或腾讯云环境 ID 字符串。
https://your-twikoo.vercel.app
Giscus 的 repoId / categoryId 从哪里获取
打开 giscus.app,选择仓库与分类后,页面生成的 <script> 标签里就有 data-repo-id 与 data-category-id。
<script>
评论语言跟站点不一致
twikoo.lang、giscus.lang 或 waline.lang 设为 "auto" 时跟随站点语言。强制指定(如 "zh-CN")可覆盖。Waline 用 "jp" 表示日语;站点语言不在其内置列表中时回退英文。
"zh-CN"
"jp"
Waline 能不能自定义图片上传或代码高亮函数
不能。配置以 JSON 写入页面,无法携带函数,imageUploader、highlighter、texRenderer 只接受 true / false;写入函数会在构建时报错而不是静默失效。
版权归属:Dawn6666666
许可证:署名 4.0 国际 (CC-BY-4.0)
评论系统默认全局关闭,由
commentConfig.ts统一管理。当前支持 Twikoo(自建/托管后端)、Giscus(基于 GitHub Discussions 的零后端方案)与 Waline(自建/托管服务端,支持匿名评论、登录、表情与文章表情反应)。遵循零额外负担原则:未开启时不产生任何外部请求、零额外 DOM 占用与零包体积膨胀。提示
按需懒加载策略 建议保持
lazy: true。评论客户端脚本仅在访客滚动至文章底部评论区域时才会按需下载并初始化,保证首屏极速加载与零多余网络开销。配置速览
enablebooleanfalsefalse时不加载评论脚本与 DOMprovider"none"/"twikoo"/"giscus"/"waline""none"lazybooleantruetwikoo.envIdstring""twikoo.scriptUrlstringtwikoo.langstring"auto"auto跟随站点twikoo.placeholderstringgiscus.repostring""owner/repo(必填)giscus.repoIdstring""giscus.categoryIdstring""giscus.categorystring"Announcements"giscus.mapping"pathname"pathname/url/title/og:title/specific/numbergiscus.strictbooleanfalsegiscus.reactionsEnabledbooleantruegiscus.emitMetadatabooleanfalsegiscus.inputPosition"top"/"bottom""bottom"giscus.theme.light/.darkstring"light"/"dark"giscus.langstring"auto"auto跟随站点giscus.scriptUrlstringwaline.serverURLstring""waline.scriptUrl/.styleUrlstringwaline.langstring"auto"auto跟随文章/站点语言(未内置的语言回退en),或 Waline 语言码如zh-CN、jpwaline.locale{ placeholder }placeholder为输入框提示waline.emojiboolean/ 数组truetrue使用 Waline 默认表情包,false关闭,也可填写表情包地址列表waline.commentSorting"latest"/"oldest"/"hottest""latest"waline.meta/.requiredMeta["nick","mail","link"]/[]requiredMeta为空时允许匿名waline.login"enable"/"disable"/"force""enable"waline.wordLimitnumber/[min, max]00不限制waline.pageSizenumber10waline.imageUploader/.highlighter/.texRendererbooleantrue/true/falsewaline.noCopyright/.noRssbooleanfalse/truewaline.recaptchaV3Key/.turnstileKeystring""waline.reactionboolean/ 数组true开启 Twikoo 评论
部署 Twikoo 服务端并获取环境 ID。支持腾讯云 CloudBase、Vercel、Railway 或私有部署。
修改配置:把
enable设为true,provider设为"twikoo",并填入envId:重新构建部署,文章页底部即出现评论组件。
开启 Giscus 评论
Giscus 基于 GitHub Discussions,无需部署任何后端,评论数据存储在你自己的公开仓库中。
准备仓库:创建一个公开 GitHub 仓库,在 Settings → General → Features 中开启 Discussions;安装 giscus App 并授权给该仓库。
获取 ID:打开 giscus.app,选择仓库与 Discussion 分类(推荐 Announcements,仅维护者可开新讨论),记下生成的
data-repo-id与data-category-id。修改配置:把
enable设为true,provider设为"giscus",填入三必填字段:重新构建部署,文章页底部即出现 Giscus 评论区,明暗主题随站点切换自动同步。
开启 Waline 评论
Waline 的评论区沿用主题为 Twikoo 设计的线条样式,评论框中的表情显示为图标,文章表情反应显示在评论区顶部。
部署 Waline 服务端:按 Waline 官方文档 部署(Vercel、Deta、Railway 或私有部署),记下服务端地址。
修改配置:把
enable设为true,provider设为"waline",填入serverURL:重新构建部署,文章页底部即出现 Waline 评论区;客户端 JS 与 CSS 在评论区进入视口后才加载。
笔记
配置以 JSON 形式写入页面,因此
imageUploader、highlighter、texRenderer只接受true/false,填写 Waline 原生的自定义函数会在构建时报错。主题不采集浏览量(Waline 的pageview始终关闭)。自托管客户端或升级版本时,scriptUrl与styleUrl须保持同一版本。生效条件与解析规则
配置解析器在以下任一情况返回
null(评论完全不渲染,零 DOM):enable为falseprovider为"none""twikoo"时:envId或scriptUrl为空"giscus"时:repo、repoId、categoryId任一为空"waline"时:serverURL、scriptUrl、styleUrl任一为空,或serverURL不是 http(s) 地址条件满足时组件才动态加载。配合
lazy: true,评论脚本在读者滚动到文章底部时才请求——不影响首屏性能。单篇控制
frontmatter 中设置
comment: false可对单篇文章关闭评论(全局开关需已启用):关闭后 FAB 的「直达评论」按钮对该文章也零 DOM(见悬浮控制流)。
实战示例
中文博客(本地化评论)
零后端 GitHub 评论
自建 CDN 脚本
Waline 实名评论
常见问题
评论组件不出现
评论加载拖慢页面
envId 用什么格式
Giscus 的 repoId / categoryId 从哪里获取
评论语言跟站点不一致
Waline 能不能自定义图片上传或代码高亮函数
版权所有
版权归属:Dawn6666666
许可证:署名 4.0 国际 (CC-BY-4.0)