Skip to content

前端配置

这里仅提供一套开箱即用的方案,如果是个人开发者可以根据 API 文档自行编写前端评论组件。

接口 API

初始化

在初始化 CWDComments 实例时,可以传入以下配置参数:

html
<script src="https://cwd-comments.zishu.me/cwd-comments.js"></script>
html
<script>
  const comments = new CWDComments({
    el: '#comments', // 容器 id
    apiBaseUrl: 'https://your-api.example.com', // 你部署的api地址
    postSlug: 'my-post', // 当前页面路径,可使用博客程序支持的 url 模板路径
  });
  comments.mount();
</script>

参数说明

参数类型必填默认值说明
elstring | HTMLElement-挂载元素选择器或 DOM 元素
apiBaseUrlstring-API 基础地址
postSlugstring-文章唯一标识符
postTitlestring-文章标题,用于邮件通知
postUrlstring-文章 URL,用于邮件通知
theme'light' | 'dark''light'主题模式
pageSizenumber20每页显示评论数
avatarPrefixstringhttps://gravatar.com/avatar头像服务前缀
adminEmailstring-博主邮箱,用于显示博主标识
adminBadgestring博主博主标识文字

头像服务前缀

常用的 Gravatar 镜像服务:

服务前缀地址
Gravatar 官方https://gravatar.com/avatar
Cravatar (国内)https://cravatar.cn/avatar
自定义镜像https://your-mirror.com/avatar

实例方法

方法说明
mount()挂载组件到 DOM
unmount()卸载组件
updateConfig(config)更新配置(支持动态切换主题等)
getConfig()获取当前配置

使用示例

javascript
// 动态切换主题
comments.updateConfig({ theme: 'dark' });

// 切换文章
comments.updateConfig({ postSlug: 'another-post' });

基于 Cloudflare Workers 构建