Implement replies in post header

This commit is contained in:
Dominic Ferrando
2025-04-19 16:40:43 -04:00
parent 656595e47e
commit 34d3ec5366
5 changed files with 82 additions and 24 deletions
+34
View File
@@ -0,0 +1,34 @@
const posts = Array.from(document.querySelectorAll("article"));
const replies = {};
for (const post of posts) {
const postId = +post.id.split("-")[1];
const postBody = post.querySelector("p").textContent;
const replyLinkIds = [...postBody.matchAll(/(?:^|\s)>>(\d+)\b/g)].map(m => parseInt(m[1]));
for (const replyLinkId of replyLinkIds) {
replies[replyLinkId] = replies[replyLinkId] ?
replies[replyLinkId].add(postId) :
replies[replyLinkId] = new Set([postId]);
}
}
for (const postId in replies) {
const post = $(`post-${postId}`);
const postRepliesContainer = post.querySelector(".post-replies");
const replyIds = Array.from(replies[postId]);
for (const replyId of replyIds) {
const replyLink = document.createElement("a");
replyLink.textContent = `>>${replyId}`;
replyLink.classList.add("reply-link-header");
replyLink.setAttribute("href", `#post-${replyId}`);
replyLink.onmouseover = (e) => highlightPost(+replyId, e);
replyLink.onmouseleave = (e) => highlightPost(+replyId, e, false);
replyLink.onclick = onReplyLinkClick;
postRepliesContainer.append(replyLink);
}
}