test: add giscus comment

This commit is contained in:
radishzzz 2025-05-30 14:34:24 +01:00
parent d12e235a23
commit 27c6746691
6 changed files with 194 additions and 92 deletions

99
public/vendors/giscus/theme.css vendored Normal file
View file

@ -0,0 +1,99 @@
/*! MIT License
* Copyright (c) 2018 GitHub Inc.
* https://github.com/primer/primitives/blob/main/LICENSE
*/
main {
--color-prettylights-syntax-comment: #6e7781;
--color-prettylights-syntax-constant: #0550ae;
--color-prettylights-syntax-entity: #8250df;
--color-prettylights-syntax-storage-modifier-import: #24292f;
--color-prettylights-syntax-entity-tag: #116329;
--color-prettylights-syntax-keyword: #cf222e;
--color-prettylights-syntax-string: #0a3069;
--color-prettylights-syntax-variable: #953800;
--color-prettylights-syntax-brackethighlighter-unmatched: #82071e;
--color-prettylights-syntax-invalid-illegal-text: #f6f8fa;
--color-prettylights-syntax-invalid-illegal-bg: #82071e;
--color-prettylights-syntax-carriage-return-text: #f6f8fa;
--color-prettylights-syntax-carriage-return-bg: #cf222e;
--color-prettylights-syntax-string-regexp: #116329;
--color-prettylights-syntax-markup-list: #3b2300;
--color-prettylights-syntax-markup-heading: #0550ae;
--color-prettylights-syntax-markup-italic: #24292f;
--color-prettylights-syntax-markup-bold: #24292f;
--color-prettylights-syntax-markup-deleted-text: #82071e;
--color-prettylights-syntax-markup-deleted-bg: #ffebe9;
--color-prettylights-syntax-markup-inserted-text: #116329;
--color-prettylights-syntax-markup-inserted-bg: #dafbe1;
--color-prettylights-syntax-markup-changed-text: #953800;
--color-prettylights-syntax-markup-changed-bg: #ffd8b5;
--color-prettylights-syntax-markup-ignored-text: #eaeef2;
--color-prettylights-syntax-markup-ignored-bg: #0550ae;
--color-prettylights-syntax-meta-diff-range: #8250df;
--color-prettylights-syntax-brackethighlighter-angle: #57606a;
--color-prettylights-syntax-sublimelinter-gutter-mark: #8c959f;
--color-prettylights-syntax-constant-other-reference-link: #0a3069;
--color-btn-text: #24292f;
--color-btn-bg: #f6f8fa;
--color-btn-border: rgb(31 35 40 / 15%);
--color-btn-shadow: 0 1px 0 rgb(31 35 40 / 4%);
--color-btn-inset-shadow: inset 0 1px 0 rgb(255 255 255 / 25%);
--color-btn-hover-bg: #f3f4f6;
--color-btn-hover-border: rgb(31 35 40 / 15%);
--color-btn-active-bg: hsl(220deg 14% 93% / 100%);
--color-btn-active-border: rgb(31 35 40 / 15%);
--color-btn-selected-bg: hsl(220deg 14% 94% / 100%);
--color-btn-primary-text: #fff;
--color-btn-primary-bg: #1f883d;
--color-btn-primary-border: rgb(31 35 40 / 15%);
--color-btn-primary-shadow: 0 1px 0 rgb(31 35 40 / 10%);
--color-btn-primary-inset-shadow: inset 0 1px 0 rgb(255 255 255 / 3%);
--color-btn-primary-hover-bg: #1a7f37;
--color-btn-primary-hover-border: rgb(31 35 40 / 15%);
--color-btn-primary-selected-bg: hsl(137deg 66% 28% / 100%);
--color-btn-primary-selected-shadow: inset 0 1px 0 rgb(0 45 17 / 20%);
--color-btn-primary-disabled-text: rgb(255 255 255 / 80%);
--color-btn-primary-disabled-bg: #94d3a2;
--color-btn-primary-disabled-border: rgb(31 35 40 / 15%);
--color-action-list-item-default-hover-bg: rgb(208 215 222 / 32%);
--color-segmented-control-bg: #eaeef2;
--color-segmented-control-button-bg: #fff;
--color-segmented-control-button-selected-border: #8c959f;
--color-fg-default: #1F2328;
--color-fg-muted: #656d76;
--color-fg-subtle: #6e7781;
--color-canvas-default: #fff;
--color-canvas-overlay: #fff;
--color-canvas-inset: #f6f8fa;
--color-canvas-subtle: #f6f8fa;
--color-border-default: #d0d7de;
--color-border-muted: hsl(210deg 18% 87% / 100%);
--color-neutral-muted: rgb(175 184 193 / 20%);
--color-accent-fg: #0969da;
--color-accent-emphasis: #0969da;
--color-accent-muted: rgb(84 174 255 / 40%);
--color-accent-subtle: #ddf4ff;
--color-success-fg: #1a7f37;
--color-attention-fg: #9a6700;
--color-attention-muted: rgb(212 167 44 / 40%);
--color-attention-subtle: #fff8c5;
--color-danger-fg: #d1242f;
--color-danger-muted: rgb(255 129 130 / 40%);
--color-danger-subtle: #ffebe9;
--color-primer-shadow-inset: inset 0 1px 0 rgb(208 215 222 / 20%);
--color-scale-gray-1: #eaeef2;
--color-scale-blue-1: #b6e3ff;
/*! Extensions from @primer/css/alerts/flash.scss */
--color-social-reaction-bg-hover: var(--color-scale-gray-1);
--color-social-reaction-bg-reacted-hover: var(--color-scale-blue-1);
}
main .pagination-loader-container {
background-image: url("https://github.com/images/modules/pulls/progressive-disclosure-line.svg");
}
main .gsc-loading-image {
background-image: url("https://github.githubassets.com/images/mona-loading-default.gif");
}

