Skip to content

Commit

Permalink
Split the comment template into a separate template
Browse files Browse the repository at this point in the history
  • Loading branch information
offbyone committed Jul 11, 2023
1 parent 97de71f commit cd462fc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 95 deletions.
98 changes: 3 additions & 95 deletions themes/offby1/templates/article.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -89,101 +89,9 @@
</div>
{% endif %}
{% if article.toot %}
<div class="comments">
<h3>Feedback</h3>
<p>You can reply to <a href="https://{{article.toot.domain}}/{{article.toot.username}}/{{article.toot.id}}">this post via Mastodon</a>: <button id="replyButton" href="https://{{ article.toot.domain }}/{{ article.toot.username }}/{{ article.toot.id }}">Reply</button></p>
<p id="mastodon-comments-list">Waiting to load comments</p>
<dialog id="toot-reply" class="mastodon" data-component="dialog">
<h3>Reply to {{ article.toot.username }}'s post</h3>
<p>
With an account on the Fediverse or Mastodon, you
can respond to this post. Since Mastodon is
decentralized, you can use your existing account
hosted by another Mastodon server or compatible
platform if you don't have an account on this one.
</p>
<p>Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.</p>
<div class="copypaste">
<input type="text" readonly="" value="https://{{ article.toot.domain }}/{{ article.toot.username }}/{{ article.toot.id }}">
<button class="button" id="copyButton">Copy</button>
<button class="button" id="cancelButton">Close</button>
</div>
</dialog>
<noscript><p>You need JavaScript to view the comments.</p></noscript>
<script type="text/javascript">
const dialog = document.querySelector('dialog');
document.getElementById('replyButton').addEventListener('click', () => {
dialog.showModal();
});
document.getElementById('copyButton').addEventListener('click', () => {
navigator.clipboard.writeText('https://{{ article.toot.domain }}/@{{ article.toot.username }}/{{ article.toot.id }}');
});
document.getElementById('cancelButton').addEventListener('click', () => {
dialog.close();
});
dialog.addEventListener('keydown', e => {
if (e.key === 'Escape') dialog.close();
});
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("mastodon-comments-list").innerHTML = "Loading comments...";
fetch('https://{{ article.toot.domain }}/api/v1/statuses/{{ article.toot.id }}/context')
.then(function(response) {
return response.json();
})
.then(function(data) {
if(data &&
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 post-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>No comments found. I'd love to hear from you!</p>";
}
});
});
</script>
</div>
{% with toot=article.toot %}
{% include 'federated_comments.html.j2' %}
{% endwith %}
{% else %}
<div class="no-comments">
<h3>Feedback</h3>
Expand Down
95 changes: 95 additions & 0 deletions themes/offby1/templates/federated_comments.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<div class="comments">
<h3>Feedback</h3>
<p>You can reply to <a href="https://{{toot.domain}}/{{toot.username}}/{{toot.id}}">this post via Mastodon</a>: <button id="replyButton" href="https://{{ toot.domain }}/{{ toot.username }}/{{ toot.id }}">Reply</button></p>
<p id="mastodon-comments-list">Waiting to load comments</p>
<dialog id="toot-reply" class="mastodon" data-component="dialog">
<h3>Reply to {{ toot.username }}'s post</h3>
<p>
With an account on the Fediverse or Mastodon, you
can respond to this post. Since Mastodon is
decentralized, you can use your existing account
hosted by another Mastodon server or compatible
platform if you don't have an account on this one.
</p>
<p>Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.</p>
<div class="copypaste">
<input type="text" readonly="" value="https://{{ toot.domain }}/{{ toot.username }}/{{ toot.id }}">
<button class="button" id="copyButton">Copy</button>
<button class="button" id="cancelButton">Close</button>
</div>
</dialog>
<noscript><p>You need JavaScript to view the comments.</p></noscript>
<script type="text/javascript">
const dialog = document.querySelector('dialog');
document.getElementById('replyButton').addEventListener('click', () => {
dialog.showModal();
});
document.getElementById('copyButton').addEventListener('click', () => {
navigator.clipboard.writeText('https://{{ toot.domain }}/@{{ toot.username }}/{{ toot.id }}');
});
document.getElementById('cancelButton').addEventListener('click', () => {
dialog.close();
});
dialog.addEventListener('keydown', e => {
if (e.key === 'Escape') dialog.close();
});
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("mastodon-comments-list").innerHTML = "Loading comments...";
fetch('https://{{ toot.domain }}/api/v1/statuses/{{ toot.id }}/context')
.then(function(response) {
return response.json();
})
.then(function(data) {
if(data &&
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 post-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>No comments found. I'd love to hear from you!</p>";
}
});
});
</script>
</div>

0 comments on commit cd462fc

Please sign in to comment.