diff --git a/__tests__/__snapshots__/main.test.js.snap b/__tests__/__snapshots__/main.test.js.snap index 08e6e32..3c389b8 100644 --- a/__tests__/__snapshots__/main.test.js.snap +++ b/__tests__/__snapshots__/main.test.js.snap @@ -4,28 +4,28 @@ exports[`given a directory of markdown files a full document is rendered 1`] = ` "# Table of Contents - - [Events in 2019](#2019) - [Events in 2021](#2021) + - [Events in 2019](#2019) -# 2019 +# 2021 -[2] total events for 2019 +[1] total events for 2021 | Date | Event | Title | Slides | Recording | Location | Language | | ---- | ----- | ----- | ------ | --------- | -------- | -------- | -| 2019-9-12 | APIdays Barcelona | Consumer-Driven Contracts: A better approach for API Testing | [Slides](https://slides.com/lirantal/consumer-driven-contracts) | [Recording](https://www.youtube.com/watch?v=zfGKX5iKSis&list=PLmEaqnTJ40Oqo9VlbcUakVmFZgx5weLrs&index=25&t=141s&ab_channel=apidays) | ES | English | -| 2019-9-27 | JSConf Budapest | StrangerDanger: Finding Security Vulnerabilities Before They Find You! | | [Recording](https://www.youtube.com/watch?v=3H8pF6yoSgU&list=PL37ZVnwpeshEMCvdYDdZ09Sy-toTftWu0&index=15&ab_channel=JSConf) | HU | English | +| 2021-7-21 | Cyber Week | Are We Forever Doomed By Software Supply Chain Risks? | | [Recording](https://www.youtube.com/watch?v=x74sMCaZKbg&ab_channel=Snyk) | IL | English | -# 2021 +# 2019 -[1] total events for 2021 +[2] total events for 2019 | Date | Event | Title | Slides | Recording | Location | Language | | ---- | ----- | ----- | ------ | --------- | -------- | -------- | -| 2021-7-21 | Cyber Week | Are We Forever Doomed By Software Supply Chain Risks? | | [Recording](https://www.youtube.com/watch?v=x74sMCaZKbg&ab_channel=Snyk) | IL | English | +| 2019-9-27 | JSConf Budapest | StrangerDanger: Finding Security Vulnerabilities Before They Find You! | | [Recording](https://www.youtube.com/watch?v=3H8pF6yoSgU&list=PL37ZVnwpeshEMCvdYDdZ09Sy-toTftWu0&index=15&ab_channel=JSConf) | HU | English | +| 2019-9-12 | APIdays Barcelona | Consumer-Driven Contracts: A better approach for API Testing | [Slides](https://slides.com/lirantal/consumer-driven-contracts) | [Recording](https://www.youtube.com/watch?v=zfGKX5iKSis&list=PLmEaqnTJ40Oqo9VlbcUakVmFZgx5weLrs&index=25&t=141s&ab_channel=apidays) | ES | English | " @@ -39,28 +39,28 @@ Liran Tal # Table of Contents - - [Events in 2019](#2019) - [Events in 2021](#2021) + - [Events in 2019](#2019) -# 2019 +# 2021 -[2] total events for 2019 +[1] total events for 2021 | Date | Event | Title | Slides | Recording | Location | Language | | ---- | ----- | ----- | ------ | --------- | -------- | -------- | -| 2019-9-12 | APIdays Barcelona | Consumer-Driven Contracts: A better approach for API Testing | [Slides](https://slides.com/lirantal/consumer-driven-contracts) | [Recording](https://www.youtube.com/watch?v=zfGKX5iKSis&list=PLmEaqnTJ40Oqo9VlbcUakVmFZgx5weLrs&index=25&t=141s&ab_channel=apidays) | ES | English | -| 2019-9-27 | JSConf Budapest | StrangerDanger: Finding Security Vulnerabilities Before They Find You! | | [Recording](https://www.youtube.com/watch?v=3H8pF6yoSgU&list=PL37ZVnwpeshEMCvdYDdZ09Sy-toTftWu0&index=15&ab_channel=JSConf) | HU | English | +| 2021-7-21 | Cyber Week | Are We Forever Doomed By Software Supply Chain Risks? | | [Recording](https://www.youtube.com/watch?v=x74sMCaZKbg&ab_channel=Snyk) | IL | English | -# 2021 +# 2019 -[1] total events for 2021 +[2] total events for 2019 | Date | Event | Title | Slides | Recording | Location | Language | | ---- | ----- | ----- | ------ | --------- | -------- | -------- | -| 2021-7-21 | Cyber Week | Are We Forever Doomed By Software Supply Chain Risks? | | [Recording](https://www.youtube.com/watch?v=x74sMCaZKbg&ab_channel=Snyk) | IL | English | +| 2019-9-27 | JSConf Budapest | StrangerDanger: Finding Security Vulnerabilities Before They Find You! | | [Recording](https://www.youtube.com/watch?v=3H8pF6yoSgU&list=PL37ZVnwpeshEMCvdYDdZ09Sy-toTftWu0&index=15&ab_channel=JSConf) | HU | English | +| 2019-9-12 | APIdays Barcelona | Consumer-Driven Contracts: A better approach for API Testing | [Slides](https://slides.com/lirantal/consumer-driven-contracts) | [Recording](https://www.youtube.com/watch?v=zfGKX5iKSis&list=PLmEaqnTJ40Oqo9VlbcUakVmFZgx5weLrs&index=25&t=141s&ab_channel=apidays) | ES | English |
rendered in postcontent
diff --git a/__tests__/md-formatter.test.js b/__tests__/md-formatter.test.js index 8132999..6167e6b 100644 --- a/__tests__/md-formatter.test.js +++ b/__tests__/md-formatter.test.js @@ -14,8 +14,8 @@ test('convert json object to markdown formatting', async () => { }) test('process events json data to provide a complete markdown document', () => { - const events = { - 2016: { + const events = [ + { year: 2016, items: [ { @@ -31,7 +31,7 @@ test('process events json data to provide a complete markdown document', () => { } ] }, - 2021: { + { year: 2021, items: [ { @@ -58,7 +58,7 @@ test('process events json data to provide a complete markdown document', () => { } ] } - } + ] const markdown = getEventsMd(events) expect(markdown).toMatchSnapshot() diff --git a/src/main.js b/src/main.js index cfd57d1..fa08959 100644 --- a/src/main.js +++ b/src/main.js @@ -3,6 +3,7 @@ import path from 'path' import { getAllFiles } from './utils/fs.js' import { convertToJson } from './utils/yaml-parser.js' import { getEventsMd, formatToMarkdown } from './utils/md-formatter.js' +import { createYearBuckets } from './utils/content-manager.js' export { formatToMarkdown, generateGigs, generateDocument } @@ -57,33 +58,20 @@ async function generateGigs({ sourceDirectory }) { } const entriesByBucket = await getEntriesByBuckets(entries) - const entriesForYearMarkdown = getEventsMd(entriesByBucket.bucketsYear) + const entriesForYearMarkdown = getEventsMd(entriesByBucket.bucketsByYear) return entriesForYearMarkdown } async function getEntriesByBuckets(entries) { - const bucketsYear = {} - // const bucketsAll = [] - // const bucketsType = {} - // const bucketsLanguage = {} - // const bucketsCountry = {} - - for (const entry of entries) { - const { attributes } = entry - const { date } = attributes - - const year = new Date(date).getFullYear() - if (bucketsYear[year] === undefined) { - bucketsYear[year] = { - year: year, - items: [entry] - } - } else { - bucketsYear[year].items.push(entry) - } - } + // @TODO these buckets: + // const bucketsAll = [] + // const bucketsType = {} + // const bucketsLanguage = {} + // const bucketsCountry = {} + + const bucketsByYear = createYearBuckets(entries) return { - bucketsYear + bucketsByYear } } diff --git a/src/utils/content-manager.js b/src/utils/content-manager.js new file mode 100644 index 0000000..0195d63 --- /dev/null +++ b/src/utils/content-manager.js @@ -0,0 +1,38 @@ +export { createYearBuckets } + +function sortByDateOrYear(a, b) { + if (a.hasOwnProperty('year') && b.hasOwnProperty('year')) { + return new Date(b.year) - new Date(a.year) + } else { + return new Date(b.attributes.date) - new Date(a.attributes.date) + } +} + +function createYearBuckets(entries) { + const bucketsYear = {} + + // sort entries by date in descending order + entries.sort(sortByDateOrYear) + + // loop them to gather them into yearly buckets + for (const entry of entries) { + const { attributes } = entry + const { date } = attributes + + const year = String(new Date(date).getFullYear()) + if (bucketsYear[year] === undefined) { + bucketsYear[year] = { + year: year, + items: [entry] + } + } else { + bucketsYear[year].items.push(entry) + } + } + + // loop over the yearly buckets and sort those in yearly descending order + const yearlyItems = Object.values(bucketsYear) + yearlyItems.sort(sortByDateOrYear) + + return yearlyItems +} diff --git a/src/utils/md-formatter.js b/src/utils/md-formatter.js index e5ef411..5312b65 100644 --- a/src/utils/md-formatter.js +++ b/src/utils/md-formatter.js @@ -6,8 +6,8 @@ export function formatToMarkdown(data) { export function getEventsMd(events) { let markdownEventsContent = '' - for (const year of Object.keys(events)) { - const eventsByYear = eventsListForYear(events[year]) + for (const yearlyItems of events) { + const eventsByYear = eventsListForYear(yearlyItems) markdownEventsContent += eventsByYear + '\n' } @@ -22,8 +22,10 @@ function getTableOfContents(events) { }) const markdownYearsItems = [] - for (const year of Object.keys(events)) { - markdownYearsItems.push(`[Events in ${year}](#${year})`) + for (const yearlyItems of events) { + markdownYearsItems.push( + `[Events in ${yearlyItems.year}](#${yearlyItems.year})` + ) } tableOfContents.push({