initiation librodrome
This commit is contained in:
23
server/api/admin/chapters/[slug].put.ts
Normal file
23
server/api/admin/chapters/[slug].put.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { writeFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const slug = getRouterParam(event, 'slug')
|
||||
|
||||
if (!slug || !/^[a-z0-9-]+$/.test(slug)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid slug' })
|
||||
}
|
||||
|
||||
const body = await readBody<{ frontmatter: string; body: string }>(event)
|
||||
|
||||
if (!body?.frontmatter && !body?.body) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Missing content' })
|
||||
}
|
||||
|
||||
const filePath = join(process.cwd(), 'content', 'book', `${slug}.md`)
|
||||
const content = `---\n${body.frontmatter.trim()}\n---\n${body.body}`
|
||||
|
||||
await writeFile(filePath, content, 'utf-8')
|
||||
|
||||
return { ok: true }
|
||||
})
|
||||
Reference in New Issue
Block a user