summaryrefslogtreecommitdiff
path: root/_includes/extensions/comments
diff options
context:
space:
mode:
authorNuri Jung <jnooree@snu.ac.kr>2022-07-15 00:39:28 +0900
committerGitHub <noreply@github.com>2022-07-14 23:39:28 +0800
commit6d8d647ec51962be077a3432667a475c6fd08c8d (patch)
tree0a2566b775f83400dc2c7cfdc11836a9e99680f3 /_includes/extensions/comments
parent26475d899bfd1cd7abdef35ecd2d77274064e0cd (diff)
fix: utterances script to obey the `follow_site_theme` setting (#91)
* Fix js for correct theme load * Use dataTheme directly instead ternary operator
Diffstat (limited to '_includes/extensions/comments')
-rw-r--r--_includes/extensions/comments/utterances.html57
1 files changed, 31 insertions, 26 deletions
diff --git a/_includes/extensions/comments/utterances.html b/_includes/extensions/comments/utterances.html
index 4743562..8fe8beb 100644
--- a/_includes/extensions/comments/utterances.html
+++ b/_includes/extensions/comments/utterances.html
@@ -1,46 +1,51 @@
-<script async
- crossorigin="anonymous"
- issue-term="{{ site.utterances.issue_term }}"
- label="{{ site.utterances.label }}"
- {%- if site.utterances.follow_site_theme -%}
- onload="initUtterancesTheme()"
- {%- endif -%}
- repo="{{ site.utterances.repo }}"
- src="https://utteranc.es/client.js"
- theme="{{ site.utterances.theme }}">
-</script>
-
{%- if site.utterances.follow_site_theme -%}
+<div id="utterances-placeholder"></div>
<script>
- const setUtterancesTheme = (dataTheme) => {
+ const utterancesThemeFromDataTheme = () => {
+ const dataTheme = document.documentElement.getAttribute('data-theme');
+ return `github-${dataTheme}`;
+ };
+
+ const setUtterancesTheme = () => {
const iframe = document.querySelector('.utterances-frame');
if (iframe) {
- const utterancesTheme = dataTheme === 'dark' ? 'github-dark' : 'github-light'
const message = {
type: 'set-theme',
- theme: utterancesTheme
+ theme: utterancesThemeFromDataTheme()
};
iframe.contentWindow.postMessage(message, 'https://utteranc.es');
}
}
- // init theme
- const initUtterancesTheme = () => {
- const iframe = document.querySelector('.utterances-frame');
- if (!iframe) return
- iframe.onload = () => {
- setUtterancesTheme(document.documentElement.getAttribute('data-theme'))
- }
- }
-
// dynamic change
const observer = new MutationObserver((mutationsList, observer) => {
for (let mutation of mutationsList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
- setUtterancesTheme(document.documentElement.getAttribute('data-theme'))
+ setUtterancesTheme();
}
}
});
- observer.observe(document.documentElement, {attributes: true, childList: false, subtree: false});
+ observer.observe(document.documentElement, { attributes: true, childList: false, subtree: false });
+
+ let utterancesScript = document.createElement('script');
+ utterancesScript.async = true;
+ utterancesScript.src = 'https://utteranc.es/client.js';
+ utterancesScript.crossOrigin = 'anonymous';
+ utterancesScript.setAttribute('issue-term', '{{ site.utterances.issue_term }}');
+ utterancesScript.setAttribute('label', '{{ site.utterances.label }}');
+ utterancesScript.setAttribute('repo', '{{ site.utterances.repo }}');
+ utterancesScript.setAttribute('theme', utterancesThemeFromDataTheme());
+
+ const placeholder = document.getElementById('utterances-placeholder');
+ placeholder.parentNode.replaceChild(utterancesScript, placeholder);
</script>
+{%- else -%}
+<script async
+ crossorigin="anonymous"
+ issue-term="{{ site.utterances.issue_term }}"
+ label="{{ site.utterances.label }}"
+ repo="{{ site.utterances.repo }}"
+ src="https://utteranc.es/client.js"
+ theme="{{ site.utterances.theme }}">
+ </script>
{%- endif -%}