summaryrefslogtreecommitdiff
path: root/_includes/extensions/comments/utterances.html
blob: 8fe8bebfd4dfb1dcb8ad8b31d5e41ddd01e0e421 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{%- if site.utterances.follow_site_theme -%}
<div id="utterances-placeholder"></div>
<script>
  const utterancesThemeFromDataTheme = () => {
    const dataTheme = document.documentElement.getAttribute('data-theme');
    return `github-${dataTheme}`;
  };

  const setUtterancesTheme = () => {
    const iframe = document.querySelector('.utterances-frame');
    if (iframe) {
      const message = {
        type: 'set-theme',
        theme: utterancesThemeFromDataTheme()
      };
      iframe.contentWindow.postMessage(message, 'https://utteranc.es');
    }
  }

  // dynamic change
  const observer = new MutationObserver((mutationsList, observer) => {
    for (let mutation of mutationsList) {
      if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
        setUtterancesTheme();
      }
    }
  });
  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 -%}