Hugo | 评论引用显示Mastodon嘟文
在?看看代码。
原作者原帖在这里!
我狂喜,这就是人家想要的评论系统!火速实装!
好,先看一下这个Diary主题的结构。评论部分是在\layouts\partials\comment.html
。
先确定全局设置里其他评论系统都已经关闭!
然后,把原作者的代码稍微修改一下即可。
点击展开:直接粘贴到comment.html里
{{ with .Params.comment }} <!-- 这里改成了comment -->
<div class="article-content">
<h2>Comments</h2>
<p>You can use your Mastodon account to reply to this <a class="link" href="https://{{ .host }}/@{{ .username }}/{{ .id }}">post</a>.</p>
<p><a class="button" href="https://{{ .host }}/interact/{{ .id }}?type=reply">Reply</a></p>
<p id="mastodon-comments-list"><button id="load-comment">Load comments</button></p>
<noscript><p>You need JavaScript to view the comments.</p></noscript>
<script src="https://cdn.jsdelivr.net/gh/cure53/DOMPurify/dist/purify.min.js"></script><!-- 这里也可以下载到本地放到文件夹里,比如写成/assets/js/purify.min.js -->
<script type="text/javascript">
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
document.getElementById("load-comment").addEventListener("click", function() {
document.getElementById("load-comment").innerHTML = "Loading";
fetch('https://{{ .host }}/api/v1/statuses/{{ .id }}/context')
.then(function(response) {
return response.json();
})
.then(function(data) {
if(data['descendants'] &&
Array.isArray(data['descendants']) &&
data['descendants'].length > 0) {
document.getElementById('mastodon-comments-list').innerHTML = "";
data['descendants'].forEach(function(reply) {
reply.account.display_name = escapeHtml(reply.account.display_name);
reply.account.emojis.forEach(emoji => {
reply.account.display_name = reply.account.display_name.replace(`:${emoji.shortcode}:`,
`<img src="${escapeHtml(emoji.static_url)}" alt="Emoji ${emoji.shortcode}" height="20" width="20" />`);
});
mastodonComment =
`<div class="mastodon-comment">
<div class="avatar">
<img src="${escapeHtml(reply.account.avatar_static)}" height=60 width=60 alt="">
</div>
<div class="content">
<div class="author">
<a href="${reply.account.url}" rel="nofollow">
<span>${reply.account.display_name}</span>
<span class="disabled">${escapeHtml(reply.account.acct)}</span>
</a>
<a class="date" href="${reply.uri}" rel="nofollow">
${reply.created_at.substr(0, 10)}
</a>
</div>
<div class="mastodon-comment-content">${reply.content}</div>
</div>
</div>`;
document.getElementById('mastodon-comments-list').appendChild(DOMPurify.sanitize(mastodonComment, {'RETURN_DOM_FRAGMENT': true}));
});
} else {
document.getElementById('mastodon-comments-list').innerHTML = "<p>Not comments found</p>";
}
});
});
</script>
</div>
{{ end }}
配置好啦,然后我们到单篇博文的frontmatter里写对应要显示的嘟文信息。 比如——
comment:
host: linuxrocks.online
username: carl
id: 105105837504372590
如果是新建一个comments.html文件,那么要在single.html里对应的位置放上{{ partial "comments.html" . }}
。
啊说来Diary主题这里写的是{{- partial "comment.html" . -}}
来着。
锵锵锵锵!请看下方!
呃,为什么这个主题不能显示……我在另一个stack主题的博客上这样设置完能看到的欸……