mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 19:51:07 +02:00
test: add giscus comment
This commit is contained in:
parent
d12e235a23
commit
27c6746691
6 changed files with 194 additions and 92 deletions
|
@ -2,93 +2,89 @@
|
|||
import { defaultLocale, themeConfig } from '@/config'
|
||||
import { giscusLocaleMap } from '@/i18n/config'
|
||||
|
||||
// Get the language code of Giscus
|
||||
function getGiscusLang(currentPath: string, defaultLocale: string): string {
|
||||
// Extract language code from path
|
||||
const pathLang = Object.keys(giscusLocaleMap).find(code =>
|
||||
currentPath.startsWith(`/${code}/`),
|
||||
)
|
||||
// Return found path language or default language
|
||||
const lang = pathLang || defaultLocale
|
||||
return giscusLocaleMap[lang as keyof typeof giscusLocaleMap]
|
||||
}
|
||||
|
||||
const giscusLang = getGiscusLang(Astro.url.pathname, defaultLocale)
|
||||
const {
|
||||
repo = "",
|
||||
repoID = "",
|
||||
category = "",
|
||||
categoryID = "",
|
||||
mapping = "pathname",
|
||||
inputPosition = "top",
|
||||
loading = "lazy",
|
||||
} = themeConfig.comment?.giscus ?? {};
|
||||
repo = '',
|
||||
repoID = '',
|
||||
category = '',
|
||||
categoryID = '',
|
||||
mapping = 'pathname',
|
||||
strict = '0',
|
||||
reactionsEnabled = '1',
|
||||
emitMetadata = '0',
|
||||
inputPosition = 'bottom',
|
||||
} = themeConfig.comment?.giscus ?? {}
|
||||
|
||||
const siteUrl = themeConfig.site.url
|
||||
const shouldRender = Boolean(repo && repoID && categoryID)
|
||||
---
|
||||
|
||||
<div class="giscus mt-16" id="giscus-container"></div>
|
||||
{shouldRender && (
|
||||
<div
|
||||
id="giscus"
|
||||
class="giscus mt-16"
|
||||
></div>
|
||||
|
||||
<script
|
||||
is:inline
|
||||
define:vars={{
|
||||
repo,
|
||||
repoID,
|
||||
category,
|
||||
categoryID,
|
||||
mapping,
|
||||
inputPosition,
|
||||
giscusLang,
|
||||
loading,
|
||||
}}
|
||||
>
|
||||
const host = window.location.origin;
|
||||
<script
|
||||
is:inline
|
||||
define:vars={{
|
||||
repo,
|
||||
repoID,
|
||||
category,
|
||||
categoryID,
|
||||
mapping,
|
||||
strict,
|
||||
reactionsEnabled,
|
||||
emitMetadata,
|
||||
inputPosition,
|
||||
giscusLocaleMap,
|
||||
defaultLocale,
|
||||
siteUrl,
|
||||
}}
|
||||
>
|
||||
function setupGiscus() {
|
||||
const giscusContainer = document.getElementById('giscus')
|
||||
if (!giscusContainer)
|
||||
return
|
||||
|
||||
function loadGiscus() {
|
||||
const existingScript = document.querySelector("script[src*='giscus.app']");
|
||||
if (existingScript) {
|
||||
existingScript.remove();
|
||||
giscusContainer.innerHTML = ''
|
||||
|
||||
const currentPath = window.location.pathname
|
||||
const pathLang = Object.keys(giscusLocaleMap).find(code =>
|
||||
currentPath.startsWith(`/${code}/`),
|
||||
)
|
||||
const lang = pathLang || defaultLocale
|
||||
const currentGiscusLang = giscusLocaleMap[lang]
|
||||
|
||||
const script = document.createElement('script')
|
||||
script.src = 'https://giscus.app/client.js'
|
||||
script.crossOrigin = 'anonymous'
|
||||
script.async = true
|
||||
|
||||
// Set up basic attributes
|
||||
const dataAttributes = {
|
||||
repo,
|
||||
repoId: repoID,
|
||||
categoryId: categoryID,
|
||||
mapping,
|
||||
strict,
|
||||
reactionsEnabled,
|
||||
emitMetadata,
|
||||
inputPosition,
|
||||
theme: `${siteUrl}/vendors/giscus/theme.css`,
|
||||
lang: currentGiscusLang,
|
||||
}
|
||||
const giscusContainer = document.getElementById("giscus-container");
|
||||
if (giscusContainer) giscusContainer.innerHTML = "";
|
||||
// 创建新脚本
|
||||
const theme = document.documentElement.classList.contains("dark")
|
||||
? host + "/css/giscus_dark.css"
|
||||
: host + "/css/giscus_light.css";
|
||||
const script = document.createElement("script");
|
||||
script.src = "https://giscus.app/client.js";
|
||||
script.setAttribute("data-repo", repo);
|
||||
script.setAttribute("data-repo-id", repoID);
|
||||
script.setAttribute("data-category", category);
|
||||
script.setAttribute("data-category-id", categoryID);
|
||||
script.setAttribute("data-mapping", mapping);
|
||||
script.setAttribute("data-strict", "0");
|
||||
script.setAttribute("data-reactions-enabled", "1");
|
||||
script.setAttribute("data-theme", theme);
|
||||
script.setAttribute("data-emit-metadata", "0");
|
||||
script.setAttribute("data-input-position", inputPosition);
|
||||
script.setAttribute("data-lang", giscusLang);
|
||||
script.setAttribute("data-loading", loading);
|
||||
script.crossOrigin = "anonymous";
|
||||
script.async = true;
|
||||
giscusContainer.appendChild(script);
|
||||
|
||||
// Add category conditionally
|
||||
if (category)
|
||||
dataAttributes.category = category
|
||||
|
||||
// Apply all attributes
|
||||
Object.assign(script.dataset, dataAttributes)
|
||||
|
||||
giscusContainer.appendChild(script)
|
||||
}
|
||||
|
||||
function changeGiscusTheme() {
|
||||
const iframe = document.querySelector("iframe.giscus-frame");
|
||||
if (!iframe || iframe.classList.contains("giscus-frame--loading")) {
|
||||
// 脚本未加载场景
|
||||
loadGiscus();
|
||||
return;
|
||||
};
|
||||
const theme = document.documentElement.classList.contains("dark")
|
||||
? host + "/css/giscus_dark.css"
|
||||
: host + "/css/giscus_light.css";
|
||||
iframe.contentWindow.postMessage(
|
||||
{ giscus: { setConfig: { theme } } },
|
||||
"https://giscus.app",
|
||||
);
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", loadGiscus);
|
||||
document.addEventListener("theme-changed", changeGiscusTheme);
|
||||
document.addEventListener("astro:page-load", loadGiscus);
|
||||
</script>
|
||||
setupGiscus()
|
||||
document.addEventListener('astro:page-load', setupGiscus)
|
||||
</script>
|
||||
)}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue