From fcb5932d33047cb02f209c2938f599e961b4488a Mon Sep 17 00:00:00 2001 From: Sepehr Ramezani Date: Fri, 1 May 2026 16:32:49 +0200 Subject: [PATCH] fix: agent notes default to markdown, add convert-to-rich-text option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Agent-created notes are now type 'markdown' by default (raw markdown content) - Switching note type from markdown → richtext auto-converts content to HTML via /api/ai/convert-markdown endpoint using the existing markdownToHtml utility - Users can freely switch between markdown and richtext in the note type selector Co-Authored-By: Claude Opus 4.5 --- .../app/api/ai/convert-markdown/route.ts | 22 +++++++++++++++ .../components/note-inline-editor.tsx | 28 ++++++++++++++++++- .../lib/ai/services/agent-executor.service.ts | 7 ++--- memento-note/lib/ai/tools/note-crud.tool.ts | 7 ++--- 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 memento-note/app/api/ai/convert-markdown/route.ts diff --git a/memento-note/app/api/ai/convert-markdown/route.ts b/memento-note/app/api/ai/convert-markdown/route.ts new file mode 100644 index 0000000..0e8e5b6 --- /dev/null +++ b/memento-note/app/api/ai/convert-markdown/route.ts @@ -0,0 +1,22 @@ +import { NextRequest, NextResponse } from 'next/server' +import { auth } from '@/auth' +import { markdownToHtml } from '@/lib/markdown-to-html' + +export async function POST(request: NextRequest) { + try { + const session = await auth() + if (!session?.user?.id) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) + } + + const { content } = await request.json() + if (!content || typeof content !== 'string') { + return NextResponse.json({ error: 'Content is required' }, { status: 400 }) + } + + const html = markdownToHtml(content) + return NextResponse.json({ html }) + } catch (error: any) { + return NextResponse.json({ error: error.message || 'Conversion failed' }, { status: 500 }) + } +} diff --git a/memento-note/components/note-inline-editor.tsx b/memento-note/components/note-inline-editor.tsx index 79f051a..b09c4e8 100644 --- a/memento-note/components/note-inline-editor.tsx +++ b/memento-note/components/note-inline-editor.tsx @@ -495,10 +495,36 @@ export function NoteInlineEditor({ { + onChange={async (newType) => { + const oldType = noteType setNoteType(newType) if (newType === 'markdown') setShowMarkdownPreview(true) else setShowMarkdownPreview(false) + + // When switching from markdown to richtext, convert content to HTML + if (oldType === 'markdown' && newType === 'richtext') { + try { + const res = await fetch('/api/ai/convert-markdown', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ content }), + }) + if (res.ok) { + const { html } = await res.json() + setContent(html) + saveInline(note.id, { + type: 'richtext', + isMarkdown: false, + content: html, + }).catch(() => {}) + onChange?.(note.id, { type: 'richtext', isMarkdown: false, content: html }) + return + } + } catch { + // Fall through to save without conversion + } + } + // Persist both type and isMarkdown immediately saveInline(note.id, { type: newType, diff --git a/memento-note/lib/ai/services/agent-executor.service.ts b/memento-note/lib/ai/services/agent-executor.service.ts index 02122ef..0ffe7eb 100644 --- a/memento-note/lib/ai/services/agent-executor.service.ts +++ b/memento-note/lib/ai/services/agent-executor.service.ts @@ -43,13 +43,12 @@ async function createAgentNote(data: { notebookId: string | null autoGenerated?: boolean }) { - const htmlContent = markdownToHtml(data.content) return prisma.note.create({ data: { title: data.title, - content: htmlContent, - type: 'richtext', - isMarkdown: false, + content: data.content, + type: 'markdown', + isMarkdown: true, autoGenerated: data.autoGenerated ?? true, userId: data.userId, notebookId: data.notebookId, diff --git a/memento-note/lib/ai/tools/note-crud.tool.ts b/memento-note/lib/ai/tools/note-crud.tool.ts index 9bfd450..3bffc71 100644 --- a/memento-note/lib/ai/tools/note-crud.tool.ts +++ b/memento-note/lib/ai/tools/note-crud.tool.ts @@ -51,13 +51,12 @@ toolRegistry.register({ }), execute: async ({ title, content, notebookId, images }) => { try { - const htmlContent = markdownToHtml(content) const note = await prisma.note.create({ data: { title, - content: htmlContent, - type: 'richtext', - isMarkdown: false, + content, + type: 'markdown', + isMarkdown: true, autoGenerated: true, userId: ctx.userId, notebookId: notebookId || null,