my-portfolio/dist/blog/js/index.js

1 line
27 KiB
JavaScript
Raw Normal View History

const scrollLimit=150;function goToURL(t){let e=t.split("/");if("/blog/"!==t&&"/blog"!==t)if("post"!==e[2]){if("category"===e[2])return e[3]?void loadPostsByCategory(e[e.length-1]):void loadAllCategories().catch((t=>console.log(t)));if("search"===e[2]&&e[3])loadSearchResults(e[e.length-1]);else{if("policy"===e[2]){if("privacy"===e[3])return void loadPrivacyPolicy();if("cookie"===e[3])return void loadCookiePolicy()}show404()}}else loadIndividualPost(e[e.length-1]).catch((t=>console.log(t)));else loadHomeContent()}function createFormattedDate(t){return new Date(t).toLocaleDateString("en-GB",{day:"2-digit",month:"short",year:"numeric"})}function createLargePost(t){let e=document.createElement("div");e.classList.add("outerContent");let n=document.createElement("img");n.className="banner",n.src=t.headerImg,n.alt=t.title,e.appendChild(n);let o=document.createElement("div");o.classList.add("content");let a=document.createElement("div");a.classList.add("postContent");let i="";t.categories.split(", ").forEach((e=>{i+=`<a href="/blog/category/${e}" class="link">${e}</a>`,t.categories.split(", ").length>1&&(i+=", ")})),i.endsWith(", ")&&(i=i.substring(0,i.length-2));let r=createFormattedDate(t.dateModified);return a.innerHTML=`\n <h2>${t.title}</h2>\n <h3>Last updated: ${r} | ${i}</h3>\n <p>${t.abstract}</p>\n <a href="/blog/post/${t.title}" class="btn btnPrimary">See Post</a>\n `,o.appendChild(a),e.appendChild(o),e}function loadHomeContent(){fetch("/api/blog/post").then((t=>t.json().then((t=>{for(let e=0;e<t.length;e++){if(1===t[e].featured){let n=document.createElement("section");n.classList.add("largePost"),n.id="featuredPost";let o=document.createElement("h1");o.innerHTML="featured post",n.appendChild(o);let a=createLargePost(t[e]);n.appendChild(a),document.querySelector("#main").appendChild(n)}if(1===e){let n=document.createElement("section");n.classList.add("largePost"),n.id="latestPost";let o=document.createElement("h1");o.innerHTML="latest post",n.appendChild(o);let a=createLargePost(t[e]);n.appendChild(a),document.querySelector("#main").appendChild(n)}}}))))}async function getLatestAndFeaturedPosts(){return[await fetch("/api/blog/post/latest").then((t=>t.json())),await fetch("/api/blog/post/featured").then((t=>t.json()))]}function csvToArray(t){let e="",n=[""],o=0,a=!0,i=null;for(i of t)'"'===i?(a&&i===e&&(n[o]+=i),a=!a):","===i&&a?i=n[++o]="":"\n"===i&&a?("\r"===e&&(row[o]=row[o].slice(0,-1)),n=n[++r]=[i=""],o=0):n[o]+=i,e=i;return n}async function getCategories(){let t=await fetch("/api/blog/categories").then((t=>t.json())),e=[];return t.forEach((t=>e=e.concat(csvToArray(t.categories.replace(/\s*,\s*/g,","))))),[...new Set(e)]}function createCategories(t){let e="";return t.forEach((t=>e+=`<a href="/blog/category/${t}" class="link">${t}</a>`)),e}function createButtonCategories(t){let e="";return t.forEach((t=>t.forEach((t=>e+=`<a href="/blog/category/${t}" class="btn btnOutline">${t}</a>`)))),e}async function createSideContent(){let t=await getLatestAndFeaturedPosts(),e=t[0],n=t[1],o=createCategories(await getCategories()),a=document.createElement("aside");return a.classList.add("sideContent"),a.innerHTML=`\n <div class="authorInfo">\n <div class="picture">\n <img src="/imgs/profile.jpg"\n alt="My professional picture taken in brighton near \n north street at night wearing a beige jacket and checkered shirt"\n class="profile">\n <p>Rohit Pai</p>\n </div>\n \n <a href="https://linkedin.com/in/rohitpai98">\n <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">\n <path d="M12 0c-6.627 0-12 5.373-12 12s5.373 12 12 12 12