2018-04-23 10:22:21 -04:00
|
|
|
const fs = require('fs')
|
2019-04-30 12:35:30 -04:00
|
|
|
const { join } = require('path')
|
2018-04-23 10:22:21 -04:00
|
|
|
const yaml = require('node-yaml')
|
2019-04-30 12:35:30 -04:00
|
|
|
const { sortAbc, sortInv, slugify } = require('./utils')
|
2018-04-23 10:22:21 -04:00
|
|
|
|
2019-04-30 12:35:30 -04:00
|
|
|
const dir = join(__dirname, '../data')
|
2018-04-23 10:22:21 -04:00
|
|
|
const trimIfExists = (str) => str ? str.trim() : undefined
|
|
|
|
|
2019-04-30 12:35:30 -04:00
|
|
|
module.exports = fs.readdirSync(dir)
|
|
|
|
.map(file => join(dir, file))
|
2018-04-23 10:22:21 -04:00
|
|
|
.map(file => yaml.readSync(file))
|
|
|
|
.map(file => {
|
2019-04-30 12:35:30 -04:00
|
|
|
file.slug = slugify(file.title)
|
|
|
|
file.type = 'category'
|
|
|
|
|
|
|
|
file.content = file.content.map(({ title, description, ...meta }, i) => ({
|
|
|
|
...meta,
|
2018-04-23 10:22:21 -04:00
|
|
|
title: trimIfExists(title),
|
|
|
|
description: trimIfExists(description),
|
2019-04-30 12:35:30 -04:00
|
|
|
category: file.slug,
|
|
|
|
color: file.color,
|
|
|
|
index: i
|
2018-04-23 10:22:21 -04:00
|
|
|
}))
|
|
|
|
|
2019-04-30 12:35:30 -04:00
|
|
|
let sort = (a, b) => sortAbc(a.title, b.title)
|
|
|
|
|
|
|
|
if (file.slug === 'articles') {
|
|
|
|
sort = (a, b) => sortInv(a.date, b.date)
|
|
|
|
}
|
|
|
|
|
|
|
|
file.content = file.content.sort(sort)
|
2018-04-23 10:22:21 -04:00
|
|
|
return file
|
|
|
|
})
|
|
|
|
.sort((a, b) => sortAbc(a.title, b.title))
|