初始提交:时之刃的小站
基于 VermilionVoid(朱墨留白)主题的个人博客。 - 站点配置集中在 src/data/site.ts 与 src/data/profile.ts - 30 篇文章、8 条偶得、相册 - 评论使用自托管 twikoo - 部署走 deploy.sh(rsync 到树莓派 + 云服务器)
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
---
|
||||
import { commentConfig } from "@/data/comments"
|
||||
|
||||
const envId =
|
||||
(import.meta.env.PUBLIC_TWIKOO_ENV_ID as string | undefined) || commentConfig.twikooEnvId
|
||||
const isEnabled = Boolean(envId)
|
||||
---
|
||||
|
||||
{isEnabled && (
|
||||
<section class="mt-8 pt-0 pb-12">
|
||||
<div class="px-6">
|
||||
<div class="max-w-3xl mx-auto">
|
||||
<div
|
||||
id="twikoo"
|
||||
data-env-id={envId}
|
||||
data-lang={commentConfig.lang}
|
||||
data-version={commentConfig.version}
|
||||
data-strip-trailing-slash={commentConfig.stripTrailingSlash ? "true" : "false"}
|
||||
></div>
|
||||
<script is:inline>
|
||||
(() => {
|
||||
const SCRIPT_ID = "twikoo-script"
|
||||
|
||||
const loadScript = (version) => {
|
||||
if (window.twikoo) return Promise.resolve()
|
||||
|
||||
const existing = document.getElementById(SCRIPT_ID)
|
||||
if (existing) {
|
||||
if (existing.dataset.loaded === "true") return Promise.resolve()
|
||||
return new Promise((resolve, reject) => {
|
||||
existing.addEventListener("load", () => resolve(), { once: true })
|
||||
existing.addEventListener("error", () => {
|
||||
reject(new Error("Failed to load twikoo"))
|
||||
}, { once: true })
|
||||
})
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const script = document.createElement("script")
|
||||
script.id = SCRIPT_ID
|
||||
script.src = `https://cdn.jsdelivr.net/npm/twikoo@${version}/dist/twikoo.min.js`
|
||||
script.async = true
|
||||
script.onload = () => {
|
||||
script.dataset.loaded = "true"
|
||||
resolve()
|
||||
}
|
||||
script.onerror = () => reject(new Error("Failed to load twikoo"))
|
||||
document.head.appendChild(script)
|
||||
})
|
||||
}
|
||||
|
||||
// 评论归属的路径。去掉结尾斜杠,与站点历史评论数据保持一致。
|
||||
const getCommentPath = (stripTrailingSlash) => {
|
||||
const pathname = window.location.pathname
|
||||
if (stripTrailingSlash && pathname.length > 1 && pathname.endsWith("/")) {
|
||||
return pathname.slice(0, -1)
|
||||
}
|
||||
return pathname
|
||||
}
|
||||
|
||||
const mountComments = async () => {
|
||||
const container = document.getElementById("twikoo")
|
||||
if (!container) return
|
||||
|
||||
const envId = container.dataset.envId
|
||||
if (!envId) return
|
||||
|
||||
try {
|
||||
await loadScript(container.dataset.version)
|
||||
// 切页时容器已被换成新节点,清空可避免重复渲染
|
||||
container.innerHTML = ""
|
||||
await window.twikoo.init({
|
||||
envId,
|
||||
el: "#twikoo",
|
||||
lang: container.dataset.lang || "zh-CN",
|
||||
path: getCommentPath(container.dataset.stripTrailingSlash === "true"),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("[twikoo] 初始化失败:", error)
|
||||
}
|
||||
}
|
||||
|
||||
mountComments()
|
||||
// 本站启用了 Astro 的 ClientRouter,切页后需要重新挂载
|
||||
document.addEventListener("astro:page-load", mountComments)
|
||||
})()
|
||||
</script>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
Reference in New Issue
Block a user