390 字
2 分钟
Astro添加Artalk评论组件
AI 智能摘要
AI 正在生成文章简介,请稍候...
安装Artalk评论组件
pnpm add artalk添加Artalk配置文件
import type { ExpressiveCodeConfig, LicenseConfig, ArtalkConfig, NavBarConfig, ProfileConfig, SiteConfig,} from "./types/config";};
// 自己根据内容定义ArtalkConfig类export const arTalkConfig: ArtalkConfig = { enable: true, site: "青漫手记", url: "https://talk.16c.top",};
export const expressiveCodeConfig: ExpressiveCodeConfig = {创建Artalk评论组件
---import 'artalk/Artalk.css'---
<div id="artalk"></div>
<script> import Artalk from 'artalk' import { arTalkConfig } from "../../config";
let artalk; let observer;
// console.log("加载评论系统") try{ initArtalk(); }catch(e){ console.log("加载评论功能失败") }
function bindThemeObserver() { if (observer) observer.disconnect();
observer = new MutationObserver(() => { if (!artalk) return; const isDark = document.documentElement.classList.contains('dark'); artalk.setDarkMode(isDark); });
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'], }); }
function initArtalk() { // console.log("初始化评论") const el = document.getElementById("artalk"); if (!el) return;
if (artalk) { artalk.destroy(); artalk = null; }
artalk = Artalk.init({ el, pageKey: location.pathname, pageTitle: document.title, server: arTalkConfig.url, site: arTalkConfig.site, darkMode: false, // 初始值无所谓,后面 observer 会立刻更新 });
// 开启主题监听 bindThemeObserver();
// 初始化时同步一次 const isDark = document.documentElement.classList.contains('dark'); artalk.setDarkMode(isDark); }
// ----------------------- // 页面加载 & 导航事件 // ----------------------- const initEvents = ['DOMContentLoaded', 'astro:after-swap', 'pjax:complete', 'swup:contentReplaced'] for (const e of initEvents) { document.addEventListener(e, () => { // console.log(`触发事件: ${e}`, document.location.pathname) if(document.location.pathname != '/' && document.location.pathname != '/index.html'){ initArtalk() } }) }
window.addEventListener("beforeunload", () => { if (artalk) artalk.destroy(); if (observer) observer.disconnect(); });</script>使用Artalk评论组件
导入组件
import License from "@components/misc/License.astro";import Markdown from "@components/misc/Markdown.astro";import Artalk from "@components/misc/Artalk.astro";import I18nKey from "@i18n/i18nKey";导入配置
import { Icon } from "astro-icon/components";import { licenseConfig, arTalkConfig } from "src/config";import ImageWrapper from "../components/misc/ImageWrapper.astro";import PostMetadata from "../components/PostMeta.astro";使用组件
{licenseConfig.enable && <License title={entry.data.title} slug={entry.slug} pubDate={entry.data.published} class="mb-6 rounded-xl license-container onload-animation"></License>} </div> </div> <!-- 评论区 --> {arTalkConfig.enable && <div class="flex w-full rounded-[var(--radius-large)] overflow-hidden relative mb-4"> <div id="post-container" class:list={["card-base z-10 px-6 md:px-9 pt-6 pb-4 relative w-full ", {} ]}> <Artalk class="mb-6 rounded-xl license-container onload-animation"></Artalk> </div> </div>} <div class="flex flex-col md:flex-row justify-between mb-4 gap-4 overflow-hidden w-full"> <a href={entry.data.nextSlug ? getPostUrlBySlug(entry.data.nextSlug) : "#"}