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
This commit is contained in:
suyao 2025-10-29 03:48:12 +08:00
parent a418b61230
commit a66c0860b2
No known key found for this signature in database

View File

@ -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: