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

68 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import { Node } from '@tiptap/core';
2021-11-11 11:23:49 +05:30
import { PARSE_HTML_PRIORITY_HIGHEST } from '../constants';
const getAnchor = (element) => {
if (element.nodeName === 'A') return element;
return element.querySelector('a');
};
2021-10-27 15:23:28 +05:30
export default Node.create({
name: 'reference',
inline: true,
group: 'inline',
atom: true,
addAttributes() {
return {
className: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => getAnchor(element).className,
2021-10-27 15:23:28 +05:30
},
referenceType: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => getAnchor(element).dataset.referenceType,
2021-10-27 15:23:28 +05:30
},
originalText: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => getAnchor(element).dataset.original,
2021-10-27 15:23:28 +05:30
},
href: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => getAnchor(element).getAttribute('href'),
2021-10-27 15:23:28 +05:30
},
text: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => getAnchor(element).textContent,
2021-10-27 15:23:28 +05:30
},
};
},
parseHTML() {
return [
{
tag: 'a.gfm:not([data-link=true])',
2021-11-11 11:23:49 +05:30
priority: PARSE_HTML_PRIORITY_HIGHEST,
},
{
tag: 'span.gl-label',
2021-10-27 15:23:28 +05:30
},
];
},
renderHTML({ node }) {
return [
'a',
{
class: node.attrs.className,
href: node.attrs.href,
'data-reference-type': node.attrs.referenceType,
'data-original': node.attrs.originalText,
},
node.attrs.text,
];
},
});