Skip to content

Commit 1089657

Browse files
committed
giscus comments work, dark mode works, unstyled
1 parent 68f3f02 commit 1089657

File tree

12 files changed

+126
-6
lines changed

12 files changed

+126
-6
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
["cx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"]
2828
],
2929
// "gitlens.currentLine.enabled": false,
30-
"cSpell.words": ["astro"]
30+
"cSpell.words": ["astro", "giscus"]
3131
}

docs/working-notes/todo3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,7 @@ write content for home and about
407407
write readme and credits
408408
search with fuse.js astro-paper
409409
fix lint, types, format
410+
put images beside .mdx in content, slug...
411+
style giscus, tags and share
410412
```
411413

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"debounce": "^2.1.0",
4242
"dotenv": "^16.4.5",
4343
"feed": "^4.2.2",
44+
"giscus": "^1.5.0",
4445
"mdast-util-to-string": "^4.0.0",
4546
"nanostores": "^0.10.3",
4647
"object-treeify": "^4.0.1",

src/components/Giscus.astro

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
3+
---
4+
5+
<giscus-widget
6+
id="giscus-comments"
7+
repo="nemanjam/nemanjam.github.io"
8+
repoid="R_kgDOKc-LeA"
9+
category="General"
10+
categoryid="DIC_kwDOKc-LeM4CZ7UL"
11+
mapping="pathname"
12+
term="Comments are welcome"
13+
strict="0"
14+
reactionsenabled="1"
15+
emitmetadata="0"
16+
inputposition="bottom"
17+
theme="light"
18+
lang="en"
19+
crossorigin="anonymous"
20+
loading="lazy"></giscus-widget>
21+
22+
<script>
23+
import 'giscus';
24+
25+
import { SELECTORS } from '@/constants/dom';
26+
import { getCurrentMode } from '@/utils/dom';
27+
28+
const { GISCUS_WIDGET_SELECTOR } = SELECTORS;
29+
30+
// will run only on page load
31+
const giscusWidget = document.querySelector(GISCUS_WIDGET_SELECTOR);
32+
const mode = getCurrentMode();
33+
34+
if (giscusWidget) giscusWidget.setAttribute('theme', mode);
35+
</script>

src/components/PostListMore.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { morePosts } = Astro.props;
1111
---
1212

1313
<div>
14-
<h2>More posts</h2>
14+
<h2 class="b-h2">More posts</h2>
1515
<ul class="grid grid-cols-1 gap-6 sm:grid-cols-2 md:grid-cols-3">
1616
{
1717
morePosts.map((post) => (

src/components/ThemeToggle.astro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import { Icon } from 'astro-icon/components';
1010
</theme-toggle>
1111

1212
<script>
13-
import { getNextTheme } from 'utils/dom';
14-
1513
import { THEME_CONFIG } from '@/constants/themes';
14+
import { getNextTheme, sendModeToGiscus } from '@/utils/dom';
1615

1716
const { CHANGE_EVENT } = THEME_CONFIG;
1817

@@ -42,6 +41,10 @@ import { Icon } from 'astro-icon/components';
4241

4342
// set the aria-checked attribute
4443
// button.setAttribute('aria-checked', String(isDarkMode()));
44+
45+
// send message with delay to Giscus here
46+
// ThemeScript has <script is:inline /> and can't import files
47+
setTimeout(() => sendModeToGiscus(), 50);
4548
},
4649
{ signal }
4750
);

src/constants/dom.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const SELECTORS = {
2+
GISCUS_WIDGET_SELECTOR: '#giscus-comments',
3+
GISCUS_IFRAME_SELECTOR: 'iframe[title="Comments"]',
4+
} as const;

src/pages/blog/[slug].astro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getAllPostsWithReadingTime } from '@/modules/post/common';
55
import { getRandomPosts } from '@/modules/post/random';
66
import { getHeadingsForTableOfContents } from '@/modules/post/table-of-contents';
77
import { default as PostLayout } from '@/layouts/Post.astro';
8+
import Giscus from '@/components/Giscus.astro';
89
import PostListMore from '@/components/PostListMore.astro';
910
import PostMeta from '@/components/PostMeta.astro';
1011
import Share from '@/components/Share.astro';
@@ -110,6 +111,8 @@ const metadata: Metadata = { title, description, image };
110111
<TagList tags={tags} size="lg" class="mb-4 md:mb-6" />
111112

112113
<Share {title} {description} url={`${ROUTES.BLOG}${slug}`} size="md" />
114+
115+
<Giscus />
113116
</Fragment>
114117

115118
<Fragment slot="more-posts">

src/types/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { CATEGORIES } from '@/constants/collections';
22
import type { PAGE_METADATA } from '@/constants/metadata';
33
import type { NAVIGATION_ITEMS } from '@/constants/navigation';
4-
import type { THEMES } from '@/constants/themes';
4+
import type { MODES, THEMES } from '@/constants/themes';
5+
import type { ValueUnion } from '@/types/utils';
56
import type { LocalImageProps } from 'astro:assets';
67

78
export type CategoryType = (typeof CATEGORIES)[number];
@@ -12,6 +13,8 @@ export type NavigationItem = (typeof NAVIGATION_ITEMS)[number];
1213

1314
export type Theme = (typeof THEMES)[number];
1415

16+
export type Mode = ValueUnion<typeof MODES>;
17+
1518
export type ImageSizes = {
1619
FIXED: Record<string, Pick<LocalImageProps, 'width' | 'height'>>;
1720
RESPONSIVE: Record<string, Pick<LocalImageProps, 'widths' | 'sizes'>>;

src/utils/dom.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SELECTORS } from '@/constants/dom';
12
import { DEFAULT_THEMES, MODES, THEME_CONFIG, THEMES } from '@/constants/themes';
23

34
import type { Theme } from '@/types/constants';
@@ -33,3 +34,22 @@ export const getNextTheme = () => {
3334
const nextIndex = (currentIndex + 1) % THEMES.length;
3435
return THEMES[nextIndex];
3536
};
37+
38+
/*-------------------------------- giscus dark/light mode ------------------------------*/
39+
40+
const { GISCUS_WIDGET_SELECTOR, GISCUS_IFRAME_SELECTOR } = SELECTORS;
41+
42+
export const sendModeToGiscus = (): void => {
43+
const giscusIframeUrl = 'https://giscus.app';
44+
45+
const shadowHost = document.querySelector(GISCUS_WIDGET_SELECTOR);
46+
const shadowRoot = shadowHost?.shadowRoot;
47+
if (!shadowRoot) return;
48+
49+
const iframe = shadowRoot.querySelector(GISCUS_IFRAME_SELECTOR) as HTMLIFrameElement;
50+
if (!iframe?.contentWindow) return;
51+
52+
const mode = getCurrentMode();
53+
54+
iframe.contentWindow.postMessage({ giscus: { setConfig: { theme: mode } } }, giscusIframeUrl);
55+
};

0 commit comments

Comments
 (0)