awesome-ipfs/scripts/data.js

35 lines
936 B
JavaScript
Raw Normal View History

2018-04-23 10:22:21 -04:00
const fs = require('fs')
const { join } = require('path')
2018-04-23 10:22:21 -04:00
const yaml = require('node-yaml')
const { sortAbc, sortInv, slugify } = require('./utils')
2018-04-23 10:22:21 -04:00
const dir = join(__dirname, '../data')
2018-04-23 10:22:21 -04:00
const trimIfExists = (str) => str ? str.trim() : undefined
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 => {
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),
category: file.slug,
color: file.color,
index: i
2018-04-23 10:22:21 -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))