-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathCodeBlock.js
More file actions
55 lines (46 loc) · 1.39 KB
/
CodeBlock.js
File metadata and controls
55 lines (46 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import TiptapCodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
import { defaultMarkdownSerializer } from '@tiptap/pm/markdown'
import { VueNodeViewRenderer } from '@tiptap/vue-2'
import CodeBlockView from './CodeBlockView.vue'
const CodeBlock = TiptapCodeBlockLowlight.extend({
parseHTML() {
return [
{
tag: 'pre',
preserveWhitespace: 'full',
// Remove trailing newline from code blocks (#2344)
getContent: (node, schema) => {
const textContent = node.textContent.replace(/\n$/, '')
const inner = textContent
? [schema.text(textContent)]
: []
return schema.nodes.codeBlock.create(null, inner)
},
},
]
},
toMarkdown(state, node, parent, index) {
// @tiptap/pm/markdown uses `params` instead of `language` attribute
node.attrs.params = node.attrs.language
return defaultMarkdownSerializer.nodes.code_block(state, node, parent, index)
},
addNodeView() {
return VueNodeViewRenderer(CodeBlockView)
},
addKeyboardShortcuts() {
return {
'Mod-a': () => {
if (!this.editor.isActive('codeBlock')) {
return
}
const nodeSize = this.editor.state.selection.$from.node().nodeSize
this.editor.commands.selectParentNode()
const from = this.editor.state.selection.$from.pos
const to = from + nodeSize
this.editor.commands.setTextSelection({ from, to })
return true
},
}
},
})
export default CodeBlock