mirror of
https://github.com/suitenumerique/docs.git
synced 2026-04-25 17:15:01 +02:00
wip introduce convert logic
This commit is contained in:
3406
src/blocknote/package-lock.json
generated
3406
src/blocknote/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -17,8 +17,11 @@
|
||||
"check": "prettier --check ./src"
|
||||
},
|
||||
"dependencies": {
|
||||
"@blocknote/server-util": "0.17.1",
|
||||
"dotenv": "16.4.5",
|
||||
"express": "4.21.1"
|
||||
"express": "4.21.1",
|
||||
"prettier": "3.3.3",
|
||||
"yjs": "13.6.20"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "5.0.0",
|
||||
|
||||
@@ -1,23 +1,37 @@
|
||||
import express, { Express, Request, Response } from 'express'
|
||||
import { asyncWrapper, convertMarkdown } from './utils'
|
||||
import dotenv from 'dotenv'
|
||||
import bodyParser from 'body-parser'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const app: Express = express()
|
||||
const router = express.Router()
|
||||
const port = process.env.PORT ?? 8081
|
||||
|
||||
app.get('/', (req: Request, res: Response) => {
|
||||
res.send('Express + TypeScript Server')
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({ extended: true }))
|
||||
|
||||
// Logging middleware, logs the request method and path for each incoming request
|
||||
router.use(async function (req, res, next) {
|
||||
console.log(`/${req.method}`)
|
||||
next()
|
||||
})
|
||||
|
||||
app.get('/__heartbeat__', (req: Request, res: Response) => {
|
||||
// Liveness probe endpoint for Kubernetes health checks
|
||||
router.get('/__heartbeat__', (req: Request, res: Response) => {
|
||||
res.status(200).send({ status: 'OK' })
|
||||
})
|
||||
|
||||
app.get('/__lbheartbeat__', (req: Request, res: Response) => {
|
||||
// Load balancer heartbeat check, useful to detect app readiness
|
||||
router.get('/__lbheartbeat__', (req: Request, res: Response) => {
|
||||
res.status(200).send({ status: 'OK' })
|
||||
})
|
||||
|
||||
router.post('/', asyncWrapper(convertMarkdown))
|
||||
|
||||
app.use('/', router)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`[server]: Server is running at http://localhost:${port}`)
|
||||
console.log(`[server]: Server listening on port ${port}`)
|
||||
})
|
||||
|
||||
60
src/blocknote/src/utils.ts
Normal file
60
src/blocknote/src/utils.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
// Utility functions for handling markdown conversion and related operations
|
||||
|
||||
import { NextFunction, Request, Response } from 'express'
|
||||
import { ServerBlockNoteEditor } from '@blocknote/server-util'
|
||||
import Y from 'yjs'
|
||||
|
||||
const toBase64 = function (str: Uint8Array) {
|
||||
return Buffer.from(str).toString('base64')
|
||||
}
|
||||
|
||||
export const asyncWrapper = (
|
||||
asyncFn: (req: Request, res: Response) => Promise<Response>
|
||||
) => {
|
||||
return function (req: Request, res: Response, next: NextFunction) {
|
||||
asyncFn(req, res).catch(next)
|
||||
}
|
||||
}
|
||||
|
||||
const validateContent = (content: string | undefined): string => {
|
||||
if (!content) {
|
||||
throw new Error('Content is required')
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
const parseMarkdownToBlocks = async (
|
||||
blockNoteEditor: ServerBlockNoteEditor,
|
||||
content: string
|
||||
) => {
|
||||
try {
|
||||
const blocks = await blockNoteEditor.tryParseMarkdownToBlocks(content)
|
||||
if (!blocks || blocks.length === 0) {
|
||||
throw new Error('No valid blocks generated')
|
||||
}
|
||||
return blocks
|
||||
} catch (error) {
|
||||
throw new Error('Failed to parse markdown content')
|
||||
}
|
||||
}
|
||||
|
||||
const processContentBlocks = (server: ServerBlockNoteEditor, blocks: any[]) => {
|
||||
try {
|
||||
const yDocument = server.blocksToYDoc(blocks, 'document-store')
|
||||
return toBase64(Y.encodeStateAsUpdate(yDocument))
|
||||
} catch (error) {
|
||||
throw new Error('Failed to process content blocks')
|
||||
}
|
||||
}
|
||||
|
||||
export const convertMarkdown = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const content = validateContent(req.body.content)
|
||||
const editor = ServerBlockNoteEditor.create()
|
||||
const blocks = await parseMarkdownToBlocks(editor, content)
|
||||
const encodedContent = processContentBlocks(editor, blocks)
|
||||
return res.send({ content: encodedContent })
|
||||
} catch (error) {
|
||||
return res.status(500).json({ error: (error as Error).message })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user