update
This commit is contained in:
1 parent
31dd2b0049
commit
7039b10df2
32 files changed
+415
-192
No files matched your search
@@ -2,53 +2,40 @@ 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 { 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(srcMediaPath(), dstMediaPath())
|
||||
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: srcPostPath(),
|
||||
cwd: srcPath('_post'),
|
||||
})
|
||||
|
||||
// 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')
|
||||
const path = f.replace(/\.md$/, '')
|
||||
let markdown = fs.readFileSync(srcPath('_post', 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 [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(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 makeHtml = new MakeHTML(path)
|
||||
|
||||
const content = makeHtml.clean(markdown)
|
||||
const excerptHtml = makeHtml.parse(
|
||||
@@ -57,11 +44,8 @@ async function main() {
|
||||
const contentHtml = makeHtml.parse(markdown)
|
||||
|
||||
db.insert({
|
||||
slug,
|
||||
title,
|
||||
date,
|
||||
image,
|
||||
tag,
|
||||
...header,
|
||||
path,
|
||||
content,
|
||||
excerptHtml,
|
||||
contentHtml,
|
||||
|
||||
Reference in new issue
Block a user