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

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

68 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import { markInputRule } from '@tiptap/core';
2021-06-08 01:23:25 +05:30
import { Link } from '@tiptap/extension-link';
2021-09-04 01:27:46 +05:30
const extractHrefFromMatch = (match) => {
return { href: match.groups.href };
};
export const extractHrefFromMarkdownLink = (match) => {
/**
* Removes the last capture group from the match to satisfy
* tiptap markInputRule expectation of having the content as
* the last capture group in the match.
*
* https://github.com/ueberdosis/tiptap/blob/%40tiptap/core%402.0.0-beta.75/packages/core/src/inputRules/markInputRule.ts#L11
*/
match.pop();
return extractHrefFromMatch(match);
};
2021-10-27 15:23:28 +05:30
export default Link.extend({
2022-01-26 12:08:38 +05:30
addOptions() {
return {
...this.parent?.(),
openOnClick: false,
};
2021-10-27 15:23:28 +05:30
},
2022-01-26 12:08:38 +05:30
2021-09-04 01:27:46 +05:30
addInputRules() {
2021-12-11 22:18:48 +05:30
const markdownLinkSyntaxInputRuleRegExp = /(?:^|\s)\[([\w|\s|-]+)\]\((?<href>.+?)\)$/gm;
const urlSyntaxRegExp = /(?:^|\s)(?<href>(?:https?:\/\/|www\.)[\S]+)(?:\s|\n)$/gim;
2021-09-04 01:27:46 +05:30
return [
2021-12-11 22:18:48 +05:30
markInputRule({
find: markdownLinkSyntaxInputRuleRegExp,
type: this.type,
getAttributes: extractHrefFromMarkdownLink,
}),
markInputRule({
find: urlSyntaxRegExp,
type: this.type,
getAttributes: extractHrefFromMatch,
}),
2021-09-04 01:27:46 +05:30
];
},
2021-09-30 23:02:18 +05:30
addAttributes() {
return {
...this.parent?.(),
href: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => element.getAttribute('href'),
},
title: {
title: null,
parseHTML: (element) => element.getAttribute('title'),
2021-09-30 23:02:18 +05:30
},
canonicalSrc: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => element.dataset.canonicalSrc,
2022-08-27 11:52:29 +05:30
renderHTML: () => '',
},
isReference: {
default: false,
renderHTML: () => '',
2021-09-30 23:02:18 +05:30
},
};
},
2021-09-04 01:27:46 +05:30
});