Skip to content
Donghai's Blog
Go back

AstroPaper主题添加Waline评论

记录为 Astro Paper 主题集成 Waline 评论系统的完整步骤。

Table of contents

Open Table of contents

步骤 1:部署 Waline 服务端

访问:部署Waline服务端

步骤 2:注册管理员账号

部署完成后,访问 https://waline.yourdomain.com/ui,注册第一个账号,这个账号会自动成为管理员,可以登录管理后台审核评论、查看数据统计。

NOTE

如果点击“注册”提示:403 Forbidden 错误,请检查 SECURE_DOMAINSSITE_URL 环境变量。例如我的博客地址是 mysite.com,Waline评论地址是 waline.mysite.com,那么这两个环境变量应该这样填写:SECURE_DOMAINS=mysite.com,comments.mysite.comSITE_URL=mysite.com

步骤 3:集成到 Astro paper

(1)安装 Waline 客户端

npm install @waline/client

(2)创建 Waline 组件

---

---

<div id="waline"></div>

<script>
  import { init, type WalineInstance } from "@waline/client";
  import "@waline/client/waline.css";
  import { any } from "astro:schema";

  function loadWaline() {
    const walineInstance = init({
      el: "#waline",
      serverURL: "https://xxx.xxx.com", // 改成你的Waline域名
      path: window.location.pathname,
      lang: "zh-CN",
      dark: 'html[data-theme="dark"]', // 自动适配暗色模式
      imageUploader: false, // 关闭图片上传功能
      login: "disable", // 禁止第三方登录
      search: false, // 关闭表情包搜索
      emoji: [
        "//unpkg.com/@waline/emojis@latest/weibo",
        "//unpkg.com/@waline/emojis@latest/bilibili",
      ],
      meta: ["nick", "mail"],
      requiredMeta: ["nick"], // 设置昵称必填
      pageSize: 10,
    });

    return walineInstance;
  }

  // 存储实例,方便页面切换时销毁
  /** @type {import('@waline/client').WalineInstance | null} */
  let walineInstance: WalineInstance | null = null;

  // 页面首次加载
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", () => {
      walineInstance = loadWaline();
    });
  } else {
    walineInstance = loadWaline();
  }

  // 【关键】视图过渡路由兼容
  document.addEventListener("astro:page-load", () => {
    if (walineInstance && walineInstance.destroy) {
      walineInstance.destroy();
    }
    walineInstance = loadWaline();
  });
</script>

<style>
  #waline {
    margin-top: 2rem;
  }
</style>src/components/Waline.astro

步骤 4:在文章中引入组件

</article>
<!-- 评论区 -->
<Waline />
<hr class="my-8 border-dashed" />src/layouts/PostDetails.astro

常见问题

  1. 注册管理员账号时提示:403 Forbidden
  1. 生产环境(npm run build 后)无法提交评论
  1. 暗色模式不生效

Share this post on:

Next Post
Dynamics 365集中视图/聚焦视图/Focused Vie
BlogsClub Meo Forever Blog