feat(giscus): standardize configuration, support i18n and add lazyLoading config

This commit is contained in:
Dillon 2022-05-26 01:51:17 +08:00
parent 487a772442
commit 61eccb2502
No known key found for this signature in database
GPG key ID: 0EF4AE57FB98F108
23 changed files with 156 additions and 61 deletions

View file

@ -865,6 +865,43 @@ var Theme = /*#__PURE__*/function () {
this.switchThemeEventSet.add(this._utterancesOnSwitchTheme); this.switchThemeEventSet.add(this._utterancesOnSwitchTheme);
} }
if (this.config.comment.giscus) {
var giscusConfig = this.config.comment.giscus;
var giscusScript = document.createElement('script');
giscusScript.src = 'https://giscus.app/client.js';
giscusScript.type = 'text/javascript';
giscusScript.setAttribute('data-repo', giscusConfig.repo);
giscusScript.setAttribute('data-repo-id', giscusConfig.repoId);
giscusScript.setAttribute('data-category', giscusConfig.category);
giscusScript.setAttribute('data-category-id', giscusConfig.categoryId);
giscusScript.setAttribute('data-lang', giscusConfig.lang);
giscusScript.setAttribute('data-mapping', giscusConfig.mapping);
giscusScript.setAttribute('data-reactions-enabled', giscusConfig.reactionsEnabled);
giscusScript.setAttribute('data-emit-metadata', giscusConfig.emitMetadata);
giscusScript.setAttribute('data-input-position', giscusConfig.inputPosition);
if (giscusConfig.lazyLoading) giscusScript.setAttribute('data-loading', 'lazy');
giscusScript.setAttribute('data-theme', this.isDark ? giscusConfig.darkTheme : giscusConfig.lightTheme);
giscusScript.crossOrigin = 'anonymous';
giscusScript.async = true;
document.getElementById('giscus').appendChild(giscusScript);
this._giscusOnSwitchTheme = this._giscusOnSwitchTheme || function () {
var message = {
setConfig: {
theme: _this11.isDark ? giscusConfig.darkTheme : giscusConfig.lightTheme,
reactionsEnabled: false
}
};
var iframe = document.querySelector('iframe.giscus-frame');
if (!iframe) return;
iframe.contentWindow.postMessage({
giscus: message
}, 'https://giscus.app');
};
this.switchThemeEventSet.add(this._giscusOnSwitchTheme);
}
} }
} }
}, { }, {

View file

@ -418,6 +418,8 @@
avatar = "mp" avatar = "mp"
meta= "" meta= ""
pageSize = 10 pageSize = 10
# automatically adapt the current theme i18n configuration when empty
# 为空时自动适配当前主题 i18n 配置
lang = "" lang = ""
visitor = true visitor = true
recordIP = true recordIP = true
@ -442,6 +444,8 @@
width = "100%" width = "100%"
numPosts = 10 numPosts = 10
appId = "" appId = ""
# automatically adapt the current theme i18n configuration when empty
# 为空时自动适配当前主题 i18n 配置
languageCode = "" languageCode = ""
# Telegram comments config (https://comments.app/) # Telegram comments config (https://comments.app/)
# Telegram comments 评论系统设置 (https://comments.app/) # Telegram comments 评论系统设置 (https://comments.app/)
@ -468,25 +472,26 @@
label = "" label = ""
lightTheme = "github-light" lightTheme = "github-light"
darkTheme = "github-dark" darkTheme = "github-dark"
# giscus comment config (https://giscus.app/)
# giscus comment 评论系统设置 (https://giscus.app/zh-CN)
[params.page.comment.giscus] [params.page.comment.giscus]
# To enable giscus, the github discussions based comment section, # You can refer to the official documentation of giscus to use the following configuration.
# Follow the steps described to enable giscus and get the values # 你可以参考官方文档来使用下列配置
# needed to populate the below information.
# https://www.joelnitta.com/posts/2021-11-24_using-giscus/
enable = false enable = false
category = "Announcements" # Announcements, Comments repo = ""
lang = "en" # 保持 en, 只需要控制每篇文章的 url 不一样即可 repoId = ""
repo = "" # Entering repo will enable giscus category = "Announcements"
repo_id = "" # query by GraphQL API, https://docs.github.com/en/graphql/overview/explorer categoryId = ""
category_id = "" # query by GraphQL API, https://docs.github.com/en/graphql/overview/explorer # automatically adapt the current theme i18n configuration when empty
# 为空时自动适配当前主题 i18n 配置
lang = ""
mapping = "pathname" mapping = "pathname"
reactions_enabled = "0" reactionsEnabled = "1"
emit_metadata = "0" emitMetadata = "0"
input_position = "top" inputPosition = "bottom"
lightTheme = "light" # github theme lazyLoading = false
darkTheme = "dark" # github theme lightTheme = "light"
crossorigin = "anonymous" darkTheme = "dark"
label = "Comment" # Label applied to issue by utteranc.es bot
# Third-party library config # Third-party library config
# 第三方库配置 # 第三方库配置
[params.page.library] [params.page.library]

View file

@ -500,6 +500,8 @@ enableEmoji = true
avatar = "mp" avatar = "mp"
meta= "" meta= ""
pageSize = 10 pageSize = 10
# automatically adapt the current theme i18n configuration when empty
# 为空时自动适配当前主题 i18n 配置
lang = "" lang = ""
visitor = true visitor = true
recordIP = true recordIP = true
@ -524,6 +526,8 @@ enableEmoji = true
width = "100%" width = "100%"
numPosts = 10 numPosts = 10
appId = "" appId = ""
# automatically adapt the current theme i18n configuration when empty
# 为空时自动适配当前主题 i18n 配置
languageCode = "" languageCode = ""
# Telegram comments config (https://comments.app/) # Telegram comments config (https://comments.app/)
# Telegram comments 评论系统设置 (https://comments.app/) # Telegram comments 评论系统设置 (https://comments.app/)
@ -550,25 +554,26 @@ enableEmoji = true
label = "" label = ""
lightTheme = "github-light" lightTheme = "github-light"
darkTheme = "github-dark" darkTheme = "github-dark"
# giscus comment config (https://giscus.app/)
# giscus comment 评论系统设置 (https://giscus.app/zh-CN)
[params.page.comment.giscus] [params.page.comment.giscus]
# To enable giscus, the github discussions based comment section, # You can refer to the official documentation of giscus to use the following configuration.
# Follow the steps described to enable giscus and get the values # 你可以参考官方文档来使用下列配置
# needed to populate the below information. enable = false
# https://www.joelnitta.com/posts/2021-11-24_using-giscus/ repo = ""
# enable = true repoId = ""
category = "Announcements" # Announcements, Comments category = "Announcements"
lang = "en" # 保持 en, 只需要控制每篇文章的 url 不一样即可 categoryId = ""
repo = "" # Entering repo will enable giscus # automatically adapt the current theme i18n configuration when empty
repo_id = "" # query by GraphQL API, https://docs.github.com/en/graphql/overview/explorer # 为空时自动适配当前主题 i18n 配置
category_id = "" # query by GraphQL API, https://docs.github.com/en/graphql/overview/explorer lang = ""
mapping = "pathname" mapping = "pathname"
reactions_enabled = "0" reactionsEnabled = "1"
emit_metadata = "0" emitMetadata = "0"
input_position = "top" inputPosition = "bottom"
lightTheme = "light" # github theme lazyLoading = false
darkTheme = "dark" # github theme lightTheme = "light"
crossorigin = "anonymous" darkTheme = "dark"
label = "Comment" # Label applied to issue by utteranc.es bot
# Third-party library config # Third-party library config
# 第三方库配置 # 第三方库配置
[params.page.library] [params.page.library]

View file

@ -51,13 +51,16 @@ other = "يعمل الموقع بنظام {{ .Hugo }} | التصميم مقدم
# === partials/comment.html === # === partials/comment.html ===
[valineLang] [valineLang]
other = "ar" other = "en"
[valinePlaceholder] [valinePlaceholder]
other = "تعليقك..." other = "تعليقك..."
[facebookLanguageCode] [facebookLanguageCode]
other = "ar_AR" other = "ar_AR"
[giscusLang]
other = "en"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -52,13 +52,16 @@ other = "Impulsat per {{ .Hugo }} | Tema - {{ .Theme }}"
# === partials/comment.html === # === partials/comment.html ===
[valineLang] [valineLang]
other = "ca" other = "en"
[valinePlaceholder] [valinePlaceholder]
other = "El teu comentari ..." other = "El teu comentari ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "ca_ES" other = "ca_ES"
[giscusLang]
other = "en"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "Ihr Kommentar ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "de_DE" other = "de_DE"
[giscusLang]
other = "de"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -58,6 +58,9 @@ other = "Your comment ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "en_US" other = "en_US"
[giscusLang]
other = "en"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "Tu comentario ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "es_MX" other = "es_MX"
[giscusLang]
other = "es"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "Votre commentaire ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "fr" other = "fr"
[giscusLang]
other = "fr"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "Komentar Anda ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "id_ID" other = "id_ID"
[giscusLang]
other = "id"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "Il tuo commento ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "it" other = "it"
[giscusLang]
other = "it"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "Twój komentarz ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "pl" other = "pl"
[giscusLang]
other = "pl"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "O seu comentário ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "pt_BR" other = "pt_BR"
[giscusLang]
other = "en"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "Comentariul dvs ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "ro_RO" other = "ro_RO"
[giscusLang]
other = "ro"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -59,6 +59,9 @@ other = "Ваш комментарий ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "ru_RU" other = "ru_RU"
[giscusLang]
other = "ru"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -52,13 +52,16 @@ other = "Покреће {{ .Hugo }} | Тема - {{ .Theme }}"
# === partials/comment.html === # === partials/comment.html ===
[valineLang] [valineLang]
other = "sr" other = "en"
[valinePlaceholder] [valinePlaceholder]
other = "Ваш коментар ..." other = "Ваш коментар ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "sr_RS" other = "sr_RS"
[giscusLang]
other = "en"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -52,13 +52,16 @@ other = "{{ .Hugo }} ద్వారా ఆధారితం | నేపథ్
# === partials/comment.html === # === partials/comment.html ===
[valineLang] [valineLang]
# not supported # not supported
other = "te" other = "en"
[valinePlaceholder] [valinePlaceholder]
other = "మీ వాఖ్యనం ..." other = "మీ వాఖ్యనం ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "te_IN" other = "te_IN"
[giscusLang]
other = "en"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -52,13 +52,16 @@ other = "เว็บนี้สร้างโดย {{ .Hugo }} | ธีม -
# === partials/comment.html === # === partials/comment.html ===
[valineLang] [valineLang]
other = "th" other = "en"
[valinePlaceholder] [valinePlaceholder]
other = "ความคิดเห็นของคุณ ..." other = "ความคิดเห็นของคุณ ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "th_TH" other = "th_TH"
[giscusLang]
other = "en"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -58,6 +58,9 @@ other = "Bình luận của bạn ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "vi" other = "vi"
[giscusLang]
other = "vi"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -52,13 +52,16 @@ other = "由 {{ .Hugo }} 强力驱动 | 主题 - {{ .Theme }}"
# === partials/comment.html === # === partials/comment.html ===
[valineLang] [valineLang]
other = "zh-cn" other = "zh-CN"
[valinePlaceholder] [valinePlaceholder]
other = "你的评论 ..." other = "你的评论 ..."
[facebookLanguageCode] [facebookLanguageCode]
other = "zh_CN" other = "zh_CN"
[giscusLang]
other = "zh-CN"
# === partials/comment.html === # === partials/comment.html ===
# === partials/assets.html === # === partials/assets.html ===

View file

@ -52,7 +52,7 @@ other = "由 {{ .Hugo }} 強力驅動 | 主題 - {{ .Theme }}"
# === partials/comment.html === # === partials/comment.html ===
[valineLang] [valineLang]
other = "zh-tw" other = "zh-TW"
[valinePlaceholder] [valinePlaceholder]
other = "你的評論 ..." other = "你的評論 ..."

View file

@ -126,20 +126,20 @@
</noscript> </noscript>
{{- end -}} {{- end -}}
{{- /* Giscus Comment System */ -}} {{- /* giscus Comment System */ -}}
{{- $giscus := $comment.giscus | default dict -}} {{- $giscus := $comment.giscus | default dict -}}
{{- if $giscus.enable -}} {{- if $giscus.enable -}}
<div id="giscus" class="comment"></div> <div id="giscus" class="comment"></div>
{{- $commentConfig = $giscus.lang | default "en" | dict "lang" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = dict "repo" $giscus.repo | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = dict "repo" $giscus.repo | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = dict "repo_id" $giscus.repo_id | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = dict "repoId" $giscus.repoId | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = dict "category" $giscus.category | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = dict "category" $giscus.category | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = dict "category_id" $giscus.category_id | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = dict "categoryId" $giscus.categoryId | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = $giscus.lang | default (T "valineLang") | dict "lang" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = $giscus.mapping | default "pathname" | dict "mapping" | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = $giscus.mapping | default "pathname" | dict "mapping" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = $giscus.reactions_enabled | default "1" | dict "reactions_enabled" | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = $giscus.reactionsEnabled | default "1" | dict "reactionsEnabled" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = $giscus.emit_metadata | default "0" | dict "emit_metadata" | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = $giscus.emitMetadata | default "0" | dict "emitMetadata" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = $giscus.input_position | default "bottom" | dict "input_position" | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = $giscus.inputPosition | default "bottom" | dict "inputPosition" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = $giscus.crossorigin | default "anonymous" | dict "crossorigin" | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = $giscus.lazyLoading | default false | dict "lazyLoading" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = $giscus.lightTheme | default "github-light" | dict "lightTheme" | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = $giscus.lightTheme | default "github-light" | dict "lightTheme" | dict "giscus" | merge $commentConfig -}}
{{- $commentConfig = $giscus.darkTheme | default "github-dark" | dict "darkTheme" | dict "giscus" | merge $commentConfig -}} {{- $commentConfig = $giscus.darkTheme | default "github-dark" | dict "darkTheme" | dict "giscus" | merge $commentConfig -}}
<noscript> <noscript>

View file

@ -649,29 +649,26 @@ class Theme {
if (this.config.comment.giscus) { if (this.config.comment.giscus) {
const giscusConfig = this.config.comment.giscus; const giscusConfig = this.config.comment.giscus;
const giscusScript = document.createElement('script'); const giscusScript = document.createElement('script');
let dataTheme = this.isDark ? giscusConfig.darkTheme : giscusConfig.lightTheme
giscusScript.src = 'https://giscus.app/client.js'; giscusScript.src = 'https://giscus.app/client.js';
giscusScript.type = 'text/javascript'; giscusScript.type = 'text/javascript';
giscusScript.setAttribute('data-repo', giscusConfig.repo); giscusScript.setAttribute('data-repo', giscusConfig.repo);
giscusScript.setAttribute('data-repo-id', giscusConfig.repo_id); giscusScript.setAttribute('data-repo-id', giscusConfig.repoId);
giscusScript.setAttribute('data-category', giscusConfig.category); giscusScript.setAttribute('data-category', giscusConfig.category);
giscusScript.setAttribute('data-category-id', giscusConfig.category_id); giscusScript.setAttribute('data-category-id', giscusConfig.categoryId);
giscusScript.setAttribute('data-mapping', giscusConfig.mapping);
giscusScript.setAttribute('data-reactions-enabled', giscusConfig.reactions_enabled);
giscusScript.setAttribute('data-emit-metadata', giscusConfig.emit_metadata);
giscusScript.setAttribute('data-input-position', giscusConfig.input_position);
giscusScript.setAttribute('data-lang', giscusConfig.lang); giscusScript.setAttribute('data-lang', giscusConfig.lang);
if (giscusConfig.label) giscusScript.setAttribute('label', giscusConfig.label); giscusScript.setAttribute('data-mapping', giscusConfig.mapping);
giscusScript.setAttribute('data-theme', dataTheme); giscusScript.setAttribute('data-reactions-enabled', giscusConfig.reactionsEnabled);
giscusScript.setAttribute('data-emit-metadata', giscusConfig.emitMetadata);
giscusScript.setAttribute('data-input-position', giscusConfig.inputPosition);
if (giscusConfig.lazyLoading) giscusScript.setAttribute('data-loading', 'lazy');
giscusScript.setAttribute('data-theme', this.isDark ? giscusConfig.darkTheme : giscusConfig.lightTheme);
giscusScript.crossOrigin = 'anonymous'; giscusScript.crossOrigin = 'anonymous';
giscusScript.async = true; giscusScript.async = true;
document.getElementById('giscus').appendChild(giscusScript); document.getElementById('giscus').appendChild(giscusScript);
this._giscusOnSwitchTheme = this._giscusOnSwitchTheme || (() => { this._giscusOnSwitchTheme = this._giscusOnSwitchTheme || (() => {
dataTheme = this.isDark ? giscusConfig.darkTheme : giscusConfig.lightTheme
const message = { const message = {
setConfig: { setConfig: {
// theme: 'https://giscus.app/themes/custom_example.css', theme: this.isDark ? giscusConfig.darkTheme : giscusConfig.lightTheme,
theme: dataTheme,
reactionsEnabled: false, reactionsEnabled: false,
} }
}; };