Perf: complete giscus comment system (#130)

This commit is contained in:
Cell
2022-05-08 23:35:59 +08:00
parent ca9125f4e1
commit 6128f3f3dd
5 changed files with 55 additions and 18 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+10 -13
View File
@@ -929,20 +929,17 @@ enableEmoji = true
lightgallery = false
[params.page.comment.giscus]
enable = true
repo = "giscus/giscus"
repoId = "MDEwOlJlcG9zaXRvcnkzNTE5NTgwNTM="
category = ""
categoryId = "MDE4OkRpc2N1c3Npb25DYXRlZ29yeTMyNzk2NTc1"
mapping = "specific"
term = "Welcome to giscus!"
repo = "Lruihao/FixIt"
repoId = "R_kgDOGihPtQ"
category = "General"
categoryId = "DIC_kwDOGihPtc4CApHX"
mapping = "title"
reactionsEnabled = "1"
emitMetadata = "1"
inputPosition = "top" # top, bottom
theme = "light"
lang = "en"
loading = "lazy"
nscript = "afterInteractive"
crossorigin = "anonymous"
emitMetadata = "0"
inputPosition = "bottom" # top, bottom
lightTheme = "light"
darkTheme = "dark"
lazyLoad = true
# Third-party library config
# 第三方库配置
[params.page.library]
+24 -1
View File
@@ -236,7 +236,30 @@
{{- $giscus := $comment.giscus | default dict -}}
{{- if $giscus.enable -}}
{{- with $giscus -}}
<script src="https://giscus.app/client.js" data-repo="{{ .Repo }}" data-repo-id="{{ .RepoId }}" data-category="{{ .Category }}" data-category-id="{{ .CategoryId }}" data-mapping="{{ .Mapping }}" data-term="{{ .Term }}" data-theme="{{ .Theme }}" data-reactions-enabled="{{ .ReactionsEnabled }}" data-emit-metadata="{{ .emitMetadata }}" data-input-position="{{ .inputPosition }}" data-lang="{{ .Lang }}" data-nscript="{{ .Nscript }}"></script>
{{- $commentConfig = .lightTheme | default "light" | dict "lightTheme" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = .darkTheme | default "dark" | dict "darkTheme" | dict "giscus" | merge $commentConfig -}}
<div id="giscus">
<script
src="https://giscus.app/client.js"
data-repo="{{ .Repo }}"
data-repo-id="{{ .RepoId }}"
data-category="{{ .Category }}"
data-category-id="{{ .CategoryId }}"
data-mapping="{{ .Mapping }}"
data-theme="preferred_color_scheme"
data-reactions-enabled="{{ .ReactionsEnabled }}"
data-emit-metadata="{{ .emitMetadata }}"
data-input-position="{{ .inputPosition }}"
data-lang="{{ $.Site.LanguageCode }}"
{{ if ne .LazyLoad false }}data-loading="lazy"{{ end }}
crossorigin="anonymous"
async
defer
></script>
<noscript>
Please enable JavaScript to view the comments powered by <a href="https://giscus.app/" rel="external nofollow noopener noreffer">giscus</a>.
</noscript>
</div>
{{- end -}}
{{- end -}}
</div>
+19 -2
View File
@@ -763,8 +763,7 @@ class Theme {
type: 'set-theme',
theme: this.isDark ? utterancesConfig.darkTheme : utterancesConfig.lightTheme
};
const iframe = document.querySelector('.utterances-frame');
iframe.contentWindow.postMessage(message, 'https://utteranc.es');
document.querySelector('.utterances-frame')?.contentWindow.postMessage(message, 'https://utteranc.es');
});
this.switchThemeEventSet.add(this._utterancesOnSwitchTheme);
return;
@@ -793,6 +792,24 @@ class Theme {
}
return;
}
if (this.config.comment.giscus) {
const giscusConfig = this.config.comment.giscus;
this._giscusOnSwitchTheme = this._giscusOnSwitchTheme || (() => {
const message = { setConfig: { theme: this.isDark ? giscusConfig.darkTheme : giscusConfig.lightTheme }};
document.querySelector('.giscus-frame')?.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
});
this.switchThemeEventSet.add(this._giscusOnSwitchTheme);
const _this = this;
_this.giscus2parentMsg = window.addEventListener('message', (event) => {
const $script = document.querySelector('#giscus>script');
if ($script){
_this._giscusOnSwitchTheme();
$script.parentElement.removeChild($script);
}
window.removeEventListener('message', _this.giscus2parentMsg);
});
return;
}
}
initCookieconsent() {