awesome-ipfs/scripts/make-data.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-04-23 14:22:21 +00:00
const lunr = require('lunr')
const fs = require('fs-extra')
const { join } = require('path')
function getData () {
const data = require('./data')
data.push({
title: 'Awesome IPFS',
slug: '_index',
content: data
.reduce((arr, cat) => arr.concat(cat.content), [])
.map((el, i) => ({
...el,
index: i
}))
2018-04-23 14:22:21 +00:00
})
data.forEach(makeIndex)
return data
2018-04-23 14:22:21 +00:00
}
function makeIndex (category) {
const data = category.content.map(({ index, title, description = '', tags = [], category = '' }) => ({
2018-04-23 14:22:21 +00:00
ref: index,
data: `${title} ${description} ${tags.join(' ')} ${category}`
2018-04-23 14:22:21 +00:00
}))
category.index = lunr(function () {
this.ref('ref')
this.field('data')
data.forEach(this.add.bind(this))
})
2018-04-23 14:22:21 +00:00
}
const process = () => {
const dir = join(__dirname, '../src/content')
fs.ensureDirSync(dir)
fs.emptyDirSync(dir)
2018-04-23 14:22:21 +00:00
const data = getData()
2018-04-23 14:22:21 +00:00
for (const { index, slug, ...meta } of data) {
const filename = join(dir, slug + '.md')
fs.writeFileSync(filename, `${JSON.stringify(meta)}
<script>var idx = JSON.parse(\`${JSON.stringify(index).replace(`'`, `\\'`)}\`);</script>`)
}
2018-04-23 14:22:21 +00:00
}
process()