|
| 1 | +{%- if include.api_url -%} |
| 2 | + {%- assign api_url = include.api_url -%} |
| 3 | +{%- else -%} |
| 4 | + {%- assign api_url = "https://welcomments.io/api" -%} |
| 5 | +{%- endif -%} |
| 6 | + |
| 7 | +{%- if include.website_id == "YOUR-WEBSITE-ID-HERE" -%} |
| 8 | + <div style="color: #ff0000;"> |
| 9 | + <h1>Website id not configured!</h1> |
| 10 | + <p>Please replace <code>YOUR-WEBSITE-ID-HERE</code> with the website id from Welcomments console.</p> |
| 11 | + </div> |
| 12 | +{%- endif -%} |
| 13 | + |
| 14 | +<div id="welcomments__container" class="welcomments__container"> |
| 15 | + {%- assign article_url = page.slug -%} |
| 16 | + |
| 17 | + {%- if article_url != "" -%} |
| 18 | + {%- assign all_comments = site.data.welcomments[article_url] -%} |
| 19 | + {%- else -%} |
| 20 | + {%- assign all_comments = site.data.welcomments["___root___"] -%} |
| 21 | + {%- endif -%} |
| 22 | + |
| 23 | + {%- if all_comments.size > 0 -%} |
| 24 | + <h3 id="welcomments__comment-count-title" class="welcomments__comments-title">{{ all_comments.size }} comment{%- if all_comments.size > 1 -%}s{%- endif -%}</h3> |
| 25 | + {%- else -%} |
| 26 | + <h3 id="welcomments__comment-count-title" class="welcomments__comments-title">No comments yet!</h3> |
| 27 | + {%- endif -%} |
| 28 | + |
| 29 | + <section id="welcomments__comment-container" class="welcomments__comment-container"> |
| 30 | + {%- if all_comments.size > 0 -%} |
| 31 | + {%- assign parent_comments = all_comments | where_exp: "c", "c.replying_to == nil" | sort: 'id' -%} |
| 32 | + |
| 33 | + {%- for comment_map in parent_comments -%} |
| 34 | + {%- assign comment = comment_map -%} |
| 35 | + {%- include welcomments/single_comment.html |
| 36 | + nesting_level = 0 |
| 37 | + all_comments = all_comments |
| 38 | + comment = comment |
| 39 | + api_url = api_url |
| 40 | + website_id = include.website_id -%} |
| 41 | + {%- endfor -%} |
| 42 | + {%- endif -%} |
| 43 | + </section> |
| 44 | + |
| 45 | + {%- include welcomments/comment_form.html |
| 46 | + api_url = api_url |
| 47 | + website_id = include.website_id -%} |
| 48 | +</div> |
| 49 | + |
| 50 | +<!-- |
| 51 | + Contains some basic comment section styling, so that you can get started quickly |
| 52 | + without having to come up with your own CSS if you don't want to. |
| 53 | + --> |
| 54 | +<link rel="stylesheet" href="https://cdn.welcomments.io/welcomments.css" /> |
| 55 | + |
| 56 | +<!-- |
| 57 | + Includes the optional Welcomments Javascript file. It enhances the user experience |
| 58 | + by sending the comment form fields without the user having to leave your site. |
| 59 | +
|
| 60 | + It also saves the comment to the local storage while your site is being rebuilt so |
| 61 | + that your users don't get confused and resubmit the same comment multiple times. If |
| 62 | + there are comments that other users posted that haven't been rebuilt as a part of your |
| 63 | + site yet, this script also "fills in" them from the Welcomments Pending Comments API. |
| 64 | +
|
| 65 | + It's gzipped and weighs just about 2.1kB. If you want, you can just delete this line |
| 66 | + and your comment form works just fine but is a bit less interactive! :-) |
| 67 | + --> |
| 68 | +<script defer id="welcomments__script" src="https://cdn.welcomments.io/welcomments.js" type="text/javascript"></script> |
| 69 | +<script type="text/javascript"> |
| 70 | + welcomments = { |
| 71 | + apiUrl: "{{ api_url }}", |
| 72 | + |
| 73 | + // Updates the "x comments" title when there are comments that are dynamically loaded. |
| 74 | + // By default, the result of this function becomes the innerText of an element with the |
| 75 | + // id "welcomments__comment-count-title". It's okay if that element doesn't exist. |
| 76 | + commentCountTitleFactory: function (commentCount) { |
| 77 | + if (commentCount === 0) { |
| 78 | + return "No comments yet!"; |
| 79 | + } |
| 80 | + |
| 81 | + return commentCount === 1 ? "One comment" : `${commentCount} comments`; |
| 82 | + }, |
| 83 | + |
| 84 | + // Builds comments dynamically when they are not part of the static HTML of the rest of |
| 85 | + // your Jekyll website yet. Should return the desired HTML as a string. By default, appends |
| 86 | + // the resulting HTML as a child element into the correct position in an element with the |
| 87 | + // id "welcomments__comment-container". |
| 88 | + placeholderCommentFactory: function (comment) { |
| 89 | + var year = new Intl.DateTimeFormat("en", {year: "numeric"}).format(comment.date); |
| 90 | + var month = new Intl.DateTimeFormat("en", {month: "long"}).format(comment.date); |
| 91 | + var day = new Intl.DateTimeFormat("en", {day: "2-digit"}).format(comment.date); |
| 92 | + var avatarUrl = comment.author.avatar_url || |
| 93 | + `https://eu.ui-avatars.com/api/?background=random&name=${comment.author.name}` |
| 94 | + |
| 95 | + return `{%- include welcomments/template.html |
| 96 | + id = "${comment.id}" |
| 97 | + element_id = "welcomments__comment-${comment.id}" |
| 98 | + element_classname = "welcomments__comment" |
| 99 | + nesting_level = "0" |
| 100 | + formatted_date = "${month} ${day}, ${year}" |
| 101 | + author_name = "${comment.author.name}" |
| 102 | + author_website = "${comment.author.website}" |
| 103 | + author_avatar_url = "${avatarUrl}" |
| 104 | + message = "${comment.message}" |
| 105 | + api_url = api_url |
| 106 | + website_id = include.website_id -%}`; |
| 107 | + }, |
| 108 | + }; |
| 109 | +</script> |
0 commit comments