Skip to content

Commit

Permalink
feat(link): Add input rule to insert links using markdown syntax
Browse files Browse the repository at this point in the history
Fixes: #55

Code ideas taken from https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/assets/javascripts/content_editor/extensions/link.js

Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
mejo- committed Jun 12, 2024
1 parent 0d0b8ef commit 2ada5ff
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -66,6 +83,17 @@ const Link = TipTapLink.extend({
}, 0]
},

addInputRules() {
const linkInputRegex = /(?:^|\s)\[([\w|\s|-]+)\]\((?<href>.+?)\)$/gm
return [
markInputRule({
find: linkInputRegex,
type: this.type,
getAttributes: extractHrefFromMarkdownLink,
}),
]
},

addProseMirrorPlugins() {
const plugins = this.parent()
// remove upstream link click handle plugin
Expand Down

0 comments on commit 2ada5ff

Please sign in to comment.