From b16b87b055704704ff6ec910d8be3d8466b34274 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 23 Jun 2021 08:06:00 +0800 Subject: [PATCH 01/10] feat(admin): merge frontmatter and content --- src/admin/api/functions/content.ts | 27 +++------- src/admin/app/components/Editor.vue | 84 +++++++++-------------------- src/admin/type.ts | 8 +++ 3 files changed, 42 insertions(+), 77 deletions(-) create mode 100644 src/admin/type.ts diff --git a/src/admin/api/functions/content.ts b/src/admin/api/functions/content.ts index fbf08a54e..ce6b52bfc 100644 --- a/src/admin/api/functions/content.ts +++ b/src/admin/api/functions/content.ts @@ -1,15 +1,10 @@ import { promises as fs } from 'fs' import { join, extname } from 'path' -import matter from 'gray-matter' import { createError, Middleware, useBody } from 'h3' import dirTree from 'directory-tree' +import { FileData, File } from '../../type' import { normalizeFiles, r } from '../utils' -interface Body { - data: any - content: string -} - export default async function contentHandler(req) { const url = req.url @@ -23,13 +18,11 @@ export default async function contentHandler(req) { try { const path = join(r('content'), url) const file = await fs.readFile(path, 'utf-8') - const { content, data } = matter(file) - return { + return { path: path.replace(r('content'), ''), extension: extname(path), - data, - content + raw: file } } catch (err) { return createError({ @@ -41,12 +34,11 @@ export default async function contentHandler(req) { // Update changes if (req.method === 'PUT') { - const { data, content } = await useBody(req) - - if (!data || !content) { + const { raw } = await useBody(req) + if (raw == null) { return createError({ statusCode: 400, - statusMessage: 'data and content keys are required' + statusMessage: '"raw" key is required' }) } @@ -54,11 +46,8 @@ export default async function contentHandler(req) { try { // @ts-ignore - await fs.stat(path, 'utf-8') - - const file = matter.stringify(content, data) - - await fs.writeFile(path, file) + // await fs.stat(path, 'utf-8') + await fs.writeFile(path, raw) return { ok: true } } catch (err) { diff --git a/src/admin/app/components/Editor.vue b/src/admin/app/components/Editor.vue index 785077c23..64fdda970 100644 --- a/src/admin/app/components/Editor.vue +++ b/src/admin/app/components/Editor.vue @@ -1,70 +1,38 @@