index.js (1685B)
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 WordCloud from "../../components/WordCloud.js"; 6 7 function Tags({ tags }) { 8 return ( 9 <div className="site"> 10 <CommonHead /> 11 <HeadTitle title="Tags" ogtitle="Tags" /> 12 <Header pageTitle={`Tags (${tags.length})`} /> 13 <WordCloud prefix="tags" arr={tags} /> 14 <Footer /> 15 </div> 16 ); 17 } 18 19 export async function getStaticProps() { 20 const { readdir } = require("fs/promises"); 21 const { join } = require("path"); 22 23 const { 24 errHandler, 25 getDetailsANDQuotes, 26 readFrontMatter, 27 } = require("../../helperFns.js"); 28 29 const tags = {}; 30 try { 31 const authors = await readdir("quotes"); 32 33 for (const author of authors) { 34 const allFiles = await readdir(join("quotes", author)); 35 const { details, quotes } = getDetailsANDQuotes(allFiles); 36 37 if (!details) throw new Error(`Error finding details`); 38 if (quotes.length < 1) throw new Error(`Not enough quotes`); 39 40 for (const quote of quotes) { 41 const { data } = await readFrontMatter(author, quote); 42 const { draft } = data; 43 if (draft) continue; 44 45 const fileTags = data.tags; 46 for (const tag of fileTags) !tags[tag] ? (tags[tag] = 1) : tags[tag]++; 47 } 48 } 49 const tagsArr = Object.keys(tags).map((_) => ({ name: _, count: tags[_] })); 50 51 tagsArr.sort((a, b) => a.name.localeCompare(b.name)); 52 53 return { 54 props: { tags: tagsArr }, 55 }; 56 } catch (err) { 57 errHandler(err); 58 } 59 } 60 61 export default Tags;