feat: Add Markdown to ProseMirror converter
- New converter supports headings, lists, blockquotes, code blocks - Documents now render with proper formatting in Outline - Auto-update collection documentStructure on document creation - Documents appear in sidebar automatically Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
import { Pool } from 'pg';
|
||||
import { BaseTool, ToolResponse, DocumentArgs, GetDocumentArgs, CreateDocumentArgs, UpdateDocumentArgs, SearchDocumentsArgs, MoveDocumentArgs } from '../types/tools.js';
|
||||
import { validatePagination, validateSortDirection, validateSortField, isValidUUID, sanitizeInput } from '../utils/security.js';
|
||||
import { markdownToProseMirror } from '../utils/markdown-to-prosemirror.js';
|
||||
|
||||
/**
|
||||
* 1. list_documents - Lista documentos publicados e drafts com filtros e paginação
|
||||
@@ -240,6 +241,9 @@ const createDocument: BaseTool<CreateDocumentArgs> = {
|
||||
await pgClient.query('BEGIN');
|
||||
|
||||
try {
|
||||
// Convert Markdown to ProseMirror JSON
|
||||
const proseMirrorContent = markdownToProseMirror(text);
|
||||
|
||||
const docQuery = `
|
||||
INSERT INTO documents (
|
||||
id, "urlId", title, text, "collectionId", "teamId", "parentDocumentId", "createdById",
|
||||
@@ -250,7 +254,7 @@ const createDocument: BaseTool<CreateDocumentArgs> = {
|
||||
gen_random_uuid(),
|
||||
substring(md5(random()::text) from 1 for 10),
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), NOW(), 1, false, false, false, ARRAY[$6]::uuid[],
|
||||
1, '{"type": "doc", "content": [{"type": "paragraph"}]}'::jsonb
|
||||
1, $10::jsonb
|
||||
)
|
||||
RETURNING id, "urlId", title, "collectionId", "publishedAt", "createdAt"
|
||||
`;
|
||||
@@ -264,7 +268,8 @@ const createDocument: BaseTool<CreateDocumentArgs> = {
|
||||
userId,
|
||||
userId,
|
||||
args.template || false,
|
||||
publishedAt
|
||||
publishedAt,
|
||||
JSON.stringify(proseMirrorContent)
|
||||
];
|
||||
|
||||
const docResult = await pgClient.query(docQuery, docParams);
|
||||
@@ -288,6 +293,24 @@ const createDocument: BaseTool<CreateDocumentArgs> = {
|
||||
text
|
||||
]);
|
||||
|
||||
// Update collection's documentStructure to include the new document
|
||||
const urlSlug = title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
||||
const updateStructureQuery = `
|
||||
UPDATE collections
|
||||
SET "documentStructure" = COALESCE("documentStructure", '[]'::jsonb) || $1::jsonb,
|
||||
"updatedAt" = NOW()
|
||||
WHERE id = $2
|
||||
`;
|
||||
await pgClient.query(updateStructureQuery, [
|
||||
JSON.stringify([{
|
||||
id: newDoc.id,
|
||||
url: `/doc/${urlSlug}-${newDoc.urlId}`,
|
||||
title: title,
|
||||
children: []
|
||||
}]),
|
||||
args.collection_id
|
||||
]);
|
||||
|
||||
await pgClient.query('COMMIT');
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user