A platform for community discussion. Free, open, simple.
DEV: replace rich editor input rules regex lookbehind (#31909)
Renato Atilioa0a5b2889fff
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.js b/app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.jsindex c95a1e9d39d..461364fc53d 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.js@@ -14,6 +14,12 @@ export function buildInputRules(extensions, params, includeDefault = true) {if (includeDefault) {const schema = params.schema;+ // adapt the 2 groups to the single group that markInputRule expects+ const getAttrs = (match) => ({+ match: [match[0].replace(match[1], ""), match[2]],+ start: match[1].length,+ });+rules.push(// TODO(renato) smartQuotes should respect `markdown_typographer_quotation_marks`...smartQuotes,@@ -29,9 +35,9 @@ export function buildInputRules(extensions, params, includeDefault = true) {return rule;}),markInputRule(/\*\*([^*]+)\*\*$/, schema.marks.strong),- markInputRule(/(?<=^|\s)__([^_]+)__$/, schema.marks.strong),- markInputRule(/(?:^|(?<!\*))\*([^*]+)\*$/, schema.marks.em),- markInputRule(/(?<=^|\s)_([^_]+)_$/, schema.marks.em),+ markInputRule(/(^|\s)__([^_]+)__$/, schema.marks.strong, getAttrs),+ markInputRule(/(^|[^*])\*([^*]+)\*$/, schema.marks.em, getAttrs),+ markInputRule(/(^|\s)_([^_]+)_$/, schema.marks.em, getAttrs),markInputRule(/`([^`]+)`$/, schema.marks.code),new InputRule(/^(\u2013-|\u2014-|___\s|\*\*\*\s)$/,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.jsindex 3908563cbba..8b5750af3f7 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@@ -13,6 +13,15 @@ export function markInputRule(regexp, markType, getAttrs) {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]) {let textStart = start + match[0].indexOf(match[1]);let textEnd = textStart + match[1].length;@@ -22,14 +31,13 @@ export function markInputRule(regexp, markType, getAttrs) {if (textStart > start) {tr.delete(start, textStart);}- end = start + match[1].length;- tr.addMark(start, end, markType.create(attrs));+ tr.addMark(start, start + match[1].length, markType.create(markAttrs));tr.removeStoredMark(markType);} else {tr.delete(start, end);tr.insertText(" ");- tr.addMark(start, start + 1, markType.create(attrs));+ tr.addMark(start, start + 1, markType.create(markAttrs));tr.removeStoredMark(markType);tr.insertText(" ");diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rbindex 3959b479de1..227db956325 100644--- a/spec/system/composer/prosemirror_editor_spec.rb+++ b/spec/system/composer/prosemirror_editor_spec.rb@@ -131,17 +131,33 @@ describe "Composer - ProseMirror editor", type: :system doit "supports _ or * to create an italic text" doopen_composer_and_toggle_rich_editorcomposer.type_content("_This is italic_\n")- composer.type_content("*This is italic*")+ composer.type_content("Hey _This is italic_\n")+ composer.type_content("*This is italic*\n")+ composer.type_content("Hey*This is italic*\n")- expect(rich).to have_css("em", text: "This is italic", count: 2)+ expect(rich).to have_css("em", text: "This is italic", count: 4)++ composer.toggle_rich_editor++ expect(composer).to have_value(+ "*This is italic*\n\nHey *This is italic*\n\n*This is italic*\n\nHey*This is italic*",+ )endit "supports __ or ** to create a bold text" doopen_composer_and_toggle_rich_editorcomposer.type_content("__This is bold__\n")- composer.type_content("**This is bold**")+ composer.type_content("**This is bold**\n")+ composer.type_content("Hey __This is bold__\n")+ composer.type_content("Hey**This is bold**")- expect(rich).to have_css("strong", text: "This is bold", count: 2)+ expect(rich).to have_css("strong", text: "This is bold", count: 4)++ composer.toggle_rich_editor++ expect(composer).to have_value(+ "**This is bold**\n\n**This is bold**\n\nHey **This is bold**\n\nHey**This is bold**",+ )endit "supports ` to create a code text" do