Skip to content

Commit

Permalink
fix: fix a bug that messed up pasted link attributes, fix #1284
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed May 7, 2021
1 parent c86e411 commit 7da647d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 8 additions & 2 deletions packages/core/src/pasteRules/markPasteRule.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> | ((match: RegExpExecArray) => Record<string, any>),
): Plugin {
const handler = (fragment: Fragment, parent?: any) => {
const nodes: any[] = []

Expand All @@ -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) {
Expand Down
11 changes: 8 additions & 3 deletions packages/extension-link/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LinkOptions>({
Expand Down Expand Up @@ -97,8 +103,7 @@ export const Link = Mark.create<LinkOptions>({

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] })),
]
},

Expand Down

0 comments on commit 7da647d

Please sign in to comment.