index.js (2081B)
1 import CommonHead from "../components/CommonHead.js"; 2 import Footer from "../components/Footer.js"; 3 import Header from "../components/Header.js"; 4 import HeadTitle from "../components/HeadTitle.js"; 5 import UnorderedList from "../components/UnorderedList.js"; 6 7 import { site } from "../config.js"; 8 9 function Home({ quotesANDauthors }) { 10 return ( 11 <div className="site"> 12 <CommonHead /> 13 <HeadTitle title="Home" ogtitle="Home" /> 14 <Header pageTitle={`${site.title} (${quotesANDauthors.length})`} /> 15 <main id="main" className="main"> 16 <div className="home-sections-container"> 17 <div className="home-sections"> 18 <section id="recent-posts" className="home-section"> 19 <header> 20 <h2 className="home-section-title title">{site.title}</h2> 21 </header> 22 <UnorderedList arr={quotesANDauthors} /> 23 </section> 24 </div> 25 </div> 26 </main> 27 <Footer /> 28 </div> 29 ); 30 } 31 32 export async function getStaticProps() { 33 const { readdir } = require("fs/promises"); 34 const { join } = require("path"); 35 36 const { 37 createQuoteObj, 38 errHandler, 39 getDetailsANDQuotes, 40 readFrontMatter, 41 } = require("../helperFns.js"); 42 43 const quotesANDauthors = []; 44 try { 45 const authors = await readdir("quotes"); 46 for (const author of authors) { 47 const allFiles = await readdir(join("quotes", author)); 48 const { details, quotes } = getDetailsANDQuotes(allFiles); 49 50 if (!details) throw new Error(`Error finding details`); 51 if (quotes.length < 1) throw new Error(`Not enough quotes`); 52 53 for (const quote of quotes) { 54 const { data } = await readFrontMatter(author, quote); 55 56 const { draft } = data; 57 if (draft) continue; 58 59 const quoteObj = createQuoteObj(author, data, quote); 60 quotesANDauthors.push(quoteObj); 61 } 62 } 63 quotesANDauthors.sort((a, b) => b.date.localeCompare(a.date)); 64 return { props: { quotesANDauthors } }; 65 } catch (err) { 66 errHandler(err); 67 } 68 } 69 70 export default Home;