added markdown handler for marks

This commit is contained in:
Chrissi2812 2019-01-31 15:07:55 +01:00
parent 8d44a4c6fc
commit 983643f789
No known key found for this signature in database
GPG Key ID: B4B82C7E618271DA
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,57 @@
import { Plugin } from 'prosemirror-state'
import { Slice, Fragment } from 'prosemirror-model'
export default function (regexp, type, getAttrs) {
const handler = fragment => {
const nodes = []
fragment.forEach(child => {
if (child.isText) {
const { text } = child
let pos = 0
let match
// eslint-disable-next-line
while ((match = regexp.exec(text)) !== null) {
if (match[1]) {
const start = match.index
const end = start + match[0].length
const textStart = start + match[0].indexOf(match[1])
const textEnd = textStart + match[1].length
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
// adding text before markdown to nodes
if (start > 0) {
nodes.push(child.cut(pos, start))
}
// adding the markdown part to nodes
nodes.push(child
.cut(textStart, textEnd)
.mark(type.create(attrs)
.addToSet(child.marks)))
pos = end
}
}
// adding rest of text to nodes
if (pos < text.length) {
nodes.push(child.cut(pos))
}
} else {
nodes.push(child.copy(handler(child.content)))
}
})
return Fragment.fromArray(nodes)
}
return new Plugin({
props: {
transformPasted: slice => new Slice(handler(slice.content), slice.openStart, slice.openEnd),
},
})
}

View File

@ -41,6 +41,7 @@ import {
import insertText from './commands/insertText'
import markInputRule from './commands/markInputRule'
import pasteRule from './commands/pasteRule'
import markPasteRule from './commands/markPasteRule'
import removeMark from './commands/removeMark'
import replaceText from './commands/replaceText'
import setInlineBlockType from './commands/setInlineBlockType'
@ -91,6 +92,7 @@ export {
// custom
insertText,
markInputRule,
markPasteRule,
pasteRule,
removeMark,
replaceText,