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
62
63
64
65
66
67
68
69
|
import { CodeBlockLowlight } from '@tiptap/extension-code-block-lowlight';
import { textblockTypeInputRule } from '@tiptap/core';
import { VueNodeViewRenderer } from '@tiptap/vue-2';
import languageLoader from '../services/code_block_language_loader';
import CodeBlockWrapper from '../components/wrappers/code_block.vue';
const extractLanguage = (element) => element.dataset.canonicalLang ?? element.getAttribute('lang');
export const backtickInputRegex = /^```([a-z]+)?[\s\n]$/;
export const tildeInputRegex = /^~~~([a-z]+)?[\s\n]$/;
export default CodeBlockLowlight.extend({
isolating: true,
exitOnArrowDown: false,
addAttributes() {
return {
language: {
default: null,
parseHTML: (element) => extractLanguage(element),
},
class: {
// eslint-disable-next-line @gitlab/require-i18n-strings
default: 'code highlight',
},
};
},
addInputRules() {
const getAttributes = (match) => languageLoader?.loadLanguageFromInputRule(match) || {};
return [
textblockTypeInputRule({
find: backtickInputRegex,
type: this.type,
getAttributes,
}),
textblockTypeInputRule({
find: tildeInputRegex,
type: this.type,
getAttributes,
}),
];
},
parseHTML() {
return [
{
tag: 'div.markdown-code-block',
skip: true,
},
{
tag: 'pre.js-syntax-highlight',
preserveWhitespace: 'full',
},
];
},
renderHTML({ HTMLAttributes }) {
return [
'pre',
{
...HTMLAttributes,
class: `content-editor-code-block ${gon.user_color_scheme} ${HTMLAttributes.class}`,
},
['code', {}, 0],
];
},
addNodeView() {
return new VueNodeViewRenderer(CodeBlockWrapper);
},
});
|