A platform for community discussion. Free, open, simple.
FIX: Prevent URL anchors from triggering hashtag autocomplete in rich editor (#37306)
Régis Hanolf6954ad79294
diff --git a/frontend/discourse-markdown-it/src/features/hashtag-autocomplete.js b/frontend/discourse-markdown-it/src/features/hashtag-autocomplete.jsindex d19ca148ac1..a4d94914059 100644--- a/frontend/discourse-markdown-it/src/features/hashtag-autocomplete.js+++ b/frontend/discourse-markdown-it/src/features/hashtag-autocomplete.js@@ -114,7 +114,7 @@ function addIconPlaceholder(buffer, state) {export function setup(helper) {helper.registerPlugin((md) => {const rule = {- matcher: /#([\u00C0-\u1FFF\u2C00-\uD7FF\w:-]{1,101})/,+ matcher: /(?<!\/)#([\u00C0-\u1FFF\u2C00-\uD7FF\w:-]{1,101})/,onMatch: addHashtag,};diff --git a/frontend/discourse/app/lib/hashtag-autocomplete.js b/frontend/discourse/app/lib/hashtag-autocomplete.jsindex 3ac5c67ffc8..61a019c23f7 100644--- a/frontend/discourse/app/lib/hashtag-autocomplete.js+++ b/frontend/discourse/app/lib/hashtag-autocomplete.js@@ -43,8 +43,8 @@ export function setupHashtagAutocomplete();}-export async function hashtagTriggerRule({ inCodeBlock }) {- return !(await inCodeBlock());+export async function hashtagTriggerRule({ inCodeBlock, inLink }) {+ return !(await inCodeBlock()) && !(await inLink?.());}export function hashtagAutocompleteOptions(diff --git a/frontend/discourse/app/modifiers/d-autocomplete.js b/frontend/discourse/app/modifiers/d-autocomplete.jsindex f05682a31bd..97af3d2f8d8 100644--- a/frontend/discourse/app/modifiers/d-autocomplete.js+++ b/frontend/discourse/app/modifiers/d-autocomplete.js@@ -81,7 +81,7 @@ export default class DAutocompleteModifier extends Modifier {targetElement = null;// Constants- ALLOWED_LETTERS_REGEXP = /[\s[{(/+]/;+ ALLOWED_LETTERS_REGEXP = /[\s[{(+]/;TRIGGER_CHAR_RELATIVE_OFFSET = 9;VERTICAL_RELATIVE_OFFSET = 10;@@ -197,6 +197,7 @@ export default class DAutocompleteModifier extends Modifier {const triggerContext = {...opts,inCodeBlock: () => this.options.textHandler.inCodeBlock(),+ inLink: () => this.options.textHandler.inLink?.() ?? false,};const triggerRuleResult = await this.options.triggerRule(this.targetElement,diff --git a/frontend/discourse/app/static/prosemirror/extensions/hashtag.js b/frontend/discourse/app/static/prosemirror/extensions/hashtag.jsindex bd17d521b38..b5fbdf29eae 100644--- a/frontend/discourse/app/static/prosemirror/extensions/hashtag.js+++ b/frontend/discourse/app/static/prosemirror/extensions/hashtag.js@@ -51,6 +51,15 @@ const extension = {handler: (state, match, start, end) => {const hashtagStart = start + match[1].length;const name = match[2].slice(1);++ // Don't convert anchors in URLs to hashtags+ const $pos = state.doc.resolve(hashtagStart);+ if (+ $pos.marks().some((mark) => mark.type === state.schema.marks.link)+ ) {+ return false;+ }+return (state.selection.$from.nodeBefore?.type !==state.schema.nodes.hashtag &&diff --git a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.jsindex ba7a9a976f5..4b94070275c 100644--- a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js+++ b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js@@ -569,6 +569,13 @@ class ProsemirrorAutocompleteHandler {return isInCodeBlock || hasCodeMark;}++ async inLink() {+ const { schema, view } = this;+ const { $from } = view.state.selection;++ return $from.marks().some((mark) => mark.type === schema.marks.link);+ }}/** @implements {PlaceholderHandler} */diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/hashtag-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/hashtag-test.jsindex 83362f14754..064af2040f1 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/hashtag-test.js+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/hashtag-test.js@@ -94,6 +94,11 @@ module(`<p><a class="hashtag-cooked" data-name="welcome" data-processed="true" contenteditable="false" draggable="true"><span class=\"hashtag-category-square hashtag-color--category-5\"></span>hello <img width=\"20\" height=\"20\" src=\"/images/emoji/twitter/wave.png?v=${v}\" title=\"wave\" alt=\"wave\" class=\"emoji\"></a></p>`,"#welcome",],+ "anchor in link matching hashtag is not converted": [+ "[Docs](https://example.com/docs/#product)",+ '<p><a href="https://example.com/docs/#product">Docs</a></p>',+ "[Docs](https://example.com/docs/#product)",+ ],};Object.entries(testCases).forEach(diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/link-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/link-test.jsindex 6739430357c..1b9d1213343 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/link-test.js+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/link-test.js@@ -38,6 +38,11 @@ module('<p><a href="/404" class="attachment" data-orig-href="upload://some-hash">File</a></p>',"[File|attachment](upload://some-hash)",],+ "link with anchor": [+ "[Docs](https://example.com/docs/#section)",+ '<p><a href="https://example.com/docs/#section">Docs</a></p>',+ "[Docs](https://example.com/docs/#section)",+ ],}).forEach(([name, [markdown, html, expectedMarkdown]]) => {test(name, async function (assert) {this.siteSettings.rich_editor = true;diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rbindex b0c78f228f8..1afcdab6bf0 100644--- a/spec/lib/pretty_text_spec.rb+++ b/spec/lib/pretty_text_spec.rb@@ -1905,13 +1905,19 @@ RSpec.describe PrettyText do) dowith_tag("span", with: { class: "hashtag-icon-placeholder" })end+ end- # ensure it does not fight with the autolinker+ it "does not fight with the autolinker" doexpect(PrettyText.cook(" http://somewhere.com/#known")).not_to include("hashtag")expect(PrettyText.cook(" http://somewhere.com/?#known")).not_to include("hashtag")expect(PrettyText.cook(" http://somewhere.com/?abc#known")).not_to include("hashtag")end+ it "does not trigger when preceeded with a slash" do+ expect(PrettyText.cook("/#known")).not_to include("hashtag")+ expect(PrettyText.cook("test/#known")).not_to include("hashtag")+ end+it "can handle mixed lists" do# known bug in old md enginecooked = PrettyText.cook("* a\n\n1. b")diff --git a/spec/system/hashtag_autocomplete_spec.rb b/spec/system/hashtag_autocomplete_spec.rbindex fe4cd8aec15..124e1f93ee8 100644--- a/spec/system/hashtag_autocomplete_spec.rb+++ b/spec/system/hashtag_autocomplete_spec.rb@@ -242,6 +242,20 @@ describe "Using #hashtag autocompletion to search for and lookup categories andendend+ it "does not show hashtag autocomplete when typing a URL with anchor" do+ topic_page.visit_topic_and_open_composer(topic)+ expect(topic_page).to have_expanded_composer++ # normal autocomplete works+ topic_page.type_in_composer("#co")+ expect(page).to have_css(".hashtag-autocomplete")++ # autocomplete does not trigger within a URL+ topic_page.clear_composer+ topic_page.type_in_composer("discourse.org/#co")+ expect(page).to have_no_css(".hashtag-autocomplete", wait: 2)+ end+context "when a user cannot access the category for a hashtag cooked in another post" dofab!(:admin)fab!(:manager_group) { Fabricate(:group, name: "Managers") }