[tag].js (2750B)
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 function Tag({ tag, quotes }) { 8 const quoteCount = quotes.length; 9 return ( 10 <div className="site"> 11 <CommonHead /> 12 <HeadTitle title={tag} ogtitle={tag} /> 13 <Header pageTitle={`Tag: ${tag} (${quoteCount})`} titleLink="/tags" /> 14 <main id="main" className="main"> 15 <UnorderedList arr={quotes} /> 16 </main> 17 <Footer /> 18 </div> 19 ); 20 } 21 22 export async function getStaticPaths() { 23 const { readdir } = require("fs/promises"); 24 const { join } = require("path"); 25 26 const { 27 errHandler, 28 getDetailsANDQuotes, 29 readFrontMatter, 30 } = require("../../helperFns.js"); 31 32 try { 33 const allAuthors = await readdir("quotes"); 34 35 const tagsSet = new Set(); 36 for (const author of allAuthors) { 37 const allFiles = await readdir(join("quotes", author)); 38 const { quotes } = getDetailsANDQuotes(allFiles); 39 if (quotes.length < 1) throw new Error(`Not enough quotes`); 40 41 for (const quote of quotes) { 42 const { data } = await readFrontMatter(author, quote); 43 44 const { draft, tags } = data; 45 if (draft) continue; 46 tags.forEach((_) => tagsSet.add(_)); 47 } 48 } 49 const paths = [...tagsSet].map((_) => ({ params: { tag: _ } })); 50 return { 51 paths, 52 fallback: false, 53 }; 54 } catch (err) { 55 errHandler(err); 56 } 57 } 58 59 export async function getStaticProps({ params: { tag } }) { 60 const { readdir } = require("fs/promises"); 61 const { join } = require("path"); 62 63 const { 64 createQuoteObj, 65 errHandler, 66 getDetailsANDQuotes, 67 readFrontMatter, 68 } = require("../../helperFns.js"); 69 70 try { 71 const authors = await readdir(join("quotes")); 72 const quotesArr = []; 73 for (const author of authors) { 74 const allFiles = await readdir(join("quotes", author)); 75 const { details, quotes } = getDetailsANDQuotes(allFiles); 76 77 if (!details) throw new Error(`Error finding details`); 78 if (quotes.length < 1) throw new Error(`Not enough quotes`); 79 80 for (const quote of quotes) { 81 const { data } = await readFrontMatter(author, quote); 82 const { draft, tags } = data; 83 if (draft) continue; 84 85 if (tags.includes(tag)) { 86 const quoteObj = createQuoteObj(author, data, quote); 87 quotesArr.push(quoteObj); 88 } 89 } 90 } 91 quotesArr.sort((a, b) => b.date.localeCompare(a.date)); 92 return { props: { tag, quotes: quotesArr } }; 93 } catch (err) { 94 errHandler(err); 95 } 96 } 97 98 export default Tag;