Skip to content

Commit

Permalink
feat: fixed comment component bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Axi404 committed Oct 17, 2024
1 parent 70803fc commit f2d4530
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const isHomepage = Astro.props.slug === '';
{
true && (
<a class="kudos sl-flex" href="https://starlight.astro.build">
<Icon name={'starlight'} /> {Astro.locals.t('builtWithStarlight.label')}
<Icon name={'starlight'} />
</a>
)
}
Expand Down
11 changes: 7 additions & 4 deletions src/components/PostComments.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<script>
// 获取用户的主题偏好
const getPreferredTheme = () => {
const getPreferredTheme = (): string | null => {
if (typeof localStorage !== "undefined" && localStorage.getItem("starlight-theme")) {
const theme = localStorage.getItem("starlight-theme");
return theme === 'auto' ? (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light") : theme;
Expand All @@ -11,7 +11,7 @@
};

// 加载 giscus 评论组件
const loadGiscus = (theme) => {
const loadGiscus = (theme: string) => {
const script = document.createElement('script');
script.src = 'https://giscus.app/client.js';
script.setAttribute('data-repo', 'CS-BAOYAN/CS-BAOYAN-2025');
Expand All @@ -29,12 +29,15 @@
script.setAttribute('crossorigin', 'anonymous');
script.async = true;

document.querySelector('.giscus').appendChild(script);
const giscusContainer = document.querySelector('.giscus');
if (giscusContainer) {
giscusContainer.appendChild(script);
}
};

// 初始化 giscus 评论组件
const theme = getPreferredTheme();
loadGiscus(theme);
loadGiscus(theme ?? 'light'); // 如果 theme 为 null,则使用 'light' 作为默认值

// 监听系统主题变化
matchMedia(`(prefers-color-scheme: light)`).addEventListener('change', () => {
Expand Down

0 comments on commit f2d4530

Please sign in to comment.