From 40b046765035605617bb48a17dfaaeedb07c47e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Thu, 24 Sep 2020 23:42:04 +0200 Subject: [PATCH] fix link paste rule --- packages/core/src/pasteRules/markPasteRule.ts | 13 +++++++------ packages/extension-link/index.ts | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/core/src/pasteRules/markPasteRule.ts b/packages/core/src/pasteRules/markPasteRule.ts index 25cf7e44..c10340b0 100644 --- a/packages/core/src/pasteRules/markPasteRule.ts +++ b/packages/core/src/pasteRules/markPasteRule.ts @@ -14,14 +14,15 @@ export default function (regexp: RegExp, type: MarkType, getAttrs?: Function) { // eslint-disable-next-line while ((match = regexp.exec(text)) !== null) { - const m = match.length - 1 + const outerMatch = Math.max(match.length - 2, 0) + const innerMatch = Math.max(match.length - 1, 0) - if (parent.type.allowsMarkType(type) && match[1]) { + if (parent.type.allowsMarkType(type)) { const start = match.index - const matchStart = start + match[0].indexOf(match[m - 1]) - const matchEnd = matchStart + match[m - 1].length // TODO: why is there no -1 - const textStart = matchStart + match[m - 1].lastIndexOf(match[m]) - const textEnd = textStart + match[m].length + const matchStart = start + match[0].indexOf(match[outerMatch]) + 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 // adding text before markdown to nodes diff --git a/packages/extension-link/index.ts b/packages/extension-link/index.ts index 1eb4d83a..40b020a8 100644 --- a/packages/extension-link/index.ts +++ b/packages/extension-link/index.ts @@ -16,7 +16,7 @@ declare module '@tiptap/core/src/Editor' { } } -export const pasteRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/gi +export const pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%_+.~#?&//=]*)/gi export default new Mark() .name('link') @@ -55,7 +55,7 @@ export default new Mark() }, })) .pasteRules(({ type }) => [ - markPasteRule(pasteRegex, type), + markPasteRule(pasteRegex, type, (url: string) => ({ href: url })), ]) .plugins(({ options }) => { if (!options.openOnClick) {