/app/assets/javascripts/content_editor/extensions/code_block_highlight.js
https://gitlab.com/523/gitlab-ce · JavaScript · 65 lines · 60 code · 4 blank · 1 comment · 2 complexity · 365cc33faf78297bb4895eadc9d42c30 MD5 · raw file
- 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.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 [
- ...(this.parent?.() || []),
- {
- tag: 'div.markdown-code-block',
- skip: true,
- },
- ];
- },
- renderHTML({ HTMLAttributes }) {
- return [
- 'pre',
- {
- ...HTMLAttributes,
- class: `content-editor-code-block ${gon.user_color_scheme} ${HTMLAttributes.class}`,
- },
- ['code', {}, 0],
- ];
- },
- addNodeView() {
- return new VueNodeViewRenderer(CodeBlockWrapper);
- },
- });