diff --git a/package.json b/package.json index ea110a1..82bcc5a 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "lint": "prettier --check . '!{CODE_OF_CONDUCT.md,LICENSE.md}' && eslint .", "format": "prettier --write .", "prepare": "npm run prebuild && npm run format", - "prebuild": "node utils/sortJSON.js && node --experimental-json-modules utils/compileReadme.js", + "prebuild": "node utils/sortJSON.js && node utils/compileReadme.js", "awesome-lint": "awesome-lint" }, "engines": { diff --git a/utils/compileReadme.js b/utils/compileReadme.js index 6afb5ef..264893f 100644 --- a/utils/compileReadme.js +++ b/utils/compileReadme.js @@ -1,15 +1,35 @@ import fs from 'fs'; -import entries from '../src/lib/data/entries.json' assert { type: 'json' }; -let regions = new Set(); -entries.forEach((entry) => { - entry.region.forEach((region) => { - regions.add(region); - }); +const filepath = 'src/lib/data/entries.json'; + +fs.readFile(filepath, 'utf-8', (err, data) => { + if (err) { + console.error(`Error reading file: ${err.message}`); + return; + } + + try { + const entries = JSON.parse(data); + const readmeContent = generateReadmeContent(entries); + writeFile('./README.md', readmeContent); + } catch (error) { + console.error(`Error parsing JSON data: ${error.message}`); + } }); -regions = Array.from(regions).sort(); -const header = `# Awesome Digital History [![Awesome](https://awesome.re/badge.svg)](https://awesome.re) +function generateReadmeContent(entries) { + const regions = [...new Set(entries.flatMap((entry) => entry.region))].sort(); + + const header = generateHeader(); + const toc = generateTableOfContents(regions); + const sections = generateSections(entries, regions); + const footer = generateFooter(); + + return `${header}${toc}${sections}${footer}`; +} + +function generateHeader() { + return `# Awesome Digital History [![Awesome](https://awesome.re/badge.svg)](https://awesome.re) [](https://maehr.github.io/awesome-digital-history/) @@ -21,60 +41,82 @@ Finding aids for textual and multimedia [primary sources](https://en.wikipedia.o 🎉 Check out our new website at [awesome-digital-history.pages.dev](https://awesome-digital-history.pages.dev/) with more information, filters, and a search function. 🎉 `; +} -let toc = `## Contents +function generateTableOfContents(regions) { + let toc = `## Contents - [Archives and primary sources](#archives-and-primary-sources) `; -regions.forEach((region) => { - toc += ` - [${region}](#${region.toString().toLowerCase().replace(/ /g, '-')})\n`; -}); - -toc += `- [Learning](#learning) + toc += regions + .map((region) => ` - [${region}](#${region.toLowerCase().replace(/ /g, '-')})\n`) + .join(''); + toc += `- [Learning](#learning) - [More Awesome](#more-awesome) - [Contribute](#contribute) `; -let sections = `## Archives and primary sources + return toc; +} + +function generateSections(entries, regions) { + let sections = `## Archives and primary sources `; -regions.forEach((region) => { - sections += `### ${region}\n\n`; - const entriesInRegion = entries.filter((entry) => entry.region.includes(region)); - entriesInRegion.forEach((entry) => { - sections += `- [${entry.title}](${entry.url}) - ${entry.description}\n`; + regions.forEach((region) => { + sections += `### ${region}\n\n`; + const entriesInRegion = entries.filter((entry) => entry.region.includes(region)); + entriesInRegion.forEach((entry) => { + sections += `- [${entry.title}](${entry.url}) - ${entry.description}\n`; + }); + sections += '\n'; }); - sections += '\n'; -}); -sections += `## Learning\n\n`; -const learningMaterials = entries.filter( - (entry) => entry.region.length === 0 && entry.type.includes('learning materials') -); -learningMaterials.forEach((entry) => { - sections += `- [${entry.title}](${entry.url}) - ${entry.description}\n`; -}); -sections += '\n'; - -sections += `## More Awesome\n\n`; -const moreAwesome = entries.filter( - (entry) => entry.region.length === 0 && !entry.type.includes('learning materials') -); -moreAwesome.forEach((entry) => { - sections += `- [${entry.title}](${entry.url}) - ${entry.description}\n`; -}); -sections += '\n'; + sections += generateLearningSection(entries); + sections += generateMoreAwesomeSection(entries); + + return sections; +} + +function generateLearningSection(entries) { + let section = `## Learning\n\n`; + const learningMaterials = entries.filter( + (entry) => entry.region.length === 0 && entry.type.includes('learning materials') + ); + learningMaterials.forEach((entry) => { + section += `- [${entry.title}](${entry.url}) - ${entry.description}\n`; + }); + return section + '\n'; +} + +function generateMoreAwesomeSection(entries) { + let section = `## More Awesome\n\n`; + const moreAwesome = entries.filter( + (entry) => entry.region.length === 0 && !entry.type.includes('learning materials') + ); + moreAwesome.forEach((entry) => { + section += `- [${entry.title}](${entry.url}) - ${entry.description}\n`; + }); + return section + '\n'; +} -const footer = `## Contribute +function generateFooter() { + return `## Contribute Contributions welcome! Read the [CONTRIBUTING.md](https://github.com/maehr/awesome-digital-history/blob/main/CONTRIBUTING.md) first. `; - -fs.writeFile('./README.md', header + toc + sections + footer, (err) => { - if (err) throw err; - console.log(`README.md has been created.`); -}); +} + +function writeFile(filePath, content) { + fs.writeFile(filePath, content, (err) => { + if (err) { + console.error(`Error writing file: ${err.message}`); + return; + } + console.log(`README.md has been created.`); + }); +} diff --git a/utils/sortJSON.js b/utils/sortJSON.js index b5829d6..842dea0 100644 --- a/utils/sortJSON.js +++ b/utils/sortJSON.js @@ -23,8 +23,7 @@ fs.readFile(filepath, 'utf-8', (err, data) => { period: [] }; - Object.assign(defaultStructure, entry); - return defaultStructure; + return { ...defaultStructure, ...entry }; }); // Sort the JSON array by the "title" field