A platform for community discussion. Free, open, simple.
UX: improve input rules for rich editor autolink/linkify (#32366)
Renato Atilio6abda2a3ef06
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.js b/app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.jsindex 461364fc53d..2b5e9d69569 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/core/inputrules.js@@ -64,7 +64,7 @@ function processInputRule(inputRule, params) {}if (inputRule instanceof Function) {- inputRule = inputRule(params);+ return processInputRule(inputRule(params));}if (inputRule instanceof InputRule) {diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.jsindex c3f1af81448..c8cf876917b 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js@@ -1,7 +1,9 @@import { ReplaceAroundStep, ReplaceStep } from "prosemirror-transform";-import { getChangedRanges } from "discourse/static/prosemirror/lib/plugin-utils";+import {+ getChangedRanges,+ markInputRule,+} from "discourse/static/prosemirror/lib/plugin-utils";-const AUTO_LINKS = ["autolink", "linkify"];const REPLACE_STEPS = [ReplaceStep, ReplaceAroundStep];/** @type {RichEditorExtension} */@@ -27,6 +29,7 @@ const extension = {title: dom.getAttribute("title"),attachment: dom.classList.contains("attachment"),"data-orig-href": dom.getAttribute("data-orig-href"),+ markup: dom.getAttribute("data-markup"),};},},@@ -39,6 +42,7 @@ const extension = {title: node.attrs.title,class: node.attrs.attachment ? "attachment" : undefined,"data-orig-href": node.attrs["data-orig-href"],+ "data-markup": node.attrs.markup || undefined,},0,];@@ -110,63 +114,69 @@ const extension = {mixable: true,},},- inputRules: ({ schema, utils }) =>- utils.markInputRule(+ inputRules: ({ schema }) => [+ markInputRule(/\[([^\]]+)]\(([^)\s]+)(?:\s+[“"']([^“"']+)[”"'])?\)$/,schema.marks.link,(match) => {return { href: match[2], title: match[3] };}),- plugins: ({ pmState: { Plugin }, utils }) => [+ markInputRule(+ // AUTOLINK_RE from https://github.com/markdown-it/markdown-it/blob/master/lib/rules_inline/autolink.mjs+ /<([a-zA-Z][a-zA-Z0-9+.-]{1,31}:[^<>\x00-\x20]*)>$/,+ schema.marks.link,+ (match) => {+ return { href: match[1], markup: "autolink" };+ }+ ),+ ],+ plugins: ({ pmState: { Plugin }, utils }) =>new Plugin({props: {- // Auto-linkify plain-text pasted URLs+ // Auto-linkify plain-text pasted URLs over a selectionclipboardTextParser(text, $context, plain, view) {if (view.state.selection.empty || !utils.getLinkify().test(text)) {return;}- return addLinkMark(view, text);+ return addLinkMark(view, text, utils);},+ // Auto-linkify pasted rich content with a single text node that is a URL over a selection+ transformPasted(slice, view) {+ if (view.state.selection.empty) {+ return slice;+ }- // Auto-linkify rich content with a single text node that is a URL- transformPasted(paste, view) {let node = null;- if (paste.content.childCount === 1) {- if (paste.content.firstChild.isText) {- node = paste.content.firstChild;+ if (slice.content.childCount === 1) {+ if (slice.content.firstChild.isText) {+ node = slice.content.firstChild;} else if (- paste.content.firstChild.type.name === "paragraph" &&- paste.content.firstChild.childCount === 1 &&- paste.content.firstChild.firstChild.isText+ slice.content.firstChild.type.name === "paragraph" &&+ slice.content.firstChild.childCount === 1 &&+ slice.content.firstChild.firstChild.isText) {- node = paste.content.firstChild.firstChild;+ node = slice.content.firstChild.firstChild;}}if (!node?.text ||- node?.marks.some((mark) => mark.type.name === "link")+ node?.marks.some(+ (mark) => mark.type.name === "link" || mark.type.name === "code"+ ) ||+ !utils.getLinkify().test(node.text)) {- return paste;+ return slice;}- const matches = utils.getLinkify().match(node.text);- const isFullMatch =- matches && matches.length === 1 && matches[0].raw === node.text;-- if (!isFullMatch) {- return paste;- }-- return addLinkMark(view, node.text);+ return addLinkMark(view, node.text, utils);},},- }),- // plugin for auto-linking during typing- new Plugin({++ // Automatically adds and removes link marks when typingappendTransaction(transactions, prevState, state) {const transaction = prevState.tr;transactions@@ -189,13 +199,17 @@ const extension = {from = Math.max(from - 1, 0);to = Math.min(to + 1, state.doc.nodeSize - 2);}++ // stores the nodes visited ahead, skipping a node if already seen in nodeAfter+ const visited = new Set();state.doc.nodesBetween(from, to, (node, pos) => {if (+ visited.has(node) ||!node.isText ||node.marks.some((mark) =>(mark.type.name === "link" &&- !AUTO_LINKS.includes(mark.attrs.markup)) ||+ mark.attrs.markup !== "linkify") ||mark.type.name === "code")) {@@ -223,6 +237,7 @@ const extension = {const nodeBefore = state.doc.nodeAt(pos - 1);let textBefore = "";+if (wordStart === 0 &&nodeBefore?.isText &&@@ -230,11 +245,9 @@ const extension = {nodeBefore.text[nodeBefore.text.length - 1]) &&!utils.isWhiteSpace(text[0]) &&- nodeBefore.marks.length === 1 &&- nodeBefore.marks.some(+ !nodeBefore.marks.some((mark) =>- mark.type.name === "link" &&- AUTO_LINKS.includes(mark.attrs.markup)+ mark.type.name === "link" && mark.attrs.markup !== "linkify")) {textBefore = nodeBefore.text;@@ -250,11 +263,11 @@ const extension = {nodeAfter.marks.length === 1 &&nodeAfter.marks.some((mark) =>- mark.type.name === "link" &&- AUTO_LINKS.includes(mark.attrs.markup)+ mark.type.name === "link" && mark.attrs.markup === "linkify")) {textAfter = nodeAfter.text;+ visited.add(nodeAfter);}const fullText = textBefore + textSlice + textAfter;@@ -267,11 +280,15 @@ const extension = {state.schema.marks.link);+ if (!utils.getLinkify().test(fullText)) {+ return;+ }+utils.getLinkify().match(fullText)?.forEach((match) => {- // small exception when we're typing `www.link.com+ // ignore if the match is just after a `if (fullText[match.index - 1] === "`") {return;}@@ -291,15 +308,21 @@ const extension = {return tr;},}),- ],};-function addLinkMark(view, href) {- const { from, to } = view.state.selection;- const linkMark = view.state.schema.marks.link.create({ href });+function addLinkMark(view, text, utils) {+ const matches = utils.getLinkify().match(text);+ const isFullMatch = matches?.length === 1 && matches[0].raw === text;++ if (!isFullMatch) {+ return;+ }+ const { from, to } = view.state.selection;const tr = view.state.tr;- tr.addMark(from, to, linkMark);++ // used only when replacing the selection, so no markup: linkify+ tr.addMark(from, to, view.state.schema.marks.link.create({ href: text }));return tr.doc.slice(from, to);}diff --git a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/link-test.js b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/link-test.jsindex 9ecda885104..feb16c5cf2e 100644--- a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/link-test.js+++ b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/link-test.js@@ -20,7 +20,7 @@ module(],autolink: ["<https://example.com>",- '<p><a href="https://example.com">https://example.com</a></p>',+ '<p><a href="https://example.com" data-markup="autolink">https://example.com</a></p>',"<https://example.com>",],"attachment link": [diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rbindex c42870fbb27..ac7a42e45f8 100644--- a/spec/system/composer/prosemirror_editor_spec.rb+++ b/spec/system/composer/prosemirror_editor_spec.rb@@ -185,6 +185,17 @@ describe "Composer - ProseMirror editor", type: :system doexpect(rich).to have_css("hr", count: 5)end++ it "supports <http://example.com> to create an 'autolink'" do+ open_composer_and_toggle_rich_editor+ composer.type_content("<http://example.com>")++ expect(rich).to have_css("a", text: "http://example.com")++ composer.toggle_rich_editor++ expect(composer).to have_value("<http://example.com>")+ endendcontext "with oneboxing" do@@ -538,7 +549,7 @@ describe "Composer - ProseMirror editor", type: :system do)end- it "respects existing marks when pasting a url to make a link" do+ it "respects existing marks when pasting a url over a selection" docdp.allow_clipboardopen_composer_and_toggle_rich_editorcdp.copy_paste("not selected `code`**bold**not*italic* not selected")@@ -557,7 +568,7 @@ describe "Composer - ProseMirror editor", type: :system do)end- it "auto-links pasted URLs from text/html" do+ it "auto-links pasted URLs from text/html over a selection" docdp.allow_clipboardopen_composer_and_toggle_rich_editor@@ -623,21 +634,23 @@ describe "Composer - ProseMirror editor", type: :system doendend- describe "auto-linking/unlinking" do+ describe "auto-linking/unlinking while typing" doit "auto-links non-protocol URLs and removes the link when no longer a URL" doopen_composer_and_toggle_rich_editor- composer.type_content("www.example.com")+ composer.type_content("www.example.com and also mid-paragraph www.example2.com")expect(rich).to have_css("a", text: "www.example.com")+ expect(rich).to have_css("a", text: "www.example2.com")+ expect(rich).to have_css("a", count: 2)composer.send_keys(%i[backspace backspace])- expect(rich).to have_no_css("a")+ expect(rich).to have_css("a", count: 1)composer.type_content("om")- expect(rich).to have_css("a", text: "www.example.com")+ expect(rich).to have_css("a", text: "www.example2.com")endit "auto-links protocol URLs" do@@ -672,6 +685,19 @@ describe "Composer - ProseMirror editor", type: :system doexpect(rich).to have_css("code", text: "code mark https://example.com")expect(rich).to have_no_css("a", text: "https://example.com")end++ it "doesn't continue a <https://url> markup='autolink'" do+ open_composer_and_toggle_rich_editor++ composer.type_content("<https://example.com>.de")++ expect(rich).to have_css("a", text: "https://example.com")+ expect(rich).to have_no_css("a", text: "https://example.com.de")++ composer.toggle_rich_editor++ expect(composer).to have_value("<https://example.com>.de")+ endenddescribe "uploads" do