rename files
This commit is contained in:
1 parent
1fdaf73f14
commit
31dd2b0049
72 files changed
+10320
-31
No files matched your search
@@ -0,0 +1,74 @@
|
||||
import fg from 'fast-glob'
|
||||
import fs from 'fs-extra'
|
||||
import yaml from 'js-yaml'
|
||||
import rimraf from 'rimraf'
|
||||
import * as z from 'zod'
|
||||
|
||||
import { Database } from '../assets/schema'
|
||||
import { builtPath, dstMediaPath, srcMediaPath, srcPostPath } from './dir'
|
||||
import { MakeHTML } from './make-html'
|
||||
|
||||
async function main() {
|
||||
rimraf.sync(dstMediaPath())
|
||||
rimraf.sync(builtPath())
|
||||
|
||||
await fs.copy(srcMediaPath(), dstMediaPath())
|
||||
await fs.ensureDir(builtPath())
|
||||
|
||||
const db = new Database(builtPath('data.db'))
|
||||
db.init()
|
||||
|
||||
const files = await fg('**/*.md', {
|
||||
cwd: srcPostPath(),
|
||||
})
|
||||
|
||||
// eslint-disable-next-line array-callback-return
|
||||
files.map((f) => {
|
||||
const slug = f.replace(/^.+\//, '').replace(/\.md$/, '')
|
||||
let header: Record<string, unknown> = {}
|
||||
let markdown = fs.readFileSync(srcPostPath(f), 'utf8')
|
||||
|
||||
if (markdown.startsWith('---\n')) {
|
||||
const [h, c = ''] = markdown.substr(4).split(/---\n(.*)$/s)
|
||||
header = yaml.safeLoad(h, {
|
||||
schema: yaml.JSON_SCHEMA,
|
||||
}) as Record<string, unknown>
|
||||
markdown = c
|
||||
}
|
||||
|
||||
const makeHtml = new MakeHTML(slug)
|
||||
|
||||
const { title, date, image, tag } = (() => {
|
||||
const { title, date, image, tag } = header
|
||||
return z
|
||||
.object({
|
||||
title: z.string(),
|
||||
date: z.date().optional(),
|
||||
image: z.string().optional(),
|
||||
tag: z.array(z.string()).optional(),
|
||||
})
|
||||
.parse({ title, date, image, tag })
|
||||
})()
|
||||
|
||||
const content = makeHtml.clean(markdown)
|
||||
const excerptHtml = makeHtml.parse(
|
||||
markdown.split(/<!-- excerpt(?:_separator)? -->/)[0]
|
||||
)
|
||||
const contentHtml = makeHtml.parse(markdown)
|
||||
|
||||
db.insert({
|
||||
slug,
|
||||
title,
|
||||
date,
|
||||
image,
|
||||
tag,
|
||||
content,
|
||||
excerptHtml,
|
||||
contentHtml,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main()
|
||||
}
|
||||
Reference in new issue
Block a user