Skip to content

Commit

Permalink
feat: add giscus support (#92)
Browse files Browse the repository at this point in the history
* feat: add giscus support #89

* Update comment.html

* Update giscus.html

* Update giscus.html

---------

Co-authored-by: Xin <[email protected]>
  • Loading branch information
iju707 and imfing authored Sep 26, 2023
1 parent 3c4ede9 commit 6a19ac3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions layouts/_default/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>{{ .Title }}</h1>
</div>
<div class="mt-16"></div>
{{ partial "components/last-updated.html" . }}
{{ partial "components/comment.html" . }}
</main>
</article>
</div>
Expand Down
1 change: 1 addition & 0 deletions layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1 class="text-center mt-2 text-4xl font-bold tracking-tight text-slate-900 dar
<div class="content">
{{ .Content }}
</div>
{{ partial "components/comment.html" . }}
</main>
</article>
</div>
Expand Down
1 change: 1 addition & 0 deletions layouts/blog/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ <h1 class="mt-2 text-4xl font-bold tracking-tight text-slate-900 dark:text-slate
{{ partial "components/last-updated.html" . }}
{{ .Scratch.Set "reversePagination" true }}
{{ partial "components/pager.html" . }}
{{ partial "components/comment.html" . }}
</main>
</article>
</div>
Expand Down
1 change: 1 addition & 0 deletions layouts/docs/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>{{ .Title }}</h1>
<div class="mt-16"></div>
{{ partial "components/last-updated.html" . }}
{{ partial "components/pager.html" . }}
{{ partial "components/comment.html" . }}
</main>
</article>
</div>
Expand Down
1 change: 1 addition & 0 deletions layouts/docs/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ <h1>{{ .Title }}</h1>
</div>
{{ partial "components/last-updated.html" . }}
{{ partial "components/pager.html" . }}
{{ partial "components/comment.html" . }}
</main>
</article>
</div>
Expand Down
8 changes: 8 additions & 0 deletions layouts/partials/components/comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- $commentEnable := site.Params.comment.enable | default false -}}
{{- $commentPageEnable := .Params.comment | default true -}}

{{- if and $commentEnable $commentPageEnable -}}
{{- if eq site.Params.comment.type "giscus" -}}
{{ partial "components/giscus.html" . }}
{{- end -}}
{{- end -}}
62 changes: 62 additions & 0 deletions layouts/partials/components/giscus.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{- with site.Params.comment.giscus -}}

{{- $lang = site.Language.Lang | default `en` -}}
<script>
/*
* "preferred color scheme" theme in giscus works using "prefers-color-scheme" in media query.
* but, hugo's theme switch function works by using "color-theme" in local storage.
* This solution was created with reference to:
* https://github.com/giscus/giscus/issues/336#issuecomment-1214366281
*/
function getGiscusTheme() {
return localStorage.getItem("color-theme");
}

function setGiscusTheme() {
function sendMessage(message) {
const iframe = document.querySelector('iframe.giscus-frame');
if (!iframe) return;
iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
}
sendMessage({
setConfig: {
theme: getGiscusTheme(),
},
});
}

document.addEventListener('DOMContentLoaded', function () {
const giscusAttributes = {
"src": "https://giscus.app/client.js",
"data-repo": "{{ .repo }}",
"data-repo-id": "{{ .repoId }}",
"data-category": "{{ .category }}",
"data-category-id": "{{ .categoryId }}",
"data-mapping": "{{ .mapping | default `pathname` }}",
"data-strict": "{{ (string .strict) | default 0 }}",
"data-reactions-enabled": "{{ (string .reactionsEnabled) | default 1 }}",
"data-emit-metadata": "{{ (string .emitMetadata) | default 0 }}",
"data-input-position": "{{ .inputPosition | default `top` }}",
"data-theme": getGiscusTheme(),
"data-lang": "{{ .lang | default $lang }}",
"crossorigin": "anonymous",
{{ if .lazyLoading -}}
"data-loading": "lazy",
{{ end -}}
"async": "",
};

// Dynamically create script tag
const giscusScript = document.createElement("script");
Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
document.getElementById('giscus').appendChild(giscusScript);

// Update giscus theme when theme switcher is clicked
const toggle = document.querySelector('.theme-toggle');
if (toggle) {
toggle.addEventListener('click', setGiscusTheme);
}
});
</script>
<div id="giscus"></div>
{{- end -}}

0 comments on commit 6a19ac3

Please sign in to comment.