Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/assets/javascripts/content_editor/extensions/link.js')
-rw-r--r--app/assets/javascripts/content_editor/extensions/link.js33
1 files changed, 32 insertions, 1 deletions
diff --git a/app/assets/javascripts/content_editor/extensions/link.js b/app/assets/javascripts/content_editor/extensions/link.js
index 9a2fa7a5c98..6f5f81cbf93 100644
--- a/app/assets/javascripts/content_editor/extensions/link.js
+++ b/app/assets/javascripts/content_editor/extensions/link.js
@@ -1,5 +1,36 @@
+import { markInputRule } from '@tiptap/core';
import { Link } from '@tiptap/extension-link';
import { defaultMarkdownSerializer } from 'prosemirror-markdown/src/to_markdown';
-export const tiptapExtension = Link;
+export const markdownLinkSyntaxInputRuleRegExp = /(?:^|\s)\[([\w|\s|-]+)\]\((?<href>.+?)\)$/gm;
+
+export const urlSyntaxRegExp = /(?:^|\s)(?<href>(?:https?:\/\/|www\.)[\S]+)(?:\s|\n)$/gim;
+
+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);
+};
+
+export const tiptapExtension = Link.extend({
+ addInputRules() {
+ return [
+ markInputRule(markdownLinkSyntaxInputRuleRegExp, this.type, extractHrefFromMarkdownLink),
+ markInputRule(urlSyntaxRegExp, this.type, extractHrefFromMatch),
+ ];
+ },
+}).configure({
+ openOnClick: false,
+});
+
export const serializer = defaultMarkdownSerializer.marks.link;