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

1 line
11 KiB
JavaScript
Raw Normal View History

const scrollLimit=150;function goToURL(e){let t=e.split("/");if("/blog/"!==e&&"/blog"!==e){if("post"!==t[2])return"category"===t[2]?t[3]?void loadPostsByCategory(t[t.length-1]):void loadAllCategories():void show404();loadIndividualPost(t[t.length-1]).catch((e=>console.log(e)))}else loadHomeContent()}function createLargePost(e){let t=document.createElement("div");t.classList.add("outerContent");let n=document.createElement("img");n.className="banner",n.src=e.headerImg,n.alt=e.title,t.appendChild(n);let a=document.createElement("div");a.classList.add("content");let o=document.createElement("div");o.classList.add("postContent");let s="";return e.categories.split(", ").forEach((t=>{s+=`<a href="/blog/category/${t}" class="link">${t}</a>`,e.categories.split(", ").length>1&&(s+=", ")})),window.categories=s,o.innerHTML=`\n <h2>${e.title}</h2>\n <h3>Last updated: ${e.dateModified} | ${s}</h3>\n <p>${e.abstract}</p>\n <a href="/blog/post/${e.title}#disqus_thread" class="btn btnPrimary">See Post</a>\n `,a.appendChild(o),t.appendChild(a),t}function loadHomeContent(){fetch("/api/blog/post").then((e=>e.json().then((e=>{for(let t=0;t<e.length;t++){if(1===e[t].featured){let n=document.createElement("section");n.classList.add("largePost"),n.id="featuredPost";let a=document.createElement("h1");a.innerHTML="featured post",n.appendChild(a);let o=createLargePost(e[t]);n.appendChild(o),document.querySelector("#main").prepend(n)}if(0===t){let n=document.createElement("section");n.classList.add("largePost"),n.id="latestPost";let a=document.createElement("h1");a.innerHTML="latest post",n.appendChild(a);let o=createLargePost(e[t]);n.appendChild(o),document.querySelector("#main").prepend(n)}}}))))}async function getLatestAndFeaturedPosts(){return[await fetch("/api/blog/post/latest").then((e=>e.json())),await fetch("/api/blog/post/featured").then((e=>e.json()))]}function csvToArray(e){let t="",n=[""],a=0,o=!0,s=null;for(s of e)'"'===s?(o&&s===t&&(n[a]+=s),o=!o):","===s&&o?s=n[++a]="":"\n"===s&&o?("\r"===t&&(row[a]=row[a].slice(0,-1)),n=n[++r]=[s=""],a=0):n[a]+=s,t=s;return n}async function getCategories(){let e=await fetch("/api/blog/categories").then((e=>e.json())),t=[];return e.forEach((e=>t.push(csvToArray(e.categories)))),t}function createCategories(e){let t="";return e.forEach((e=>e.forEach((e=>t+=`<a href="/blog/category/${e}" class="link">${e}</a>`)))),t}function createButtonCategories(e){let t="";return e.forEach((e=>e.forEach((e=>t+=`<a href="/blog/category/${e}" class="btn btnOutline">${e}</a>`)))),t}async function createSideContent(){let e=await getLatestAndFeaturedPosts(),t=e[0],n=e[1],a=createCategories(await getCategories()),o=document.createElement("aside");return o.classList.add("sideContent"),o.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-5.373 12-12-5.373-12-12-12zm-2 16h-2v-6h2v6zm-1-6.891c-.607 0-1.1-.496-1.1-1.109 0-.612.492-1.109 1.1-1.109s1.1.497 1.1 1.109c0 .613-.493 1.109-1.1 1.109zm8 6.891h-1.998v-2.861c0-1.881-2.002-1.722-2.002 0v2.861h-2v-6h2v1.093c.872-1.616 4-1.736 4 1.548v3.359z"/>\n </svg>\n </a>\n \n <a href="mailto:ro