diff --git a/client/public/posts.mjs b/client/public/posts.mjs index f1c4379..44efb35 100644 --- a/client/public/posts.mjs +++ b/client/public/posts.mjs @@ -4,6 +4,21 @@ function toMonthYearString(str) { const date = new Date(str) return `${date.toLocaleString('default', { month: 'long' })} ${date.getFullYear()}` } +async function showPost(id) { + const post_list = await getPosts(id) + const post = post_list[0] + const title = document.createElement("h1") + title.textContent = post.title + + const postsDOM = document.getElementById("posts") + postsDOM.appendChild(title) + + post.content.split('\n').forEach(paragraph => { + const p = document.createElement("p") + p.textContent = paragraph + postsDOM.appendChild(p) + }); +} async function updatePosts() { const posts = await getPosts() @@ -22,7 +37,13 @@ async function updatePosts() { month_lists[i].forEach((post, i) => { const post_DOM = document.createElement("h6") - post_DOM.textContent = `${post.date.substring(0, 10)} - ${post.title}` + post_DOM.textContent = `${post.date.substring(0, 10)} - ` + + const post_link = document.createElement("a") + post_link.href = 'posts?post=' + post._id + post_link.textContent = post.title + post_DOM.appendChild(post_link) + month_DOM.appendChild(post_DOM) }) @@ -30,4 +51,11 @@ async function updatePosts() { }) } -updatePosts() +// Check if a specific post is requested +let url = new URL(document.location.href); +url.searchParams.sort(); +let post_id = url.searchParams.values().next().value; + +const reg = /([0-9]|[a-f]){24}/ +if (post_id && reg.test(post_id)) showPost(post_id) +else updatePosts()