create database

This commit is contained in:
Pacharapol Withayasakpunt committed 2020-02-27 16:41:36 +07:00
1 parent e2be0d1d05
commit 186b2661e8
13 files changed
+435 -220

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
import fs from 'fs'
import crypto from 'crypto'
import matter from 'gray-matter'
import glob from 'fast-glob'
import DataStore from 'nedb-promises'
import MakeHtml from '../src/utils/make-html'
// eslint-disable-next-line camelcase
const excerpt_separator = '<!-- excerpt_separator -->'
const db = new DataStore('assets/data.nedb')
;(async () => {
await db.ensureIndex({ fieldName: 'h', unique: true })
const files = await glob('**/*.md', {
cwd: '../../data'
})
await Promise.all(files.map(async (f) => {
let type = ''
if (f.startsWith('slides/')) {
type = 'slides'
}
const s = fs.readFileSync(`../../data/${f}`, 'utf8')
const h = crypto.createHash('sha256').update(s).digest('base64')
const { data, content, excerpt } = matter(s, {
excerpt_separator
})
try {
if (type === 'slides') {
const slides = content.split(/\n===\n/g).map((ss) => {
return ss.split(/\n--\n/g).map((s) => {
const make = new MakeHtml()
return make.parse(s)
}).reduce((prev, c, i) => ({ ...prev, [i.toString()]: c }), {} as any)
}).reduce((prev, c, i) => ({ ...prev, [i.toString()]: c }), {} as any)
const computedExcerpt = slides['0']['0']
await db.insert({
...data,
filename: f,
h,
excerpt: computedExcerpt,
slides
})
} else {
const make = new MakeHtml()
let computedExcerpt = ''
let computedContent = ''
if (excerpt) {
computedExcerpt = make.parse(excerpt)
computedContent = make.parse(content.substr(excerpt.length + excerpt_separator.length))
} else {
computedContent = make.parse(content)
computedExcerpt = computedContent.substr(0, 500).replace(/<[^>]*$/, '')
computedContent = computedContent.substr(computedExcerpt.length)
}
await db.insert({
...data,
filename: f,
h,
excerpt: computedExcerpt,
remaining: computedContent
})
}
} catch (e) {
console.error(e, f)
}
}))
})().catch(console.error)