add comment system
This commit is contained in:
57
layouts/partials/comments/disqus.html
Normal file
57
layouts/partials/comments/disqus.html
Normal file
@ -0,0 +1,57 @@
|
||||
<script>
|
||||
var disqus_config = function () {
|
||||
this.page.url = '{{ .Permalink }}';
|
||||
this.page.identifier = '{{ .RelPermalink }}';
|
||||
};
|
||||
|
||||
function addDisqus() {
|
||||
let disqusThread = document.createElement('div');
|
||||
let paragraph = document.createElement('p');
|
||||
|
||||
disqusThread.id = 'disqus_thread';
|
||||
paragraph.className = 'text-center text-muted small';
|
||||
paragraph.innerHTML = 'Comments powered by <a href="https://disqus.com/">Disqus</a>.';
|
||||
disqusThread.appendChild(paragraph);
|
||||
|
||||
const footer = document.querySelector('footer');
|
||||
footer.insertAdjacentElement("beforebegin", disqusThread);
|
||||
}
|
||||
|
||||
{{/* Auto switch theme */}}
|
||||
function reloadDisqus(event) {
|
||||
if (event.source === window && event.data && event.data.id === Theme.ID) {
|
||||
{{/* Disqus hasn't been loaded */}}
|
||||
if (typeof DISQUS === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (document.readyState == 'complete') {
|
||||
DISQUS.reset({ reload: true, config: disqus_config });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addDisqus();
|
||||
|
||||
if (Theme.switchable) {
|
||||
addEventListener('message', reloadDisqus);
|
||||
}
|
||||
|
||||
{{/* Lazy loading */}}
|
||||
var disqusObserver = new IntersectionObserver(
|
||||
function (entries) {
|
||||
if (entries[0].isIntersecting) {
|
||||
var d = document,
|
||||
s = d.createElement('script');
|
||||
s.src = 'https://{{ .Site.Params.comments.disqus.shortname }}.disqus.com/embed.js';
|
||||
s.setAttribute('data-timestamp', +new Date());
|
||||
(d.head || d.body).appendChild(s);
|
||||
|
||||
disqusObserver.disconnect();
|
||||
}
|
||||
},
|
||||
{ threshold: [0] }
|
||||
);
|
||||
|
||||
disqusObserver.observe(document.getElementById('disqus_thread'));
|
||||
</script>
|
54
layouts/partials/comments/giscus.html
Normal file
54
layouts/partials/comments/giscus.html
Normal file
@ -0,0 +1,54 @@
|
||||
<script>
|
||||
(function () {
|
||||
const themeMapper = Theme.getThemeMapper('light', 'dark_dimmed');
|
||||
const initTheme = themeMapper[Theme.visualState];
|
||||
|
||||
let lang = '{{ .Site.Params.comments.giscus.lang | default .Site.Language.Lang }}';
|
||||
{{/* https://github.com/giscus/giscus/tree/main/locales */}}
|
||||
if (lang.length > 2 && !lang.startsWith('zh')) {
|
||||
lang = lang.slice(0, 2);
|
||||
}
|
||||
|
||||
let giscusAttributes = {
|
||||
src: 'https://giscus.app/client.js',
|
||||
'data-repo': '{{ .Site.Params.comments.giscus.repo }}',
|
||||
'data-repo-id': '{{ .Site.Params.comments.giscus.repo_id }}',
|
||||
'data-category': '{{ .Site.Params.comments.giscus.category }}',
|
||||
'data-category-id': '{{ .Site.Params.comments.giscus.category_id }}',
|
||||
'data-mapping': '{{ .Site.Params.comments.giscus.mapping | default "pathname" }}',
|
||||
'data-strict': '{{ .Site.Params.comments.giscus.strict | default "0" }}',
|
||||
'data-reactions-enabled': '{{ .Site.Params.comments.giscus.reactions_enabled | default "1" }}',
|
||||
'data-emit-metadata': '0',
|
||||
'data-theme': initTheme,
|
||||
'data-input-position': '{{ .Site.Params.comments.giscus.input_position | default "bottom" }}',
|
||||
'data-lang': lang,
|
||||
'data-loading': 'lazy',
|
||||
crossorigin: 'anonymous',
|
||||
async: ''
|
||||
};
|
||||
|
||||
let giscusNode = document.createElement('script');
|
||||
Object.entries(giscusAttributes).forEach(([key, value]) =>
|
||||
giscusNode.setAttribute(key, value)
|
||||
);
|
||||
|
||||
const $footer = document.querySelector('footer');
|
||||
$footer.insertAdjacentElement("beforebegin", giscusNode);
|
||||
|
||||
addEventListener('message', (event) => {
|
||||
if (event.source === window && event.data && event.data.id === Theme.ID) {
|
||||
const newTheme = themeMapper[Theme.visualState];
|
||||
|
||||
const message = {
|
||||
setConfig: {
|
||||
theme: newTheme
|
||||
}
|
||||
};
|
||||
|
||||
const giscus =
|
||||
document.getElementsByClassName('giscus-frame')[0].contentWindow;
|
||||
giscus.postMessage({ giscus: message }, 'https://giscus.app');
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
37
layouts/partials/comments/utterances.html
Normal file
37
layouts/partials/comments/utterances.html
Normal file
@ -0,0 +1,37 @@
|
||||
<script>
|
||||
(function () {
|
||||
const origin = 'https://utteranc.es';
|
||||
const themeMapper = Theme.getThemeMapper('github-light', 'github-dark');
|
||||
const initTheme = themeMapper[Theme.visualState];
|
||||
|
||||
let script = document.createElement('script');
|
||||
script.src = 'https://utteranc.es/client.js';
|
||||
script.setAttribute('repo', '{{ .Site.Params.comments.utterances.repo }}');
|
||||
script.setAttribute('issue-term', '{{ .Site.Params.comments.utterances.issue_term }}');
|
||||
script.setAttribute('theme', initTheme);
|
||||
script.crossOrigin = 'anonymous';
|
||||
script.async = true;
|
||||
|
||||
const $footer = document.querySelector('footer');
|
||||
$footer.insertAdjacentElement('beforebegin', script);
|
||||
|
||||
addEventListener('message', (event) => {
|
||||
let newTheme;
|
||||
|
||||
{{/*
|
||||
Credit to <https://github.com/utterance/utterances/issues/170#issuecomment-594036347>
|
||||
*/}}
|
||||
if (event.source === window && event.data && event.data.id === Theme.ID) {
|
||||
newTheme = themeMapper[Theme.visualState];
|
||||
|
||||
const message = {
|
||||
type: 'set-theme',
|
||||
theme: newTheme
|
||||
};
|
||||
|
||||
const utterances = document.querySelector('.utterances-frame').contentWindow;
|
||||
utterances.postMessage(message, origin);
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
Reference in New Issue
Block a user