$("#blog-archive-button").on("click", function () { let archive = $('#blog-archive'); archive.toggle(); $(this).html((archive.is(":visible") ? "Hide" : "Show") + " archived posts"); }); $.ajax({ dataType: "json", url: "blog/src/posts.json", cache: false, success: function (json) { let category = window.location.hash.match(/#blog-(.+)/); populateBlog(json, category && decodeURI(category[1]) || "featured"); let anchor = window.location.hash; // blog categories should also move to the blog if (anchor.startsWith("#blog-")) anchor = anchor.substring(0, 5); forceToAnchor(anchor); } }); function populateBlog(json, currentCategory) { const currCat = currentCategory; let archive = $('#blog-archive'); let list = $('#blog-list'); let cats = $('#blog-cats'); archive.html(""); list.html(""); cats.html(""); // force all and featured categories to be first addCategory(json, cats, "All Posts", currCat); addCategory(json, cats, "Featured", currCat); for (let i = json.length - 1; i >= 0; i--) { let post = json[i]; for (let cat of post.cat) addCategory(json, cats, cat, currCat); if (currCat === "all-posts" || post.cat.some(c => c.toLowerCase().replace(" ", "-") === currCat)) { let p = /*html*/ `

${post.name}

${post.summary}
${post.date} ${post.cat.join(", ")}
`; if (post.archived) { archive.append(p); } else { list.append(p); } } } if (!archive.html()) archive.html(`There are no archived posts in this category.`); if (!list.html()) populateBlog(json, "featured"); } function addCategory(json, cats, cat, currCat) { let catId = cat.toLowerCase().replace(" ", "-"); let catAnchor = `blog-cat-${catId}`; if (!$(`#${catAnchor}`).length) { cats.append( /*html*/ ``); $(`#${catAnchor}`).on('click', function () { populateBlog(json, catId); history.replaceState(null, null, `#blog-${catId}`); }); } }