2022-08-27 11:52:29 +05:30
|
|
|
import { lowlight } from 'lowlight/lib/core';
|
2022-07-23 23:45:48 +05:30
|
|
|
import { textblockTypeInputRule } from '@tiptap/core';
|
2022-11-25 23:54:43 +05:30
|
|
|
import { base64DecodeUnicode } from '~/lib/utils/text_utility';
|
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,
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
lowlight,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
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) {
|
2022-11-25 23:54:43 +05:30
|
|
|
const source = base64DecodeUnicode(
|
|
|
|
element.dataset.diagramSrc.replace('data:text/plain;base64,', ''),
|
|
|
|
);
|
2022-06-21 17:19:12 +05:30
|
|
|
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
|
|
|
},
|
|
|
|
});
|