Skip to content

Commit

Permalink
implement comments
Browse files Browse the repository at this point in the history
  • Loading branch information
morfikov committed Oct 11, 2021
1 parent 6782117 commit 9ac4346
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
25 changes: 25 additions & 0 deletions layouts/_default/single.html
@@ -0,0 +1,25 @@
{{ define "main" }}
<main class="main">
{{ partial "breadcrumb.html" . }}
<div class="single block">
<article class="entry">
{{- partial "entry/featured.html" (dict "page" . "IsSingle" true) }}
{{- partial "entry/meta.html" (dict "page" . "class" "mb") }}
<h1 class="entry__title">{{ .Title }}</h1>
{{- partial "entry/toc.html" . }}
<div class="entry__content">{{ .Content }}</div>
{{ if or (.Param "share") (isset $.Params "tags") }}
<footer class="entry__footer">
{{ partial "entry/tags.html" . }}
{{ partial "entry/share.html" . }}
</footer>
{{ end }}
</article>
</div>
</main>
{{ partial "authorbox.html" . }}
{{ partial "related.html" . }}
<div id="comments">
{{ partial "comments.html" $ }}
</div>
{{ end }}
65 changes: 65 additions & 0 deletions layouts/partials/comments.html
@@ -0,0 +1,65 @@
<script>
var id = {{ .Params.GHissueID }};

if (id)
{
let url = "https://github.com/morfikov/morfitronik-comments/issues/".concat(id);
let api_url = "https://api.github.com/repos/morfikov/morfitronik-comments/issues/".concat(id, "/comments");

var commentsDiv = document.getElementById("comments");

let xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.open("GET", api_url);
xhr.setRequestHeader("Accept", "application/vnd.github.v3.html+json");
xhr.send();

xhr.onload = function()
{
if (xhr.status != 200)
{
let errorText = document.createElement("p");
errorText.innerHTML = "<i>Comments for this post yet are not opened yet (or you have GitHub scripts disabled).</i>";
commentsDiv.appendChild(errorText);
}
else
{
let comments = xhr.response;

let mainHeader = document.createElement("h2");
mainHeader.innerHTML = "Comments: ".concat(comments.length);
commentsDiv.appendChild(mainHeader);

let issueLink = document.createElement("p");
issueLink.innerHTML = "<i>You can leave a comment using this <a href='".concat(url, "'>GitHub issue</a>.</i>");
commentsDiv.appendChild(issueLink);

comments.forEach(function(comment)
{
let commentContent = document.createElement("div");
commentContent.setAttribute('class', 'gh-comment')
commentContent.innerHTML = "".concat(
"<div class='gh-header'>",
"<img src='", comment.user.avatar_url, "' />",
"<div style='margin:auto 0;'>",
"<b><a class='gh-username' href='", comment.user.html_url, "'>", comment.user.login, "</a></b>",
" commented at <em>", new Date(comment.created_at), "</em>",
"</div>",
"</div>",
"<div class='gh-body'>",
comment.body_html,
"</div>"
);
commentsDiv.appendChild(commentContent);
});
}
};

xhr.onerror = function()
{
let errorText = document.createElement("p");
errorText.innerHTML = "<i>Some error loading comments.</i>";
commentsDiv.appendChild(errorText);
};
}
</script>

0 comments on commit 9ac4346

Please sign in to comment.