$("#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.search.match(/cat=([^&]+)/); populateBlog(json, category && decodeURI(category[1]) || "Featured"); forceToAnchor(); } }); function populateBlog(json, currCat) { 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 addCatButton(json, cats, "All Posts", currCat); addCatButton(json, cats, "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)) { 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 ${currCat} posts.`); if (!list.html()) populateBlog(json, "Featured"); } function addCatButton(json, cats, cat, currCat) { let catAnchor = `blog-cat-${cat.toLowerCase().replace(" ", "_")}`; if (!$(`#${catAnchor}`).length) { cats.append( /*html*/ ``); $(`#${catAnchor}`).on('click', function () { populateBlog(json, cat); history.replaceState(null, null, `?cat=${cat}${location.hash}`); }); } }