59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import fg from 'fast-glob'
|
|
import fs from 'fs-extra'
|
|
import yaml from 'js-yaml'
|
|
import rimraf from 'rimraf'
|
|
|
|
import { Database, IMatter } from '../server/db'
|
|
import { THEME_FILENAME } from '../server/theme'
|
|
import { builtPath, dstMediaPath, srcPath } from './dir'
|
|
import { MakeHTML } from './make-html'
|
|
|
|
async function main() {
|
|
rimraf.sync(dstMediaPath())
|
|
rimraf.sync(builtPath())
|
|
|
|
await fs.copy(srcPath('_media'), dstMediaPath())
|
|
await fs.ensureDir(builtPath())
|
|
|
|
await fs.copyFile(srcPath(THEME_FILENAME), builtPath(THEME_FILENAME))
|
|
|
|
const db = new Database(builtPath('data.db'))
|
|
db.init()
|
|
|
|
const files = await fg('**/*.md', {
|
|
cwd: srcPath('_post'),
|
|
})
|
|
|
|
// eslint-disable-next-line array-callback-return
|
|
files.map((f) => {
|
|
const path = f.replace(/\.md$/, '')
|
|
let markdown = fs.readFileSync(srcPath('_post', f), 'utf8')
|
|
|
|
const [h, c = ''] = markdown.substr(4).split(/---\n(.*)$/s)
|
|
const header = yaml.safeLoad(h, {
|
|
schema: yaml.JSON_SCHEMA,
|
|
}) as IMatter
|
|
markdown = c
|
|
|
|
const makeHtml = new MakeHTML(path)
|
|
|
|
const content = makeHtml.clean(markdown)
|
|
const excerptHtml = makeHtml.parse(
|
|
markdown.split(/<!-- excerpt(?:_separator)? -->/)[0]
|
|
)
|
|
const contentHtml = makeHtml.parse(markdown)
|
|
|
|
db.insert({
|
|
...header,
|
|
path,
|
|
content,
|
|
excerptHtml,
|
|
contentHtml,
|
|
})
|
|
})
|
|
}
|
|
|
|
if (require.main === module) {
|
|
main()
|
|
}
|