Skip to content

Commit 51b06c2

Browse files
author
Stephan Schmitz
committed
add comments
1 parent 3bfa449 commit 51b06c2

File tree

4 files changed

+136
-23
lines changed

4 files changed

+136
-23
lines changed

index.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@
4040
<h1 id="titleHolder">Loading gist...</h1>
4141
</header>
4242
<div id="authorHolder">
43-
<span>Post by <span id="gistAuthor"></span>, <span id="gistPubDate"></span></span>
43+
<span>Post by <span id="gistAuthor"></span>, at <span id="gistPubDate"></span></span>
4444
</div>
4545
<div id="gistContent">
4646
<noscript>Enable JavaScript to use nicegist.</noscript>
47+
<div id="loadingIndicator">
48+
<img src="./static/dom_distiller_material_spinner.svg">
49+
</div>
4750
</div>
4851
</article>
49-
<div id="loadingIndicator">
50-
<img src="./static/dom_distiller_material_spinner.svg">
51-
</div>
52-
<div id="gistComments"></div>
52+
<div id="gist-comments"></div>
5353
<footer>
5454
<p id="footerIntro">Check out the <a href="https://github.com/nicegist/nicegist.github.io">source for Nicegist on GitHub</a></p>
5555
<p id="footerPost"><a href="https://nicegist.github.io/">Nicegist</a> · writing for hackers · zero setup · publish in seconds</p>

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "Nicegist",
2+
"name": "Nicegist · writing for hackers · zero setup · publish in seconds",
33
"shortname": "Nicegist",
44
"theme_color": "#FAFAFA",
55
"background_color": "#FAFAFA",

static/app.js

