diff options
author | Ferdinand Thiessen <rpm@fthiessen.de> | 2022-07-21 09:55:14 +0300 |
---|---|---|
committer | Ferdinand Thiessen <rpm@fthiessen.de> | 2022-07-29 17:07:53 +0300 |
commit | 1ee4b069ca17fa08121287fe9e6f2f3beb37df1a (patch) | |
tree | 1b60fbdc9d67816547be7b28fc075c1fb53931a4 /src/nodes | |
parent | e577eec1493e47178a795341c85a305a4597efb8 (diff) |
Add support for editing (not creating) FrontMatter
This adds support for parsing files containing FrontMatter and editing
it as like a code block.
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
Diffstat (limited to 'src/nodes')
-rw-r--r-- | src/nodes/FrontMatter.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/nodes/FrontMatter.js b/src/nodes/FrontMatter.js new file mode 100644 index 000000000..d374228d1 --- /dev/null +++ b/src/nodes/FrontMatter.js @@ -0,0 +1,47 @@ +import TiptapCodeBlock from '@tiptap/extension-code-block' + +const FrontMatter = TiptapCodeBlock.extend({ + name: 'frontMatter', + addAttributes() { + return { + ...this.parent?.(), + class: { + default: 'frontmatter', + rendered: true, + }, + } + }, + parseHTML: () => { + return [ + { + tag: 'pre[id="frontmatter"]', + preserveWhitespace: 'full', + priority: 9001, + attrs: { + language: 'yaml', + }, + }, + ] + }, + toMarkdown: (state, node) => { + if (!state.out.match(/^\s*/)) throw Error('FrontMatter must be the first node of the document!') + state.write('') + state.out = '' + state.write('---\n') + state.text(node.textContent, false) + state.ensureNewLine() + state.write('---') + state.closeBlock(node) + }, + // FrontMatter are only valid at the begin of a document + draggable: false, + // Override rules from Codeblock + addCommands() { + return {} + }, + addInputRules: () => [], + addPasteRules: () => [], + addProseMirrorPlugins: () => [], +}) + +export default FrontMatter |