@@ -159,10 +159,30 @@ var scrollToElem = function(elemSelector) {
159
159
160
160
var addAuthor = function ( gist ) {
161
161
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>' ;
163
163
document . querySelector ( '#authorHolder' ) . style . display = 'block' ;
164
164
} ;
165
165
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&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
+
166
186
var loadGist = function ( gistId ) {
167
187
GithubApi . getGist ( gistId , function ( gist ) {
168
188
if ( gist ) {
@@ -196,7 +216,16 @@ var loadGist = function(gistId) {
196
216
permalink : true ,
197
217
permalinkClass : 'header-anchor' ,
198
218
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 - z A - Z 0 - 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
+ }
200
229
} ) ;
201
230
202
231
for ( var i in files . markdown ) {
@@ -223,20 +252,24 @@ var loadGist = function(gistId) {
223
252
addAuthor ( gist ) ;
224
253
}
225
254
226
- // add link to gist comment section , if we have comments
255
+ // add gist comments , if we have
227
256
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
+ } ) ;
240
273
}
241
274
242
275
// add syntax highlighting to code blocks
0 commit comments