mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-19 06:43:02 +08:00
added markdown handler for marks
This commit is contained in:
parent
8d44a4c6fc
commit
983643f789
57
packages/tiptap-commands/src/commands/markPasteRule.js
Normal file
57
packages/tiptap-commands/src/commands/markPasteRule.js
Normal 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),
|
||||
},
|
||||
})
|
||||
|
||||
}
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user