diff --git a/src/lib/notion/NotionAPIWrapper.ts b/src/lib/notion/NotionAPIWrapper.ts index db7443137..d46ddbb67 100644 --- a/src/lib/notion/NotionAPIWrapper.ts +++ b/src/lib/notion/NotionAPIWrapper.ts @@ -11,6 +11,7 @@ import sanitizeTags from '../anki/sanitizeTags'; import ParserRules from '../parser/ParserRules'; import Settings from '../parser/Settings'; import isHeading from './helpers/isHeading'; +import { ParagraphBlockObject } from './types'; const ANON_LIMIT = 21 * 2; const PATREON_LIMIT = 100 * 2; @@ -69,6 +70,17 @@ class NotionAPIWrapper { }); } + async createBlock( + parent: string, + newBlock: ParagraphBlockObject + ): Promise { + return this.notion.blocks.children.append({ + block_id: parent, + /* @ts-ignore */ + children: [newBlock], + }); + } + async getDatabase(id: string): Promise { return this.notion.databases.retrieve({ database_id: id }); } diff --git a/src/lib/notion/types.ts b/src/lib/notion/types.ts new file mode 100644 index 000000000..899c347ea --- /dev/null +++ b/src/lib/notion/types.ts @@ -0,0 +1,16 @@ +export type ParagraphBlockObject = { + paragraph: { + color: string; + text: [ + { + type: string; + text: { + content: string; + }; + annotations: { + color: string; + }; + } + ]; + }; +}; diff --git a/src/routes/notion/createBlock.ts b/src/routes/notion/createBlock.ts new file mode 100644 index 000000000..376ade5ba --- /dev/null +++ b/src/routes/notion/createBlock.ts @@ -0,0 +1,15 @@ +import express from 'express'; +import NotionAPIWrapper from '../../lib/notion/NotionAPIWrapper'; + +export default async function deleteBlock( + api: NotionAPIWrapper, + req: express.Request, + res: express.Response +) { + const { id } = req.params; + if (!id) { + return res.status(400).send(); + } + const block = await api.createBlock(id, req.body.newBlock); + res.json(block); +} diff --git a/src/routes/notion/index.ts b/src/routes/notion/index.ts index 9fec5c606..c616f3701 100644 --- a/src/routes/notion/index.ts +++ b/src/routes/notion/index.ts @@ -15,6 +15,7 @@ import ensureResponse from './helpers/ensureResponse'; import { captureException } from '@sentry/node'; import renderBlock from './renderBlock'; import deleteBlock from './deleteBlock'; +import createBlock from './createBlock'; const router = express.Router(); @@ -126,6 +127,13 @@ router.get('/block/:id', RequireAuthentication, async (req, res) => }, res) ); +router.post('/block/:id', RequireAuthentication, async (req, res) => { + ensureResponse(async () => { + const api = await ConfigureNotionAPI(req, res); + return createBlock(api, req, res); + }, res); +}); + router.delete('/block/:id', RequireAuthentication, async (req, res) => ensureResponse(async () => { if (!res.locals.patreon) {