From 98cf0e425a73e6a8d94688d50403422a55fe4387 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Tue, 7 May 2019 18:50:04 +0200 Subject: [PATCH] fix markInputRules for italics mark add another layer of matching by introducing support for second match group since javascript (at least in current browsers) still lacks lookbehind in regex so now supports /nomatch(markstart(text)markend)nomatch/ and still supports the /markstart(text)markend/ syntax all `nomatch` will be kept as is so kindof simulating lookbehinds --- .../src/commands/markInputRule.js | 29 +++++++++++-------- .../tiptap-extensions/src/marks/Italic.js | 3 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/tiptap-commands/src/commands/markInputRule.js b/packages/tiptap-commands/src/commands/markInputRule.js index 066760e6c..d37d29c4d 100644 --- a/packages/tiptap-commands/src/commands/markInputRule.js +++ b/packages/tiptap-commands/src/commands/markInputRule.js @@ -3,23 +3,28 @@ import { InputRule } from 'prosemirror-inputrules' export default function (regexp, markType, getAttrs) { return new InputRule(regexp, (state, match, start, end) => { const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs - const { tr } = state - let markEnd = end + const { tr } = state + const m = match.length - 1 + let markEnd = end + let markStart = start - if (match[1]) { - const startSpaces = match[0].search(/\S/) - const textStart = start + match[0].indexOf(match[1]) - const textEnd = textStart + match[1].length - if (textEnd < end) { - tr.delete(textEnd, end) + if (match[m]) { + const matchStart = start + match[0].indexOf(match[m - 1]) + const matchEnd = matchStart + match[m - 1].length - 1 + const textStart = matchStart + match[m - 1].lastIndexOf(match[m]) + const textEnd = textStart + match[m].length + + if (textEnd < matchEnd) { + tr.delete(textEnd, matchEnd) } - if (textStart > start) { - tr.delete(start + startSpaces, textStart) + if (textStart > matchStart) { + tr.delete(matchStart, textStart) } - markEnd = start + startSpaces + match[1].length + markStart = matchStart + markEnd = markStart + match[m].length } - tr.addMark(start, markEnd, markType.create(attrs)) + tr.addMark(markStart, markEnd, markType.create(attrs)) tr.removeStoredMark(markType) // Do not continue with mark. return tr }) diff --git a/packages/tiptap-extensions/src/marks/Italic.js b/packages/tiptap-extensions/src/marks/Italic.js index 17eaf3c98..30f686218 100644 --- a/packages/tiptap-extensions/src/marks/Italic.js +++ b/packages/tiptap-extensions/src/marks/Italic.js @@ -30,7 +30,8 @@ export default class Italic extends Mark { inputRules({ type }) { return [ - markInputRule(/(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)$/, type), + markInputRule(/(?:^|[^_])(_([^_]+)_)$/, type), + markInputRule(/(?:^|[^*])(\*([^*]+)\*)$/, type), ] }