From a66c0860b2b83b62bb96d0f8915c8d8a22ab09b5 Mon Sep 17 00:00:00 2001 From: suyao Date: Wed, 29 Oct 2025 03:48:12 +0800 Subject: [PATCH] Add markdown parsing and serialization for YAML front matter - Add `markdownTokenName` property for custom parsing - Implement `parseMarkdown` to convert markdown tokens to Tiptap JSON - Implement `renderMarkdown` to serialize Tiptap nodes back to markdown format --- .../extensions/yaml-front-matter.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/renderer/src/components/RichEditor/extensions/yaml-front-matter.ts b/src/renderer/src/components/RichEditor/extensions/yaml-front-matter.ts index d5305bd09..addb06ebe 100644 --- a/src/renderer/src/components/RichEditor/extensions/yaml-front-matter.ts +++ b/src/renderer/src/components/RichEditor/extensions/yaml-front-matter.ts @@ -17,6 +17,9 @@ export const YamlFrontMatter = Node.create({ atom: true, draggable: false, + // Markdown token name for custom parsing + markdownTokenName: 'yamlFrontMatter', + addOptions() { return { HTMLAttributes: {} @@ -75,6 +78,30 @@ export const YamlFrontMatter = Node.create({ ] }, + // Parse markdown token to Tiptap JSON + parseMarkdown(token: any) { + // Extract YAML content from the token + // The content should be the raw YAML text between --- delimiters + const content = token.text || token.raw || '' + return { + type: this.name, + attrs: { + content: content.trim() + } + } + }, + + // Serialize Tiptap node to markdown + renderMarkdown(node: any) { + const content = node.attrs.content || '' + // If content doesn't end with ---, add it + const trimmedContent = content.trim() + if (trimmedContent && !trimmedContent.endsWith('---')) { + return trimmedContent + '\n---\n\n' + } + return trimmedContent ? trimmedContent + '\n\n' : '' + }, + addCommands() { return { insertYamlFrontMatter: