debian-mirror-gitlab/app/assets/javascripts/content_editor/extensions/diagram.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-07-23 23:45:48 +05:30
import { textblockTypeInputRule } from '@tiptap/core';
2022-06-21 17:19:12 +05:30
import { PARSE_HTML_PRIORITY_HIGHEST } from '../constants';
2022-07-23 23:45:48 +05:30
import languageLoader from '../services/code_block_language_loader';
2022-06-21 17:19:12 +05:30
import CodeBlockHighlight from './code_block_highlight';
2022-07-23 23:45:48 +05:30
const backtickInputRegex = /^```(mermaid|plantuml)[\s\n]$/;
2022-06-21 17:19:12 +05:30
export default CodeBlockHighlight.extend({
name: 'diagram',
isolating: true,
addAttributes() {
return {
language: {
default: null,
parseHTML: (element) => {
return element.dataset.diagram;
},
},
2022-07-16 23:28:13 +05:30
isDiagram: {
default: true,
},
2022-07-23 23:45:48 +05:30
showPreview: {
default: true,
},
2022-06-21 17:19:12 +05:30
};
},
parseHTML() {
return [
2022-07-23 23:45:48 +05:30
{
priority: PARSE_HTML_PRIORITY_HIGHEST,
tag: 'pre[lang="mermaid"]',
getAttrs: () => ({ language: 'mermaid' }),
},
2022-06-21 17:19:12 +05:30
{
priority: PARSE_HTML_PRIORITY_HIGHEST,
tag: '[data-diagram]',
getContent(element, schema) {
const source = atob(element.dataset.diagramSrc.replace('data:text/plain;base64,', ''));
const node = schema.node('paragraph', {}, [schema.text(source)]);
return node.content;
},
},
];
},
renderHTML({ HTMLAttributes: { language, ...HTMLAttributes } }) {
return [
'div',
[
'pre',
{
language,
class: `content-editor-code-block code highlight`,
...HTMLAttributes,
},
['code', {}, 0],
],
];
},
addCommands() {
return {};
},
addInputRules() {
2022-07-23 23:45:48 +05:30
const getAttributes = (match) => languageLoader?.loadLanguageFromInputRule(match) || {};
return [
textblockTypeInputRule({
find: backtickInputRegex,
type: this.type,
getAttributes,
}),
];
2022-06-21 17:19:12 +05:30
},
});