Skip to content

Commit

Permalink
Provide an ability to use comments form
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Apr 14, 2024
1 parent 23d7728 commit 43c1220
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
37 changes: 37 additions & 0 deletions assets/js/giscus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
category,
categoryId,
emitMetadata,
inputPosition,
lang,
mapping,
reactionsEnabled,
repo,
repoId,
strict,
theme
} from '@params';

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,
'data-strict': strict,
'data-reactions-enabled': reactionsEnabled,
'data-emit-metadata': emitMetadata,
'data-input-position': inputPosition,
'data-theme': theme,
'data-lang': lang,
'crossorigin': 'anonymous',
'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);
});
24 changes: 24 additions & 0 deletions exampleSite/config/_default/params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,27 @@ search:
# - it
# - pt
# - ru

comments:
enable: false
type: giscus

# Configure giscus comments.
#
# For more see: https://giscus.app/
giscus:
# Required parameters:
repo: sergeyklay/gohugo-theme-ed
repoId: R_kgDOHEYacw
category: General
categoryId: DIC_kwDOHEYac84Cerse

# Optional parameters:
#
# theme: light
# mapping: pathname
# strict: 0
# reactionsEnabled: 1
# emitMetadata: 0
# inputPosition: bottom
# lang: en
2 changes: 2 additions & 0 deletions layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ <h1 class="text-title">
{{ if not (in (slice "drama" "narrative" "poem") $postType) }}
{{ partial "post-tags.html" . }}
{{ end }}

{{ partial "comments.html" . }}
</article>
{{ end }}
39 changes: 39 additions & 0 deletions layouts/partials/comments.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{- $enableComments := site.Params.comments.enable | default false -}}

{{ if not (eq .Params.comments nil) }}
{{ $enableComments = .Params.comments }}
{{ end }}

{{- if $enableComments -}}
{{- if eq site.Params.comments.type "giscus" -}}
{{- $isProduction := (or (eq (getenv "HUGO_ENV") "production") (eq site.Params.env "production")) -}}

{{- $repo := site.Params.comments.giscus.repo -}}
{{- $repoId := site.Params.comments.giscus.repoId -}}
{{- $category := site.Params.comments.giscus.category -}}
{{- $categoryId := site.Params.comments.giscus.categoryId -}}
{{- $theme := site.Params.comments.giscus.theme | default "light" -}}
{{- $emitMetadata := site.Params.comments.giscus.emitMetadata | default 0 -}}
{{- $inputPosition := site.Params.comments.giscus.inputPosition | default "bottom" -}}
{{- $lang := site.Params.comments.giscus.lang | default "en" -}}
{{- $mapping := site.Params.comments.giscus.mapping | default "pathname" -}}
{{- $reactionsEnabled := site.Params.comments.giscus.reactionsEnabled | default 1 -}}
{{- $strict := site.Params.comments.giscus.strict | default 0 -}}
{{- $commentsParams := dict "repo" $repo "repoId" $repoId "category" $category "categoryId" $categoryId "theme" $theme "emitMetadata" $emitMetadata "inputPosition" $inputPosition "lang" $lang "mapping" $mapping "reactionsEnabled" $reactionsEnabled "strict" $strict -}}

{{- $siteComments := slice -}}
{{- $siteComments = $siteComments | append (resources.Get "js/giscus.js") -}}
{{- $siteComments = $siteComments | resources.Concat "js/giscus-bundle.js" -}}

{{- $siteComments = $siteComments | js.Build (dict "format" "iife" "minify" $isProduction "target" "es2015" "params" $commentsParams) -}}

{{- if or (site.Params.assets.disable_fingerprinting) (not $isProduction) }}
<script src="{{ $siteComments.RelPermalink }}"></script>
{{- else -}}
{{- $siteComments = $siteComments | fingerprint }}
<script src="{{ $siteComments.RelPermalink }}" integrity="{{ $siteComments.Data.Integrity }}"></script>
{{- end -}}

<div id="giscus"></div>
{{- end -}}
{{- end -}}

0 comments on commit 43c1220

Please sign in to comment.