A platform for community discussion. Free, open, simple.
FEATURE: Allow dots in the middle of tag names (#39679)
Sam246f9a3276ba
diff --git a/frontend/discourse-markdown-it/src/features/hashtag-autocomplete.js b/frontend/discourse-markdown-it/src/features/hashtag-autocomplete.jsindex a4d94914059..8f0d983c1d9 100644--- a/frontend/discourse-markdown-it/src/features/hashtag-autocomplete.js+++ b/frontend/discourse-markdown-it/src/features/hashtag-autocomplete.js@@ -114,7 +114,8 @@ 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:-](?:[\u00C0-\u1FFF\u2C00-\uD7FF\w:.-]{0,99}[\u00C0-\u1FFF\u2C00-\uD7FF\w:-])?)/,onMatch: addHashtag,};diff --git a/frontend/discourse/app/lib/render-tag.js b/frontend/discourse/app/lib/render-tag.jsindex 0b8d6d935ef..d01c147a291 100644--- a/frontend/discourse/app/lib/render-tag.js+++ b/frontend/discourse/app/lib/render-tag.js@@ -18,6 +18,7 @@ export function defaultRenderTag(tag, params) {const tagName = typeof tag === "string" ? tag : tag.name;const visibleName = escapeExpression(tagName);const tagNameLower = visibleName.toLowerCase();+ const tagPathName = tagNameLower.replaceAll(".", "%2E");const classes = ["discourse-tag"];const htmlTag = params.tagName || "a";@@ -28,12 +29,12 @@ export function defaultRenderTag(tag, params) {const username = params.tagsForUser? params.tagsForUser: User.current().username;- path = `/u/${username}/messages/tags/${tagNameLower}`;+ path = `/u/${username}/messages/tags/${tagPathName}`;} else if (typeof tag === "object" && tag.id) {const slugForUrl = tag.slug || `${tag.id}-tag`;path = `/tag/${slugForUrl}/${tag.id}`;} else {- path = `/tag/${tagNameLower}`;+ path = `/tag/${tagPathName}`;}}const href = path ? ` href='${getURL(path)}' ` : "";diff --git a/frontend/discourse/app/lib/search.js b/frontend/discourse/app/lib/search.jsindex a15b5dbc2b1..86b96b237d1 100644--- a/frontend/discourse/app/lib/search.js+++ b/frontend/discourse/app/lib/search.js@@ -105,10 +105,11 @@ export function translateResults(results, opts) {.map(function (tag) {const id = tag.id;const name = escapeExpression(tag.name);+ const slug = tag.slug || `${id}-tag`;return EmberObject.create({id,name,- url: getURL("/tag/" + name),+ url: getURL(`/tag/${slug}/${id}`),});}).filter((item) => item != null);diff --git a/frontend/discourse/app/models/tag.js b/frontend/discourse/app/models/tag.jsindex d8cb0bc1398..2ea59bb2fdf 100644--- a/frontend/discourse/app/models/tag.js+++ b/frontend/discourse/app/models/tag.js@@ -15,7 +15,7 @@ export default class Tag extends RestModel {return getURL(`/tag/${slugForUrl}/${this.id}`);}// fallback for tags without id (legacy)- return getURL(`/tag/${this.name}`);+ return getURL(`/tag/${this.name.replaceAll(".", "%2E")}`);}@computed("count", "pm_count")diff --git a/frontend/discourse/app/static/prosemirror/extensions/hashtag.js b/frontend/discourse/app/static/prosemirror/extensions/hashtag.jsindex e2e28b464b3..96517d8e802 100644--- a/frontend/discourse/app/static/prosemirror/extensions/hashtag.js+++ b/frontend/discourse/app/static/prosemirror/extensions/hashtag.js@@ -47,7 +47,8 @@ const extension = {inputRules: [{- match: /(^|\W)(#[\u00C0-\u1FFF\u2C00-\uD7FF\w:-]{1,101})\s$/,+ match:+ /(^|\W)(#[\u00C0-\u1FFF\u2C00-\uD7FF\w:-](?:[\u00C0-\u1FFF\u2C00-\uD7FF\w:.-]{0,99}[\u00C0-\u1FFF\u2C00-\uD7FF\w:-])?)\s$/,handler: (state, match, start, end) => {const hashtagStart = start + match[1].length;const name = match[2].slice(1);diff --git a/frontend/discourse/select-kit/services/tag-utils.js b/frontend/discourse/select-kit/services/tag-utils.jsindex 8ce746709b8..9732379a4df 100644--- a/frontend/discourse/select-kit/services/tag-utils.js+++ b/frontend/discourse/select-kit/services/tag-utils.js@@ -73,9 +73,10 @@ export default class TagUtils extends Service {input = input.trim().replace(/\s+/g, "-")- .replace(/[\/\?#\[\]@!\$&'\(\)\*\+,;=\.%\\`^\s|\{\}"<>]+/g, "")+ .replace(/[\/\?#\[\]@!\$&'\(\)\*\+,;=%\\`^\s|\{\}"<>]+/g, "").replace(/-{2,}/g, "-")- .substring(0, this.siteSettings.max_tag_length);+ .substring(0, this.siteSettings.max_tag_length)+ .replace(/^\.+|\.+$/g, "");if (this.siteSettings.force_lowercase_tags) {input = input.toLowerCase();diff --git a/frontend/discourse/tests/acceptance/hashtag-autocomplete-test.js b/frontend/discourse/tests/acceptance/hashtag-autocomplete-test.jsindex bcabed879cb..96a44d1a832 100644--- a/frontend/discourse/tests/acceptance/hashtag-autocomplete-test.js+++ b/frontend/discourse/tests/acceptance/hashtag-autocomplete-test.js@@ -1,6 +1,6 @@import { click, visit } from "@ember/test-helpers";import { test } from "qunit";-import pretender from "discourse/tests/helpers/create-pretender";+import pretender, { response } from "discourse/tests/helpers/create-pretender";import {acceptance,simulateKeys,@@ -91,6 +91,37 @@ acceptance("#hashtag autocompletion in composer", function (needs) {.exists();});+ test("autocomplete supports periods in the middle of tag names", async function (assert) {+ const terms = [];++ pretender.get("/hashtags/search.json", (request) => {+ terms.push(request.queryParams.term);++ return response({+ results: [+ {+ id: 301,+ text: "sam.i.am x 1",+ slug: "sam.i.am",+ icon: "tag",+ relative_url: "/tag/sam-i-am/301",+ ref: "sam.i.am",+ type: "tag",+ style_type: "icon",+ },+ ],+ });+ });++ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await simulateKeys(".d-editor-input", "abc #sam.i");++ assert.true(terms.includes("sam.i"));+ assert.dom(".hashtag-autocomplete__option").exists({ count: 1 });+ assert.dom(".hashtag-autocomplete__option").hasText("sam.i.am x 1");+ });+test("iconHTML is correctly generated for various hashtag types and style_type configs", async function (assert) {await visit("/t/internationalization-localization/280");await click("#topic-footer-buttons .btn.create");diff --git a/frontend/discourse/tests/unit/lib/render-tag-test.js b/frontend/discourse/tests/unit/lib/render-tag-test.jsindex cf49447e507..4bce6a4327c 100644--- a/frontend/discourse/tests/unit/lib/render-tag-test.js+++ b/frontend/discourse/tests/unit/lib/render-tag-test.js@@ -21,6 +21,13 @@ module("Unit | Utility | render-tag", function (hooks) {);});+ test("defaultRenderTag encodes legacy tag links with periods", function (assert) {+ assert.strictEqual(+ renderTag("node.js"),+ "<a href='/tag/node%2Ejs' data-tag-name=node.js class='discourse-tag simple'>node.js</a>"+ );+ });+test("renderTag with extraClass", function (assert) {const result = renderTag("foo", {extraClass: "ins del",diff --git a/frontend/discourse/tests/unit/models/tag-test.js b/frontend/discourse/tests/unit/models/tag-test.jsindex 5f508f5f0e3..a3ade49aa1d 100644--- a/frontend/discourse/tests/unit/models/tag-test.js+++ b/frontend/discourse/tests/unit/models/tag-test.js@@ -19,6 +19,11 @@ module("Unit | Model | tag", function (hooks) {assert.strictEqual(tag.totalCount, 13);});+ test("url encodes legacy tag names with periods without ids", function (assert) {+ const tag = this.store.createRecord("tag", { name: "node.js" });+ assert.strictEqual(tag.url, "/tag/node%2Ejs");+ });+test("pmOnly", function (assert) {const tag = this.store.createRecord("tag", { pm_only: false });diff --git a/frontend/discourse/tests/unit/services/tag-utils-test.js b/frontend/discourse/tests/unit/services/tag-utils-test.jsindex 8eeca77ca03..b3ea05d05b6 100644--- a/frontend/discourse/tests/unit/services/tag-utils-test.js+++ b/frontend/discourse/tests/unit/services/tag-utils-test.js@@ -34,8 +34,19 @@ module("Unit | Service | tag-utils", function (hooks) {"has-spaces");assert.strictEqual(- this.tagUtils.createContentFromInput("special!@#chars"),+ this.tagUtils.createContentFromInput("special!@chars"),"specialchars");});++ test("createContentFromInput allows periods in the middle of tag names", function (assert) {+ assert.strictEqual(+ this.tagUtils.createContentFromInput("node.js"),+ "node.js"+ );+ assert.strictEqual(+ this.tagUtils.createContentFromInput(".node.js."),+ "node.js"+ );+ });});diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rbindex 559e3d27c56..11cb856ac9d 100644--- a/lib/discourse_tagging.rb+++ b/lib/discourse_tagging.rb@@ -2,7 +2,8 @@module DiscourseTaggingTAGS_FIELD_NAME = "tags"- TAGS_FILTER_REGEXP = /[\/\?#\[\]@!\$&'\(\)\*\+,;=\.%\\`^\s|\{\}"<>]+/ # /?#[]@!$&'()*+,;=.%\`^|{}"<>+ # Tag names can include periods, like node.js.+ TAGS_FILTER_REGEXP = /[\/\?#\[\]@!\$&'\(\)\*\+,;=%\\`^\s|\{\}"<>]+/ # /?#[]@!$&'()*+,;=%\`^|{}"<>TAGS_STAFF_CACHE_KEY = "staff_tag_names"TAG_GROUP_TAG_IDS_SQL = <<-SQL@@ -834,7 +835,8 @@ module DiscourseTaggingtag.gsub!(/[^[:word:][:punct:]]+/, "")tag.gsub!(TAGS_FILTER_REGEXP, "")tag.squeeze!("-")- truncate ? tag[0...SiteSetting.max_tag_length] : tag+ tag = truncate ? tag[0...SiteSetting.max_tag_length] : tag+ tag.gsub(/\A\.+|\.+\z/, "")enddef self.tag_ids_matching_localizations(term, term_type:)diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex 47a26ef1f78..f39c7e8ce87 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -937,7 +937,7 @@ class TopicsFilterbreak if key_prefix && key_prefix != "-"value.scan(- /\A(?<tag_names>([\p{N}\p{L}\-_]+)(?<delimiter>[,+])?([\p{N}\p{L}\-_]+)?(\k<delimiter>[\p{N}\p{L}\-_]+)*)\z/,+ /\A(?<tag_names>([\p{N}\p{L}\-_.]+)(?<delimiter>[,+])?([\p{N}\p{L}\-_.]+)?(\k<delimiter>[\p{N}\p{L}\-_.]+)*)\z/,) do |tag_names, delimiter|match_all =if delimiter == ","diff --git a/spec/lib/discourse_tagging_spec.rb b/spec/lib/discourse_tagging_spec.rbindex 8b7cf0cde1d..a3fb4444317 100644--- a/spec/lib/discourse_tagging_spec.rb+++ b/spec/lib/discourse_tagging_spec.rb@@ -1851,7 +1851,7 @@ RSpec.describe DiscourseTagging doTag.new(name: "math=fun").save(validate: false)expect(described_class.tags_for_saving(%w[math=fun fun*[email protected]], guardian).try(:sort),- ).to eq(%w[math=fun fun2gmailcom].sort)+ ).to eq(%w[math=fun fun2gmail.com].sort)endend@@ -1867,6 +1867,11 @@ RSpec.describe DiscourseTagging doexpect(DiscourseTagging.clean_tag("hel\ufefflo")).to eq("hello")end+ it "allows periods in the middle of tag names" do+ expect(DiscourseTagging.clean_tag("node.js")).to eq("node.js")+ expect(DiscourseTagging.clean_tag(".node.js.")).to eq("node.js")+ end+it "removes multiple consecutive dashes" doexpect(DiscourseTagging.clean_tag("hello---world")).to eq("hello-world")expect(DiscourseTagging.clean_tag("Finances & Accounting")).to eq("finances-accounting")diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rbindex 72580a0a5f6..2b239e4265b 100644--- a/spec/lib/pretty_text_spec.rb+++ b/spec/lib/pretty_text_spec.rb@@ -1927,6 +1927,37 @@ RSpec.describe PrettyText dowith_tag("span", with: { class: "hashtag-icon-placeholder" })end+ tag_with_periods = Fabricate(:tag, name: "sam.i.am")+ Fabricate(:topic, tags: [tag_with_periods])+ cooked = PrettyText.cook(" #sam.i.am", user_id: user.id)+ expect(cooked).to have_tag(+ "a",+ with: {+ class: "hashtag-cooked",+ href: tag_with_periods.url,+ "data-type": "tag",+ "data-slug": tag_with_periods.name,+ "data-id": tag_with_periods.id,+ },+ ) do+ with_tag("span", with: { class: "hashtag-icon-placeholder" })+ end++ cooked = PrettyText.cook(" #known::tag.", user_id: user.id)+ expect(cooked).to include("</a>.")+ expect(cooked).to have_tag(+ "a",+ with: {+ class: "hashtag-cooked",+ href: tag.url,+ "data-type": "tag",+ "data-slug": tag.name,+ "data-id": tag.id,+ },+ ) do+ with_tag("span", with: { class: "hashtag-icon-placeholder" })+ end+cooked = PrettyText.cook("[`a` #known::tag here](http://example.com)", user_id: user.id)html = <<~HTMLdiff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex a3d10215acd..bd1e6509bb2 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -1303,6 +1303,18 @@ RSpec.describe TopicsFilter do).to contain_exactly(topic_with_tag_and_tag2.id)end+ it "should return topics tagged with period-delimited tag names" do+ tag_with_period = Fabricate(:tag, name: "node.js")+ topic_with_period_tag = Fabricate(:topic, tags: [tag_with_period])++ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("tags:#{tag_with_period.name}")+ .pluck(:id),+ ).to contain_exactly(topic_with_period_tag.id)+ end+it "should only return topics that are tagged with tag1 and tag2 when query string is `tags:tag1 tags:tag2`" dotopic_with_tag_and_tag2_and_tag3 = Fabricate(:topic, tags: [tag, tag2, tag3])diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rbindex ba83fe755b4..cb712f59f09 100644--- a/spec/requests/tags_controller_spec.rb+++ b/spec/requests/tags_controller_spec.rb@@ -425,6 +425,46 @@ RSpec.describe TagsController doexpect(topic_list["tags"].map { |t| t["id"] }).to contain_exactly(tag.id)end+ it "shows tags with periods by canonical id URL and encoded legacy URL" do+ node = Fabricate(:tag, name: "node.js")+ node_topic = Fabricate(:topic, tags: [node])++ get "/tag/#{node.slug}/#{node.id}.json"+ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(+ node_topic.id,+ )++ get "/tag/node%2Ejs.json"+ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(+ node_topic.id,+ )+ end++ it "shows tag intersections with encoded period tag names" do+ node = Fabricate(:tag, name: "node.js")+ other = Fabricate(:tag, name: "other")+ matching_topic = Fabricate(:topic, tags: [node, other])+ Fabricate(:topic, tags: [node])++ get "/tags/intersection/node%2Ejs/other.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(+ matching_topic.id,+ )+ end++ it "returns tag info for an encoded period tag name" do+ Fabricate(:tag, name: "node.js")++ get "/tag/node%2Ejs/info.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["tag_info"]["name"]).to eq("node.js")+ end+it "should handle invalid tags" doget "/tag/%2ftest%2f"expect(response.status).to eq(404)diff --git a/spec/system/tags/tags_spec.rb b/spec/system/tags/tags_spec.rbindex 69cf0019e5f..7ced1b44683 100644--- a/spec/system/tags/tags_spec.rb+++ b/spec/system/tags/tags_spec.rb@@ -407,4 +407,48 @@ describe "Tags" doexpect(composer_tag_chooser).to have_option_name("cap-open")endend++ describe "tag names with periods" do+ let(:discovery) { PageObjects::Pages::Discovery.new }+ let(:topic_page) { PageObjects::Pages::Topic.new }++ fab!(:node_tag) { Fabricate(:tag, name: "node.js") }+ fab!(:node_topic) do+ Fabricate(:topic, title: "Tagged with node.js example", tags: [node_tag]).tap do |t|+ Fabricate(:post, topic: t)+ end+ end++ before { SearchIndexer.enable }+ after { SearchIndexer.disable }++ it "renders the period verbatim through list, topic, tag page, and search" do+ SearchIndexer.update_tags_index(node_tag.id, node_tag.name)++ sign_in(user_tl1)++ visit "/latest"+ expect(discovery.topic_list).to have_topic_tag(node_topic, "node.js")++ discovery.topic_list.click_topic_title(node_topic)+ expect(topic_page).to have_topic_title("Tagged with node.js example")+ expect(topic_page.topic_tags).to include("node.js")++ find(".title-wrapper .discourse-tag", text: "node.js").click++ expect(page).to have_current_path("/tag/#{node_tag.slug}/#{node_tag.id}")+ expect(discovery.topic_list).to have_topic(node_topic)++ visit "/search?q=node&search_type=categories_tags"++ expect(page).to have_selector(+ ".fps-tag-item a[href=\"/tag/#{node_tag.slug}/#{node_tag.id}\"]",+ text: "node.js",+ )++ find(".fps-tag-item a", text: "node.js").click++ expect(page).to have_current_path("/tag/#{node_tag.slug}/#{node_tag.id}")+ end+ endend