View file

@ -2,93 +2,89 @@
import { defaultLocale, themeConfig } from '@/config' import { defaultLocale, themeConfig } from '@/config'
import { giscusLocaleMap } from '@/i18n/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 { const {
repo = "", repo = '',
repoID = "", repoID = '',
category = "", category = '',
categoryID = "", categoryID = '',
mapping = "pathname", mapping = 'pathname',
inputPosition = "top", strict = '0',
loading = "lazy", reactionsEnabled = '1',
} = themeConfig.comment?.giscus ?? {}; 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 <script
is:inline is:inline
define:vars={{ define:vars={{
repo, repo,
repoID, repoID,
category, category,
categoryID, categoryID,
mapping, mapping,
inputPosition, strict,
giscusLang, reactionsEnabled,
loading, emitMetadata,
}} inputPosition,
> giscusLocaleMap,
const host = window.location.origin; defaultLocale,
siteUrl,
}}
>
function setupGiscus() {
const giscusContainer = document.getElementById('giscus')
if (!giscusContainer)
return
function loadGiscus() { giscusContainer.innerHTML = ''
const existingScript = document.querySelector("script[src*='giscus.app']");
if (existingScript) { const currentPath = window.location.pathname
existingScript.remove(); 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 = ""; // Add category conditionally
// 创建新脚本 if (category)
const theme = document.documentElement.classList.contains("dark") dataAttributes.category = category
? host + "/css/giscus_dark.css"
: host + "/css/giscus_light.css"; // Apply all attributes
const script = document.createElement("script"); Object.assign(script.dataset, dataAttributes)
script.src = "https://giscus.app/client.js";
script.setAttribute("data-repo", repo); giscusContainer.appendChild(script)
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);
} }
function changeGiscusTheme() { setupGiscus()
const iframe = document.querySelector("iframe.giscus-frame"); document.addEventListener('astro:page-load', setupGiscus)
if (!iframe || iframe.classList.contains("giscus-frame--loading")) { </script>
// 脚本未加载场景 )}
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>

View file

@ -2,7 +2,14 @@
import { defaultLocale, themeConfig } from '@/config' import { defaultLocale, themeConfig } from '@/config'
import { walineLocaleMap } from '@/i18n/config' import { walineLocaleMap } from '@/i18n/config'
const { waline: { serverURL = '', emoji = [], search = false, imageUploader = false } = {} } = themeConfig.comment ?? {} const {
waline: {
serverURL = '',
emoji = [],
search = false,
imageUploader = false,
} = {},
} = themeConfig.comment ?? {}
--- ---
<div <div
@ -25,7 +32,7 @@ const { waline: { serverURL = '', emoji = [], search = false, imageUploader = fa
> >
import { init } from '/vendors/waline/waline.js' import { init } from '/vendors/waline/waline.js'
function initWaline() { function setupWaline() {
const currentPath = window.location.pathname const currentPath = window.location.pathname
const pathLang = Object.keys(walineLocaleMap).find(code => const pathLang = Object.keys(walineLocaleMap).find(code =>
currentPath.startsWith(`/${code}/`), currentPath.startsWith(`/${code}/`),
@ -50,8 +57,8 @@ function initWaline() {
}) })
} }
initWaline() setupWaline()
document.addEventListener('astro:page-load', initWaline) document.addEventListener('astro:page-load', setupWaline)
</script> </script>
<!-- Custom CSS Styles >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> --> <!-- Custom CSS Styles >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->

View file

@ -1,6 +1,6 @@
--- ---
// import Disqus from '@/components/Comments/Disqus.astro' // import Disqus from '@/components/Comments/Disqus.astro'
// import Giscus from '@/components/Comments/Giscus.astro' import Giscus from '@/components/Comments/Giscus.astro'
// import Twikoo from '@/components/Comments/Twikoo.astro' // import Twikoo from '@/components/Comments/Twikoo.astro'
import Waline from '@/components/Comments/Waline.astro' import Waline from '@/components/Comments/Waline.astro'
import { themeConfig } from '@/config' import { themeConfig } from '@/config'
@ -12,8 +12,8 @@ const enableComments = themeConfig.comment?.enabled ?? false
// const showDisqus = enableComments && disqusShortname.trim() !== '' // const showDisqus = enableComments && disqusShortname.trim() !== ''
// Giscus // Giscus
// const giscusRepo = themeConfig.comment?.giscus?.repo || '' const giscusRepo = themeConfig.comment?.giscus?.repo || ''
// const showGiscus = enableComments && giscusRepo.trim() !== '' const showGiscus = enableComments && giscusRepo.trim() !== ''
// Twikoo // Twikoo
// const twikooEnvId = themeConfig.comment?.twikoo?.envId || '' // const twikooEnvId = themeConfig.comment?.twikoo?.envId || ''
@ -25,6 +25,6 @@ const showWaline = enableComments && walineURL.trim() !== ''
--- ---
<!-- {showDisqus && <Disqus />} --> <!-- {showDisqus && <Disqus />} -->
<!-- {showGiscus && <Giscus />} --> {showGiscus && <Giscus />}
<!-- {showTwikoo && <Twikoo />} --> <!-- {showTwikoo && <Twikoo />} -->
{showWaline && <Waline />} {showWaline && <Waline />}

View file

@ -75,7 +75,7 @@ export const themeConfig: ThemeConfig = {
// https://waline.js.org/en/ // https://waline.js.org/en/
waline: { waline: {
// server url // server url
serverURL: 'https://retypeset-comment.radishzz.cc', serverURL: '',
// emoji url // emoji url
emoji: [ emoji: [
'https://unpkg.com/@waline/emojis@1.2.0/tw-emoji', 'https://unpkg.com/@waline/emojis@1.2.0/tw-emoji',

View file

@ -36,7 +36,7 @@ export const giscusLocaleMap: Record<string, string> = {
'en': 'en', 'en': 'en',
'es': 'es', 'es': 'es',
'fr': 'fr', 'fr': 'fr',
'ja': 'jp', 'ja': 'ja',
'ko': 'ko', 'ko': 'ko',
'pl': 'pl', 'pl': 'pl',
'pt': 'pt', 'pt': 'pt',