+48-15
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,30 @@ var scrollToElem = function(elemSelector) {
159159

160160
var addAuthor = function(gist) {
161161
document.querySelector('#gistAuthor').innerHTML = '<a href="' + gist.owner.html_url + '">@' + gist.owner.login + '</a>';
162-
document.querySelector('#gistPubDate').textContent = gist.created_at;
162+
document.querySelector('#gistPubDate').innerHTML = '<a href="' + gist.html_url + '">' + gist.created_at + '</a>';
163163
document.querySelector('#authorHolder').style.display = 'block';
164164
};
165165

166+
var getCommentHTML = function(comment, renderedMarkdown) {
167+
var username = comment.user === null ? 'ghost' : comment.user.login; // when a gist user was deleted, github uses a "ghost" label
168+
var avatar = comment.user === null ? 'https://avatars3.githubusercontent.com/u/10137' : comment.user.avatar_url;
169+
var commentUsername = comment.user === null ? `<span class="username">${username}</span>` : `<a href="${comment.user.html_url}" class="username">${username}</a>`;
170+
return `<div class="comment-block">
171+
<div class="comment" id="comment-${comment.id}">
172+
<div class="comment-block-title">
173+
<img class="avatar" height="32" width="32" alt="@${username}" src="${avatar}?s=88&amp;v=4">
174+
<div class="comment-block-meta">
175+
${commentUsername}<br>
176+
commented at <a href="#comment-${comment.id}"><time class="timestamp" datetime="${comment.created_at}">${comment.created_at}</time></a>
177+
</div>
178+
</div>
179+
<div class="comment-block-comment">
180+
<div class="comment-body">${renderedMarkdown}</div>
181+
</div>
182+
</div>
183+
</div>`;
184+
};
185+
166186
var loadGist = function(gistId) {
167187
GithubApi.getGist(gistId, function(gist) {
168188
if (gist) {
@@ -196,7 +216,16 @@ var loadGist = function(gistId) {
196216
permalink: true,
197217
permalinkClass: 'header-anchor',
198218
permalinkSymbol: '¶',
199-
permalinkBefore: true
219+
permalinkBefore: true,
220+
slugify: function(str) {
221+
// use custom slugify function to fix several issues with anchor generation (special chars related)
222+
str = encodeURIComponent(String(str).trim().toLowerCase().replace(/[^a-zA-Z0-9]+/g,"-"));
223+
if (/[0-9]/.test(str[0])) { // ids must not start with a number
224+
var x = str.split('-', 1);
225+
str = str.substring((x[0].length + 1));
226+
}
227+
return str;
228+
}
200229
});
201230

202231
for (var i in files.markdown) {
@@ -223,20 +252,24 @@ var loadGist = function(gistId) {
223252
addAuthor(gist);
224253
}
225254

226-
// add link to gist comment section, if we have comments
255+
// add gist comments, if we have
227256
if (!isHomepage && gist.comments > 0) {
228-
// var commentsHTML = '';
229-
// GithubApi.getGistComments(gistId, function(comments) {
230-
// if (comments && comments.length) {
231-
// for (var m in comments) {
232-
// var commentHTML = md.render(comments[m].body);
233-
// console.log(commentHTML);
234-
// }
235-
// }
236-
// }, function(error) {
237-
// console.warn(error);
238-
// });
239-
document.querySelector('#gistComments').innerHTML = '<a target="_blank" href="https://gist.github.com/' + gistId + '#comments">' + gist.comments + ' comments</a>';
257+
var hl = gist.comments > 1 ? gist.comments + ' Comments' : '1 Comment';
258+
var commentsHTML = '<h2><a class="header-anchor" href="#gist-comments" aria-hidden="true">¶</a>' + hl + '</h2>';
259+
commentsHTML += '<p><a target="_blank" href="' + gist.html_url + '#partial-timeline-marker">Add comment on Gist</a></p>'
260+
GithubApi.getGistComments(gistId, function(comments) {
261+
if (comments && comments.length) {
262+
// create a new instance, since we don't want to create anchor links within comments
263+
md = window.markdownit({linkify: true});
264+
for (var m in comments) {
265+
commentsHTML += getCommentHTML(comments[m], md.render(comments[m].body));
266+
}
267+
document.querySelector('#gist-comments').style.display = 'block';
268+
document.querySelector('#gist-comments').innerHTML = commentsHTML;
269+
}
270+
}, function(error) {
271+
console.warn(error);
272+
});
240273
}
241274

242275
// add syntax highlighting to code blocks

static/dom_distiller_viewer.css

+82-2
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ video::-webkit-media-controls-fullscreen-button {
229229
width: 100%;
230230
}
231231

232-
article > #gistContent, #gistComments, footer {
232+
article > #gistContent, #gist-comments, footer {
233233
margin: 24px 16px 24px 16px;
234234
}
235235

@@ -361,7 +361,7 @@ img {
361361
footer {
362362
margin-top: 4em;
363363
margin-bottom: 2em;
364-
border-top: 1px solid #eee;
364+
border-top: 1px solid #e1e4e8;
365365
}
366366

367367
footer p {
@@ -401,6 +401,10 @@ h6:hover .header-anchor {
401401
}
402402
}
403403

404+
article {
405+
margin-bottom: 45px;
406+
}
407+
404408
#authorHolder {
405409
margin: 5px 16px;
406410
display: none;
@@ -413,3 +417,79 @@ h6:hover .header-anchor {
413417
color: #aaa !important;
414418
text-decoration: underline;
415419
}
420+
#gist-comments {
421+
display: none;
422+
}
423+
#gist-comments[style*="block"] {
424+
border-top: 1px solid #e1e4e8;
425+
}
426+
.comment-block {
427+
border-top: 1px solid #e1e4e8;
428+
padding: 15px 0;
429+
}
430+
.comment-block>.comment {
431+
margin: 0;
432+
}
433+
.comment {
434+
background: #fff;
435+
border: 1px solid #d1d5da;
436+
border-radius: 3px;
437+
margin: 0 15px 15px;
438+
overflow: hidden;
439+
padding: 0;
440+
white-space: normal;
441+
word-break: break-word;
442+
word-wrap: break-word;
443+
}
444+
.comment-block-title {
445+
background-color: #fff;
446+
border-bottom: 1px solid #e1e4e8;
447+
color: #586069;
448+
font-size: 12px;
449+
line-height: 16px;
450+
padding: 10px 15px;
451+
position: relative;
452+
}
453+
.comment-block-title:before,
454+
.comment-block-title:after {
455+
content: "";
456+
display: table;
457+
}
458+
.avatar {
459+
border-radius: 3px;
460+
display: inline-block;
461+
line-height: 1;
462+
overflow: hidden;
463+
vertical-align: middle;
464+
margin: 0;
465+
}
466+
.comment-block-title .avatar {
467+
border-radius: 3px;
468+
float: left;
469+
margin-right: 10px;
470+
}
471+
.comment-block-title .username {
472+
color: #444d56 !important;
473+
font-weight: 600;
474+
}
475+
.comment-block-title .timestamp {
476+
color: #586069 !important;
477+
}
478+
.comment-block-title a {
479+
text-decoration: none;
480+
}
481+
.comment-block-meta {
482+
margin-left: 42px;
483+
}
484+
.comment-block-comment {
485+
padding: 15px;
486+
}
487+
.comment-body {
488+
line-height: 1.5;
489+
word-wrap: break-word;
490+
}
491+
.comment-body:before,
492+
.comment-body:after {
493+
content: "";
494+
display: table;
495+
}

0 commit comments

Comments
 (0)