From cb5ca55d9efaa18a9c6fb4c6dbdbeb38067916e4 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 7 Dec 2024 13:53:07 -0500 Subject: [PATCH] fix: Allow inline mark matching to work with preceding brackets Refactor markInputRule, add markInputRuleForPattern --- shared/editor/lib/markInputRule.ts | 42 ++++++++++++++++++---------- shared/editor/marks/Bold.ts | 4 +-- shared/editor/marks/Code.ts | 4 +-- shared/editor/marks/Highlight.ts | 4 +-- shared/editor/marks/Italic.ts | 26 ++--------------- shared/editor/marks/Strikethrough.ts | 4 +-- shared/editor/marks/Underline.ts | 4 +-- 7 files changed, 41 insertions(+), 47 deletions(-) diff --git a/shared/editor/lib/markInputRule.ts b/shared/editor/lib/markInputRule.ts index 914225cacf..0e383ce315 100644 --- a/shared/editor/lib/markInputRule.ts +++ b/shared/editor/lib/markInputRule.ts @@ -1,3 +1,4 @@ +import escapeRegExp from "lodash/escapeRegExp"; import { InputRule } from "prosemirror-inputrules"; import { MarkType } from "prosemirror-model"; import { EditorState } from "prosemirror-state"; @@ -7,9 +8,14 @@ import { getMarksBetween } from "../queries/getMarksBetween"; * A factory function for creating Prosemirror plugins that automatically apply a mark to text * that matches a given regular expression. * - * @param regexp The regular expression to match - * @param markType The mark type to apply - * @param getAttrs A function that returns the attributes to apply to the mark + * Assumes the mark is not already applied, and that the regex includes two named capture groups: + * `remove` and `text`. The `remove` group is used to determine what text should be removed from + * the document before applying the mark, and the `text` group is used to determine what text + * should be marked. + * + * @param regexp The regular expression to match. + * @param markType The mark type to apply. + * @param getAttrs An optional function that returns the attributes to apply to the new mark. * @returns The input rule */ export default function markInputRule( @@ -28,16 +34,11 @@ export default function markInputRule( const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs; const { tr } = state; const captureGroup = match.groups?.text ?? match[match.length - 1]; + const removalGroup = match.groups?.remove ?? match[match.length - 2]; const fullMatch = match[0]; - console.log({ - match, - fullMatch, - captureGroup, - }); - if (captureGroup) { - const matchStart = start + fullMatch.indexOf(captureGroup); + const matchStart = start + fullMatch.lastIndexOf(removalGroup); const textStart = start + fullMatch.lastIndexOf(captureGroup); const textEnd = textStart + captureGroup.length; @@ -53,8 +54,10 @@ export default function markInputRule( tr.delete(textEnd, end); } if (textStart > start) { - tr.delete(start, textStart); + tr.delete(matchStart, textStart); } + + start = matchStart; end = start + captureGroup.length; } @@ -65,14 +68,25 @@ export default function markInputRule( ); } -export function markInputRuleForCharacter( - character: string, +/** + * A factory function for creating Prosemirror plugins that automatically apply a mark to text + * is surrounded by a given pattern. + * + * @param pattern The pattern to match. + * @param markType The mark type to apply. + * @param getAttrs An optional function that returns the attributes to apply to the new mark. + * @returns The input rule + */ +export function markInputRuleForPattern( + pattern: string, markType: MarkType, getAttrs?: (match: string[]) => Record ): InputRule { + const escapedPattern = escapeRegExp(pattern); + return markInputRule( new RegExp( - `(?:^|[\\s\\[\\{\\(])(${character}((?[^${character}]+))${character})$` + `(?:^|[\\s\\[\\{\\(])(?${escapedPattern}(?[^${escapedPattern}]+)${escapedPattern})$` ), markType, getAttrs diff --git a/shared/editor/marks/Bold.ts b/shared/editor/marks/Bold.ts index fce24c32e2..240b82a346 100644 --- a/shared/editor/marks/Bold.ts +++ b/shared/editor/marks/Bold.ts @@ -1,7 +1,7 @@ import { toggleMark } from "prosemirror-commands"; import { InputRule } from "prosemirror-inputrules"; import { MarkSpec, MarkType } from "prosemirror-model"; -import markInputRule from "../lib/markInputRule"; +import { markInputRuleForPattern } from "../lib/markInputRule"; import Mark from "./Mark"; const heavyWeightRegex = /^(bold(er)?|[5-9]\d{2,})$/; @@ -33,7 +33,7 @@ export default class Bold extends Mark { } inputRules({ type }: { type: MarkType }): InputRule[] { - return [markInputRule(/(?:\*\*)([^*]+)(?:\*\*)$/, type)]; + return [markInputRuleForPattern("**", type)]; } keys({ type }: { type: MarkType }) { diff --git a/shared/editor/marks/Code.ts b/shared/editor/marks/Code.ts index feffe694b7..ba6f04916f 100644 --- a/shared/editor/marks/Code.ts +++ b/shared/editor/marks/Code.ts @@ -9,7 +9,7 @@ import { } from "prosemirror-model"; import { Plugin, TextSelection } from "prosemirror-state"; import { EditorView } from "prosemirror-view"; -import { markInputRuleForCharacter } from "../lib/markInputRule"; +import { markInputRuleForPattern } from "../lib/markInputRule"; import { MarkdownSerializerState } from "../lib/markdown/serializer"; import { isInCode } from "../queries/isInCode"; import Mark from "./Mark"; @@ -28,7 +28,7 @@ export default class Code extends Mark { } inputRules({ type }: { type: MarkType }) { - return [markInputRuleForCharacter("`", type)]; + return [markInputRuleForPattern("`", type)]; } keys({ type }: { type: MarkType }) { diff --git a/shared/editor/marks/Highlight.ts b/shared/editor/marks/Highlight.ts index f56f2ce2a5..821328d173 100644 --- a/shared/editor/marks/Highlight.ts +++ b/shared/editor/marks/Highlight.ts @@ -1,7 +1,7 @@ import { rgba } from "polished"; import { toggleMark } from "prosemirror-commands"; import { MarkSpec, MarkType } from "prosemirror-model"; -import markInputRule from "../lib/markInputRule"; +import { markInputRuleForPattern } from "../lib/markInputRule"; import markRule from "../rules/mark"; import Mark from "./Mark"; @@ -53,7 +53,7 @@ export default class Highlight extends Mark { } inputRules({ type }: { type: MarkType }) { - return [markInputRule(/(?:==)([^=]+)(?:==)$/, type)]; + return [markInputRuleForPattern("==", type)]; } keys({ type }: { type: MarkType }) { diff --git a/shared/editor/marks/Italic.ts b/shared/editor/marks/Italic.ts index 3c3bf0a30d..514dfa1dd3 100644 --- a/shared/editor/marks/Italic.ts +++ b/shared/editor/marks/Italic.ts @@ -2,7 +2,7 @@ import { toggleMark } from "prosemirror-commands"; import { InputRule } from "prosemirror-inputrules"; import { MarkSpec, MarkType } from "prosemirror-model"; import { Command } from "prosemirror-state"; -import markInputRule from "../lib/markInputRule"; +import { markInputRuleForPattern } from "../lib/markInputRule"; import Mark from "./Mark"; export default class Italic extends Mark { @@ -25,29 +25,9 @@ export default class Italic extends Mark { } inputRules({ type }: { type: MarkType }): InputRule[] { - /** - * Due to use of snake_case strings common in docs the matching conditions - * are a bit more strict than e.g. the ** bold syntax to help prevent - * false positives. - * - * Matches: - * _1_ - * _123_ - * (_one_ - * [_one_ - * - * No match: - * __ - * __123_ - * __123__ - * _123 - * one_123_ - * ONE_123_ - * 1_123_ - */ return [ - markInputRule(/(?:^|[^_a-zA-Z0-9])(_([^_]+)_)$/, type), - markInputRule(/(?:^|[^*a-zA-Z0-9])(\*([^*]+)\*)$/, type), + markInputRuleForPattern("_", type), + markInputRuleForPattern("*", type), ]; } diff --git a/shared/editor/marks/Strikethrough.ts b/shared/editor/marks/Strikethrough.ts index cf55a58683..3acc653e26 100644 --- a/shared/editor/marks/Strikethrough.ts +++ b/shared/editor/marks/Strikethrough.ts @@ -1,6 +1,6 @@ import { toggleMark } from "prosemirror-commands"; import { MarkSpec, MarkType } from "prosemirror-model"; -import markInputRule from "../lib/markInputRule"; +import { markInputRuleForPattern } from "../lib/markInputRule"; import Mark from "./Mark"; export default class Strikethrough extends Mark { @@ -36,7 +36,7 @@ export default class Strikethrough extends Mark { } inputRules({ type }: { type: MarkType }) { - return [markInputRule(/~([^~]+)~$/, type)]; + return [markInputRuleForPattern("~", type)]; } toMarkdown() { diff --git a/shared/editor/marks/Underline.ts b/shared/editor/marks/Underline.ts index b65223e34f..ede1bcbbb0 100644 --- a/shared/editor/marks/Underline.ts +++ b/shared/editor/marks/Underline.ts @@ -1,6 +1,6 @@ import { toggleMark } from "prosemirror-commands"; import { MarkSpec, MarkType } from "prosemirror-model"; -import markInputRule from "../lib/markInputRule"; +import { markInputRuleForPattern } from "../lib/markInputRule"; import underlinesRule from "../rules/underlines"; import Mark from "./Mark"; @@ -32,7 +32,7 @@ export default class Underline extends Mark { } inputRules({ type }: { type: MarkType }) { - return [markInputRule(/(?:__)([^_]+)(?:__)$/, type)]; + return [markInputRuleForPattern("__", type)]; } keys({ type }: { type: MarkType }) {