diff --git a/src/marks/Link.js b/src/marks/Link.js index 545cf758a92..f5d713183a7 100644 --- a/src/marks/Link.js +++ b/src/marks/Link.js @@ -20,10 +20,27 @@ * */ +import { markInputRule } from '@tiptap/core' import TipTapLink from '@tiptap/extension-link' import { domHref, parseHref } from './../helpers/links.js' import { linkClicking } from '../plugins/links.js' +const extractHrefFromMatch = (match) => { + return { href: match.groups.href } +} + +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) +} + const Link = TipTapLink.extend({ addOptions() { @@ -66,6 +83,17 @@ const Link = TipTapLink.extend({ }, 0] }, + addInputRules() { + const linkInputRegex = /(?:^|\s)\[([\w|\s|-]+)\]\((?.+?)\)$/gm + return [ + markInputRule({ + find: linkInputRegex, + type: this.type, + getAttributes: extractHrefFromMarkdownLink, + }), + ] + }, + addProseMirrorPlugins() { const plugins = this.parent() // remove upstream link click handle plugin