Skip to content

A platform for community discussion. Free, open, simple.

FIX: prosemirror input rule code mark check (#35416)

Renato Atiliof50b637354e8
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/lib/plugin-utils.js b/app/assets/javascripts/discourse/app/static/prosemirror/lib/plugin-utils.js
index 404e94d5dc8..f15eda13986 100644
--- a/app/assets/javascripts/discourse/app/static/prosemirror/lib/plugin-utils.js
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/lib/plugin-utils.js
@@ -5,51 +5,87 @@ export { getLinkify, isBoundary, isWhiteSpace } from "../lib/markdown-it";
export { buildBBCodeAttrs } from "discourse/lib/text";
-// https://discuss.prosemirror.net/t/input-rules-for-wrapping-marks/537
+/**
+ * Creates an InputRule that applies a mark based on a regex pattern.
+ *
+ * Makes assumptions about having a single matching group, which can be rewritten with getAttrs
+ *
+ * Initially from https://discuss.prosemirror.net/t/input-rules-for-wrapping-marks/537
+ *
+ * For usage examples,
+ * @see discourse/app/static/prosemirror/core/inputrules.js
+ *
+ * @param {RegExp} regexp
+ * @param {import("prosemirror-model").MarkType} markType
+ * @param {Function} getAttrs
+ *
+ * @returns {import("prosemirror-inputrules").InputRule}
+ */
export function markInputRule(regexp, markType, getAttrs) {
- return new InputRule(
- regexp,
- (state, match, start, end) => {
- const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs;
- const tr = state.tr;
-
- // attrs may override match or start
- const {
- match: attrsMatch,
- start: attrsStart,
- ...markAttrs
- } = attrs ?? {};
- match = attrsMatch ?? match;
- start += attrsStart ?? 0;
-
- if (match[1]) {
- const textStart = start + match[0].indexOf(match[1]);
- const textEnd = textStart + match[1].length;
- if (textEnd < end) {
- tr.delete(textEnd, end);
- }
- if (textStart > start) {
- tr.delete(start, textStart);
+ return new InputRule(regexp, (state, match, start, end) => {
+ const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs;
+ const tr = state.tr;
+
+ // attrs may override match or start
+ const { match: attrsMatch, start: attrsStart, ...markAttrs } = attrs ?? {};
+
+ match = attrsMatch ?? match;
+ start = start + (attrsStart ?? 0);
+
+ if (match[1]) {
+ const fullMatch = match[0];
+ const capturedContent = match[1];
+ const contentStart = fullMatch.indexOf(capturedContent);
+ const contentEnd = contentStart + capturedContent.length;
+
+ const ranges = [];
+ if (contentStart > 0) {
+ ranges.push([start, start + contentStart]);
+ }
+ if (contentEnd < fullMatch.length) {
+ ranges.push([start + contentEnd, start + fullMatch.length]);
+ }
+
+ for (const [rangeStart, rangeEnd] of ranges) {
+ let hasCodeMark = false;
+ state.doc.nodesBetween(rangeStart, rangeEnd, (node) => {
+ if (node.isInline && node.marks.some((m) => m.type.spec.code)) {
+ hasCodeMark = true;
+ return false;
+ }
+ });
+ if (hasCodeMark) {
+ return null;
}
+ }
+ }
- tr.addMark(start, start + match[1].length, markType.create(markAttrs));
- tr.setStoredMarks(tr.doc.resolve(start + match[1].length + 1).marks());
- } else {
- tr.delete(start, end);
- tr.insertText(" ");
- tr.addMark(start, start + 1, markType.create(markAttrs));
- tr.removeStoredMark(markType);
- tr.insertText(" ");
-
- tr.setSelection(
- state.selection.constructor.create(tr.doc, start, start + 1)
- );
+ if (match[1]) {
+ const textStart = start + match[0].indexOf(match[1]);
+ const textEnd = textStart + match[1].length;
+ if (textEnd < end) {
+ tr.delete(textEnd, end);
+ }
+ if (textStart > start) {
+ tr.delete(start, textStart);
}
- return tr;
- },
- { inCodeMark: false }
- );
+ tr.addMark(start, start + match[1].length, markType.create(markAttrs));
+ tr.setStoredMarks(tr.doc.resolve(start + match[1].length + 1).marks());
+ } else {
+ tr.delete(start, end);
+ tr.insertText(" ");
+ tr.addMark(start, start + 1, markType.create(markAttrs));
+ tr.removeStoredMark(markType);
+ tr.insertText(" ");
+
+ tr.setSelection(
+ state.selection.constructor.create(tr.doc, start, start + 1)
+ );
+ }
+
+ return tr;
+ });
}
export function getChangedRanges(tr) {
diff --git a/app/assets/javascripts/discourse/package.json b/app/assets/javascripts/discourse/package.json
index 9a41e74d9e6..870175b8538 100644
--- a/app/assets/javascripts/discourse/package.json
+++ b/app/assets/javascripts/discourse/package.json
@@ -60,7 +60,7 @@
"prosemirror-gapcursor": "^1.4.0",
"prosemirror-highlightjs": "^0.9.1",
"prosemirror-history": "^1.4.1",
- "prosemirror-inputrules": "^1.5.0",
+ "prosemirror-inputrules": "^1.5.1",
"prosemirror-keymap": "^1.2.3",
"prosemirror-markdown": "^1.13.2",
"prosemirror-model": "^1.25.3",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 464618878c7..110c7f3c719 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -346,7 +346,7 @@ importers:
version: link:../pretty-text
prosemirror-codemark:
specifier: ^0.4.2
prosemirror-commands:
specifier: ^1.7.1
version: 1.7.1
@@ -363,8 +363,8 @@ importers:
specifier: ^1.4.1
version: 1.4.1
prosemirror-inputrules:
- specifier: ^1.5.0
- version: 1.5.0
+ specifier: ^1.5.1
+ version: 1.5.1
prosemirror-keymap:
specifier: ^1.2.3
version: 1.2.3
@@ -7613,8 +7613,8 @@ packages:
resolution: {integrity: sha512-2JZD8z2JviJrboD9cPuX/Sv/1ChFng+xh2tChQ2X4bB2HeK+rra/bmJ3xGntCcjhOqIzSDG6Id7e8RJ9QPXLEQ==}
- resolution: {integrity: sha512-K0xJRCmt+uSw7xesnHmcn72yBGTbY45vm8gXI4LZXbx2Z0jwh5aF9xrGQgrVPu0WbyFVFF3E/o9VhJYz6SQWnA==}
+ resolution: {integrity: sha512-7wj4uMjKaXWAQ1CDgxNzNtR9AlsuwzHfdFH1ygEHA2KHF2DOEaXl1CJfNPAKCg9qNEh4rum975QLaCiQPyY6Fw==}
resolution: {integrity: sha512-4HucRlpiLd1IPQQXNqeo81BGtkY8Ai5smHhKW9jjPKRc2wQIxksg7Hl1tTI2IfT2B/LgX6bfYvXxEpJl7aKYKw==}
@@ -17279,9 +17279,9 @@ snapshots:
retry: 0.12.0
signal-exit: 3.0.7
dependencies:
- prosemirror-inputrules: 1.5.0
+ prosemirror-inputrules: 1.5.1
prosemirror-model: 1.25.3
prosemirror-state: 1.4.3
prosemirror-view: 1.41.3
@@ -17319,7 +17319,7 @@ snapshots:
prosemirror-view: 1.41.3
rope-sequence: 1.3.4
dependencies:
prosemirror-state: 1.4.3
prosemirror-transform: 1.10.4
diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rb
index d558f0d548f..64a8e578f72 100644
--- a/spec/system/composer/prosemirror_editor_spec.rb
+++ b/spec/system/composer/prosemirror_editor_spec.rb
@@ -330,6 +330,14 @@ describe "Composer - ProseMirror editor", type: :system do
expect(rich).to have_no_css("aside.quote")
expect(rich).to have_content("This [quote] should not trigger")
end
+
+ it "avoids applying input rules in inline code if part of the matched text" do
+ open_composer
+ composer.type_content("This `__code` should not__ be bold")
+
+ expect(rich).to have_no_css("strong")
+ expect(rich).to have_css("code", text: "__code")
+ end
end
context "with oneboxing" do