Skip to content

Commit

Permalink
feat: sort by date (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal authored Nov 18, 2021
1 parent a6036c1 commit 85684a9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 46 deletions.
32 changes: 16 additions & 16 deletions __tests__/__snapshots__/main.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
"
Expand All @@ -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 |
<p align=\\"center\\"> rendered in postcontent </p>
Expand Down
8 changes: 4 additions & 4 deletions __tests__/md-formatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -31,7 +31,7 @@ test('process events json data to provide a complete markdown document', () => {
}
]
},
2021: {
{
year: 2021,
items: [
{
Expand All @@ -58,7 +58,7 @@ test('process events json data to provide a complete markdown document', () => {
}
]
}
}
]

const markdown = getEventsMd(events)
expect(markdown).toMatchSnapshot()
Expand Down
32 changes: 10 additions & 22 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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
}
}
38 changes: 38 additions & 0 deletions src/utils/content-manager.js
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 6 additions & 4 deletions src/utils/md-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand All @@ -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({
Expand Down

0 comments on commit 85684a9

Please sign in to comment.