From 23785fbb8e22a7d169e0cf1605d6bc0bd05f320f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 4 Apr 2021 01:54:29 +0200 Subject: [PATCH] made blog category links a lot nicer --- index.html | 1 + node/blog.js | 2 +- scripts/blog.js | 29 +++++++++++++++++------------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index fdfae4a..963d136 100644 --- a/index.html +++ b/index.html @@ -164,6 +164,7 @@ +

Blog

diff --git a/node/blog.js b/node/blog.js index 74a8faf..099a3e9 100644 --- a/node/blog.js +++ b/node/blog.js @@ -42,7 +42,7 @@ fs.readFile(`${folder}index.html`, function (_, html) { document.querySelector('meta[property="og:description"]').setAttribute("content", post.summary); document.getElementById("nav-items").innerHTML = /*html*/ ` - Back to Main Page + Back to Main Page ${last ? /*html*/ `Previous ${post.cat[0]} Post` : ""} ${next ? /*html*/ `Next ${post.cat[0]} Post` : ""} `; diff --git a/scripts/blog.js b/scripts/blog.js index b489012..b8ea61c 100644 --- a/scripts/blog.js +++ b/scripts/blog.js @@ -9,29 +9,32 @@ $.ajax({ url: "blog/src/posts.json", cache: false, success: function (json) { - let category = window.location.search.match(/cat=([^&]+)/); - populateBlog(json, category && decodeURI(category[1]) || "Featured"); + let category = window.location.hash.match(/#blog-(.+)/); + populateBlog(json, category && decodeURI(category[1]) || "featured"); forceToAnchor(); } }); function populateBlog(json, currCat) { + let anchors = $('#blog-cat-anchors'); let archive = $('#blog-archive'); let list = $('#blog-list'); let cats = $('#blog-cats'); + anchors.html(""); archive.html(""); list.html(""); cats.html(""); // force all and featured categories to be first - addCatButton(json, cats, "All Posts", currCat); - addCatButton(json, cats, "Featured", currCat); + addCategory(json, cats, anchors, "All Posts", currCat); + addCategory(json, cats, anchors, "Featured", currCat); for (let i = json.length - 1; i >= 0; i--) { let post = json[i]; for (let cat of post.cat) - addCatButton(json, cats, cat, currCat); - if (currCat === "All Posts" || post.cat.includes(currCat)) { + addCategory(json, cats, anchors, cat, currCat); + let catIds = post.cat.map(c => c.toLowerCase().replace(" ", "-")); + if (currCat === "all-posts" || catIds.includes(currCat)) { let p = /*html*/ `

@@ -51,18 +54,20 @@ function populateBlog(json, currCat) { } if (!archive.html()) - archive.html(`There are no archived ${currCat} posts.`); + archive.html(`There are no archived posts in this category.`); if (!list.html()) populateBlog(json, "Featured"); } -function addCatButton(json, cats, cat, currCat) { - let catAnchor = `blog-cat-${cat.toLowerCase().replace(" ", "_")}`; +function addCategory(json, cats, anchors, cat, currCat) { + let catId = cat.toLowerCase().replace(" ", "-"); + let catAnchor = `blog-cat-${catId}`; if (!$(`#${catAnchor}`).length) { - cats.append( /*html*/ ``); + cats.append( /*html*/ ``); $(`#${catAnchor}`).on('click', function () { - populateBlog(json, cat); - history.replaceState(null, null, `?cat=${cat}${location.hash}`); + populateBlog(json, catId); + history.replaceState(null, null, `#blog-${catId}`); }); + anchors.append( /*html*/ ``); } } \ No newline at end of file