2018-04-23 10:22:21 -04:00
|
|
|
const lunr = require('lunr')
|
|
|
|
const fs = require('fs-extra')
|
2019-04-30 12:35:30 -04:00
|
|
|
const { join } = require('path')
|
|
|
|
|
|
|
|
function getData () {
|
2019-07-13 16:07:23 -04:00
|
|
|
const data = require('./data')
|
2019-04-30 12:35:30 -04:00
|
|
|
|
|
|
|
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 10:22:21 -04:00
|
|
|
})
|
|
|
|
|
2019-04-30 12:35:30 -04:00
|
|
|
data.forEach(makeIndex)
|
|
|
|
return data
|
2018-04-23 10:22:21 -04:00
|
|
|
}
|
|
|
|
|
2019-04-30 12:35:30 -04:00
|
|
|
function makeIndex (category) {
|
|
|
|
const data = category.content.map(({ index, title, description = '', tags = [], category = '' }) => ({
|
2018-04-23 10:22:21 -04:00
|
|
|
ref: index,
|
2019-04-30 12:35:30 -04:00
|
|
|
data: `${title} ${description} ${tags.join(' ')} ${category}`
|
2018-04-23 10:22:21 -04:00
|
|
|
}))
|
|
|
|
|
2019-04-30 12:35:30 -04:00
|
|
|
category.index = lunr(function () {
|
|
|
|
this.ref('ref')
|
|
|
|
this.field('data')
|
|
|
|
data.forEach(this.add.bind(this))
|
|
|
|
})
|
2018-04-23 10:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const process = () => {
|
2019-04-30 12:35:30 -04:00
|
|
|
const dir = join(__dirname, '../src/content')
|
|
|
|
fs.ensureDirSync(dir)
|
|
|
|
fs.emptyDirSync(dir)
|
2018-04-23 10:22:21 -04:00
|
|
|
|
2019-04-30 12:35:30 -04:00
|
|
|
const data = getData()
|
2018-04-23 10:22:21 -04:00
|
|
|
|
2019-04-30 12:35:30 -04: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 10:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
process()
|