-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathCodeBlock.js
More file actions
61 lines (50 loc) · 1.86 KB
/
CodeBlock.js
File metadata and controls
61 lines (50 loc) · 1.86 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
56
57
58
59
60
61
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import TiptapCodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
import { VueNodeViewRenderer } from '@tiptap/vue-2'
import codeBlockShortcuts from './CodeBlock/codeBlockShortcuts.js'
import CodeBlockView from './CodeBlock/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
// After https://github.com/ProseMirror/prosemirror-markdown/pull/90 the upstream markdown serialization
// is no longer comparible with our extraction stripping the last new line, so we keep a reverted copy
// to stay consistent with our unit tests
// Make sure the front matter fences are longer than any dash sequence within it
const backticks = node.textContent.match(/`{3,}/gm)
const fence = backticks ? (backticks.sort().slice(-1)[0] + '`') : '```'
const language = node.attrs.params !== 'plaintext' ? node.attrs.params : ''
state.write(fence + (language || '') + '\n')
state.text(node.textContent, false)
// Add a newline to the current content before adding closing marker
state.ensureNewLine()
state.write(fence)
state.closeBlock(node)
},
addNodeView() {
return VueNodeViewRenderer(CodeBlockView)
},
addKeyboardShortcuts() {
return codeBlockShortcuts
},
})
export default CodeBlock