From 7da647d99f1dcefabc653347a8e2abfae3bb972e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 7 May 2021 09:54:40 +0200 Subject: [PATCH] fix: fix a bug that messed up pasted link attributes, fix #1284 --- packages/core/src/pasteRules/markPasteRule.ts | 10 ++++++++-- packages/extension-link/src/link.ts | 11 ++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/core/src/pasteRules/markPasteRule.ts b/packages/core/src/pasteRules/markPasteRule.ts index 361d464d..7ece60cf 100644 --- a/packages/core/src/pasteRules/markPasteRule.ts +++ b/packages/core/src/pasteRules/markPasteRule.ts @@ -1,7 +1,11 @@ import { Plugin, PluginKey } from 'prosemirror-state' import { Slice, Fragment, MarkType } from 'prosemirror-model' -export default function (regexp: RegExp, type: MarkType, getAttrs?: (match: any) => any): Plugin { +export default function ( + regexp: RegExp, + type: MarkType, + getAttributes?: Record | ((match: RegExpExecArray) => Record), +): Plugin { const handler = (fragment: Fragment, parent?: any) => { const nodes: any[] = [] @@ -22,7 +26,9 @@ export default function (regexp: RegExp, type: MarkType, getAttrs?: (match: any) const matchEnd = matchStart + match[outerMatch].length const textStart = matchStart + match[outerMatch].lastIndexOf(match[innerMatch]) const textEnd = textStart + match[innerMatch].length - const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs + const attrs = getAttributes instanceof Function + ? getAttributes(match) + : getAttributes // adding text before markdown to nodes if (matchStart > 0) { diff --git a/packages/extension-link/src/link.ts b/packages/extension-link/src/link.ts index 515f8d55..7db16184 100644 --- a/packages/extension-link/src/link.ts +++ b/packages/extension-link/src/link.ts @@ -40,8 +40,14 @@ declare module '@tiptap/core' { } } +/** + * A regex that matches any string that contains a link + */ export const pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)/gi -export const pasteRegexWithBrackets = /(?:\()https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/()]*)(?:\))/gi + +/** + * A regex that matches an url + */ export const pasteRegexExact = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)$/gi export const Link = Mark.create({ @@ -97,8 +103,7 @@ export const Link = Mark.create({ addPasteRules() { return [ - markPasteRule(pasteRegex, this.type, (url: string) => ({ href: url })), - markPasteRule(pasteRegexWithBrackets, this.type, (url: string) => ({ href: url })), + markPasteRule(pasteRegex, this.type, match => ({ href: match[0] })), ] },