|
| 1 | +import BLOG from '@/blog.config' |
| 2 | +import { useEffect } from 'react' |
| 3 | +import { useRouter } from 'next/router' |
| 4 | + |
| 5 | +const Giscus = () => { |
| 6 | + const router = useRouter() |
| 7 | + const { asPath } = router |
| 8 | + |
| 9 | + useEffect(() => { |
| 10 | + const theme = |
| 11 | + BLOG.appearance === 'auto' |
| 12 | + ? 'preferred_color_scheme' |
| 13 | + : BLOG.appearance === 'light' |
| 14 | + ? 'light' |
| 15 | + : 'dark' |
| 16 | + |
| 17 | + const script = document.createElement('script') |
| 18 | + script.src = 'https://giscus.app/client.js' |
| 19 | + script.setAttribute('data-repo', BLOG.comment.giscusConfig.repo) |
| 20 | + script.setAttribute('data-repo-id', BLOG.comment.giscusConfig.repoId) |
| 21 | + script.setAttribute('data-category', BLOG.comment.giscusConfig.category) |
| 22 | + script.setAttribute('data-category-id', BLOG.comment.giscusConfig.categoryId) |
| 23 | + script.setAttribute('data-mapping', BLOG.comment.giscusConfig.mapping) |
| 24 | + script.setAttribute('data-strict', '0') |
| 25 | + script.setAttribute('data-reactions-enabled', BLOG.comment.giscusConfig.reactionsEnabled) |
| 26 | + script.setAttribute('data-emit-metadata', BLOG.comment.giscusConfig.emitMetadata) |
| 27 | + script.setAttribute('data-input-position', BLOG.comment.giscusConfig.inputPosition) |
| 28 | + script.setAttribute('data-theme', theme) |
| 29 | + script.setAttribute('data-lang', BLOG.comment.giscusConfig.lang) |
| 30 | + script.setAttribute('crossorigin', BLOG.comment.giscusConfig.crossorigin) |
| 31 | + script.setAttribute('async', true) |
| 32 | + |
| 33 | + const anchor = document.getElementById('comments') |
| 34 | + if (anchor) { |
| 35 | + anchor.innerHTML = '' |
| 36 | + anchor.appendChild(script) |
| 37 | + } |
| 38 | + |
| 39 | + return () => { |
| 40 | + if (anchor) { |
| 41 | + anchor.innerHTML = '' |
| 42 | + } |
| 43 | + } |
| 44 | + }, [asPath]) |
| 45 | + |
| 46 | + return ( |
| 47 | + <div |
| 48 | + id='comments' |
| 49 | + className='mt-10' |
| 50 | + /> |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +export default Giscus |
| 55 | + |
0 commit comments