|
| 1 | +import './styles.scss' |
| 2 | + |
| 3 | +import Image from '@tiptap/extension-image' |
| 4 | +import { TableKit } from '@tiptap/extension-table' |
| 5 | +import { Markdown } from '@tiptap/markdown' |
| 6 | +import { |
| 7 | + createAtomBlockMarkdownSpec, |
| 8 | + createBlockMarkdownSpec, |
| 9 | + createInlineMarkdownSpec, |
| 10 | + EditorContent, |
| 11 | + Mark, |
| 12 | + Node, |
| 13 | + useEditor, |
| 14 | +} from '@tiptap/react' |
| 15 | +import StarterKit from '@tiptap/starter-kit' |
| 16 | +import { useState } from 'react' |
| 17 | + |
| 18 | +const CustomNode = Node.create({ |
| 19 | + name: 'custom', |
| 20 | + group: 'block', |
| 21 | + content: 'block*', |
| 22 | + |
| 23 | + renderHTML() { |
| 24 | + return ['div', { 'data-type': 'custom' }, 0] |
| 25 | + }, |
| 26 | + |
| 27 | + parseHTML() { |
| 28 | + return [ |
| 29 | + { |
| 30 | + tag: 'div[data-type="custom"]', |
| 31 | + }, |
| 32 | + ] |
| 33 | + }, |
| 34 | + |
| 35 | + ...createBlockMarkdownSpec({ |
| 36 | + nodeName: 'custom', |
| 37 | + content: 'block', |
| 38 | + }), |
| 39 | +}) |
| 40 | + |
| 41 | +const CustomAtom = Node.create({ |
| 42 | + name: 'customAtom', |
| 43 | + group: 'block', |
| 44 | + atom: true, |
| 45 | + |
| 46 | + renderHTML() { |
| 47 | + return ['div', { 'data-type': 'atom' }] |
| 48 | + }, |
| 49 | + |
| 50 | + parseHTML() { |
| 51 | + return [ |
| 52 | + { |
| 53 | + tag: 'div[data-type="atom"]', |
| 54 | + }, |
| 55 | + ] |
| 56 | + }, |
| 57 | + |
| 58 | + addNodeView() { |
| 59 | + return () => { |
| 60 | + const el = document.createElement('div') |
| 61 | + el.setAttribute('data-type', 'atom') |
| 62 | + el.textContent = 'This is an atom node.' |
| 63 | + |
| 64 | + return { |
| 65 | + dom: el, |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | + |
| 70 | + ...createAtomBlockMarkdownSpec({ |
| 71 | + nodeName: 'atom', |
| 72 | + }), |
| 73 | +}) |
| 74 | + |
| 75 | +const CustomInline = Node.create({ |
| 76 | + name: 'customInline', |
| 77 | + group: 'inline', |
| 78 | + inline: true, |
| 79 | + content: 'inline*', |
| 80 | + |
| 81 | + renderHTML() { |
| 82 | + return ['span', { 'data-type': 'inline' }, 0] |
| 83 | + }, |
| 84 | + |
| 85 | + parseHTML() { |
| 86 | + return [ |
| 87 | + { |
| 88 | + tag: 'span[data-type="inline"]', |
| 89 | + }, |
| 90 | + ] |
| 91 | + }, |
| 92 | + |
| 93 | + ...createInlineMarkdownSpec({ |
| 94 | + nodeName: 'customInline', |
| 95 | + content: 'inline', |
| 96 | + }), |
| 97 | +}) |
| 98 | + |
| 99 | +const CustomMark = Mark.create({ |
| 100 | + name: 'customMark', |
| 101 | + |
| 102 | + renderHTML() { |
| 103 | + return ['span', { 'data-type': 'mark' }, 0] |
| 104 | + }, |
| 105 | + |
| 106 | + parseHTML() { |
| 107 | + return [ |
| 108 | + { |
| 109 | + tag: 'span[data-type="mark"]', |
| 110 | + }, |
| 111 | + ] |
| 112 | + }, |
| 113 | + |
| 114 | + renderMarkdown(node, helpers) { |
| 115 | + return `=>${helpers.renderChildren(node)}<=` |
| 116 | + }, |
| 117 | + |
| 118 | + parseMarkdown(token, helpers) { |
| 119 | + return { |
| 120 | + type: this.name, |
| 121 | + content: helpers.applyMark('customMark', helpers.parseInline(token.tokens || [])), |
| 122 | + } |
| 123 | + }, |
| 124 | + |
| 125 | + markdownTokenizer: { |
| 126 | + name: 'customMark', |
| 127 | + level: 'inline', |
| 128 | + start(src) { |
| 129 | + return src.indexOf('=>') |
| 130 | + }, |
| 131 | + tokenize(src, _tokens, lexer) { |
| 132 | + const rule = /^(=>)([\s\S]+?)(<=)/ |
| 133 | + const match = rule.exec(src) |
| 134 | + |
| 135 | + if (!match) { |
| 136 | + return undefined |
| 137 | + } |
| 138 | + |
| 139 | + const innerContent = match[2].trim() |
| 140 | + |
| 141 | + return { |
| 142 | + type: 'customMark', |
| 143 | + raw: match[0], |
| 144 | + text: innerContent, |
| 145 | + tokens: lexer.inlineTokens(innerContent), |
| 146 | + } |
| 147 | + }, |
| 148 | + }, |
| 149 | +}) |
| 150 | + |
| 151 | +export default () => { |
| 152 | + const [serializedContent, setSerializedContent] = useState('') |
| 153 | + const editor = useEditor({ |
| 154 | + extensions: [Markdown, StarterKit, Image, TableKit, CustomNode, CustomAtom, CustomInline, CustomMark], |
| 155 | + content: ` |
| 156 | + <p>In this demo, you can see how to define custom syntax for Markdown.</p> |
| 157 | + <div data-type="custom"> |
| 158 | + <p>This is a custom node.</p> |
| 159 | + </div> |
| 160 | + <div data-type="custom"> |
| 161 | + <p>We also support nested nodes.</p> |
| 162 | +
|
| 163 | + <div data-type="custom"> |
| 164 | + <p>This is a custom node.</p> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + <div data-type="atom"></div> |
| 168 | + <p> |
| 169 | + This is a <span data-type="mark">paragraph</span> with a <span data-type="inline">custom inline node</span>. |
| 170 | + </p> |
| 171 | + <p>Feel free to edit this document to see the live-changes.</p> |
| 172 | + `, |
| 173 | + onUpdate: ({ editor: currentEditor }) => { |
| 174 | + setSerializedContent(currentEditor.getMarkdown()) |
| 175 | + }, |
| 176 | + onCreate: ({ editor: currentEditor }) => { |
| 177 | + setSerializedContent(currentEditor.getMarkdown()) |
| 178 | + }, |
| 179 | + }) |
| 180 | + |
| 181 | + return ( |
| 182 | + <> |
| 183 | + <div className="grid"> |
| 184 | + <EditorContent className="editor-wrapper" editor={editor} /> |
| 185 | + <div className="preview"> |
| 186 | + <pre>{serializedContent}</pre> |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + </> |
| 190 | + ) |
| 191 | +} |
0 commit comments