awesome-ipfs/scripts/make-data.js
Henrique Dias ab32206828
chore: update dependencies (#263)
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
2019-07-13 20:07:23 +00:00

51 lines
1.1 KiB
JavaScript

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
}))
})
data.forEach(makeIndex)
return data
}
function makeIndex (category) {
const data = category.content.map(({ index, title, description = '', tags = [], category = '' }) => ({
ref: index,
data: `${title} ${description} ${tags.join(' ')} ${category}`
}))
category.index = lunr(function () {
this.ref('ref')
this.field('data')
data.forEach(this.add.bind(this))
})
}
const process = () => {
const dir = join(__dirname, '../src/content')
fs.ensureDirSync(dir)
fs.emptyDirSync(dir)
const data = getData()
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>`)
}
}
process()