|
1 | 1 | <script setup lang="ts"> |
2 | | -import { onMounted, onBeforeUnmount, watch, ref, nextTick, toRef, type Ref } from "vue"; |
3 | | -
|
4 | | -const COMMENT_REPOSITORY = "toothlessdev/toothlessdev.github.io"; |
5 | | -
|
6 | | -const props = defineProps<{ |
7 | | - theme: string; |
8 | | -}>(); |
9 | | -
|
10 | | -const containerRef = ref<HTMLDivElement | null>(null); |
11 | | -
|
12 | | -function mountUtterances(theme: string) { |
13 | | - const container = containerRef.value; |
14 | | -
|
15 | | - if (!container) return; |
16 | | - if (container.querySelector("iframe.utterances-frame")) return; |
17 | | -
|
18 | | - const script = document.createElement("script"); |
19 | | -
|
20 | | - script.src = "https://utteranc.es/client.js"; |
21 | | - script.async = true; |
22 | | - script.crossOrigin = "anonymous"; |
| 2 | +import { computed } from "vue"; |
| 3 | +import { useData } from "vitepress"; |
| 4 | +import Giscus from "@giscus/vue"; |
| 5 | +
|
| 6 | +const GISCUS = { |
| 7 | + repo: "toothlessdev/toothlessdev.github.io", |
| 8 | + repoId: "R_kgDOK8A2Yg", |
| 9 | + category: "General", |
| 10 | + categoryId: "DIC_kwDOK8A2Ys4Cv8uz", |
| 11 | +} as const; |
| 12 | +
|
| 13 | +const { isDark, lang } = useData(); |
| 14 | +
|
| 15 | +const theme = computed(() => (isDark.value ? "dark" : "light")); |
| 16 | +const mapping = "pathname"; |
| 17 | +const uiLang = computed(() => (lang.value?.startsWith("ko") ? "ko" : "en")); |
| 18 | +</script> |
23 | 19 |
|
24 | | - script.setAttribute("repo", COMMENT_REPOSITORY); |
25 | | - script.setAttribute("issue-term", "pathname"); |
26 | | - script.setAttribute("label", "blog-comment"); |
27 | | - script.setAttribute("theme", theme); |
| 20 | +<template> |
| 21 | + <div class="giscus-comments"> |
| 22 | + <Giscus |
| 23 | + class="giscus" |
| 24 | + :repo="GISCUS.repo" |
| 25 | + :repo-id="GISCUS.repoId" |
| 26 | + :category="GISCUS.category" |
| 27 | + :category-id="GISCUS.categoryId" |
| 28 | + :mapping="mapping" |
| 29 | + strict="0" |
| 30 | + reactions-enabled="1" |
| 31 | + emit-metadata="0" |
| 32 | + input-position="top" |
| 33 | + :theme="theme" |
| 34 | + :lang="uiLang" |
| 35 | + crossorigin="anonymous" |
| 36 | + loading="lazy" |
| 37 | + /> |
| 38 | + </div> |
| 39 | +</template> |
28 | 40 |
|
29 | | - container.appendChild(script); |
| 41 | +<style scoped> |
| 42 | +.giscus-comments { |
| 43 | + margin-top: 4rem; |
| 44 | + padding-top: 1rem; |
| 45 | + border-top: 1px solid var(--vp-c-divider); |
30 | 46 | } |
31 | 47 |
|
32 | | -function setUtterancesTheme(theme: string) { |
33 | | - const iframe = containerRef.value?.querySelector<HTMLIFrameElement>("iframe.utterances-frame"); |
34 | | - if (!iframe?.contentWindow) return; |
35 | | - iframe.contentWindow.postMessage({ type: "set-theme", theme }, "https://utteranc.es"); |
| 48 | +.giscus-comments :deep(.giscus), |
| 49 | +.giscus-comments :deep(iframe) { |
| 50 | + margin-top: 0; |
36 | 51 | } |
| 52 | +</style> |
37 | 53 |
|
38 | | -onMounted(async () => { |
39 | | - await nextTick(); |
40 | | - mountUtterances(props.theme); |
41 | | -}); |
42 | | -
|
43 | | -watch(toRef(props, "theme"), (theme) => { |
44 | | - if (!containerRef.value?.querySelector("iframe.utterances-frame")) { |
45 | | - mountUtterances(theme); |
46 | | - return; |
47 | | - } |
48 | | - setUtterancesTheme(theme); |
49 | | -}); |
50 | | -
|
51 | | -onBeforeUnmount(() => { |
52 | | - const container = containerRef.value; |
53 | | - if (!container) return; |
54 | | -
|
55 | | - container.querySelector("iframe.utterances-frame")?.remove(); |
56 | | -}); |
57 | | -</script> |
| 54 | +<style> |
| 55 | +.giscus-frame { |
| 56 | + margin-top: 2rem; |
| 57 | +} |
58 | 58 |
|
59 | | -<template> |
60 | | - <div ref="containerRef" /> |
61 | | -</template> |
| 59 | +.giscus { |
| 60 | + margin-top: 2rem; |
| 61 | +} |
| 62 | +</style> |
0 commit comments