diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index 44f0130acbb..d661b26a219 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -1,10 +1,7 @@ /*global Mousetrap:true */ import { default as computed, on, observes } from 'ember-addons/ember-computed-decorators'; -import Category from 'discourse/models/category'; import { categoryHashtagTriggerRule } from 'discourse/lib/category-hashtags'; -import { TAG_HASHTAG_POSTFIX } from 'discourse/lib/tag-hashtags'; import { search as searchCategoryTag } from 'discourse/lib/category-tag-search'; -import { SEPARATOR } from 'discourse/lib/category-hashtags'; import { cookAsync } from 'discourse/lib/text'; import { translations } from 'pretty-text/emoji/data'; import { emojiSearch, isSkinTonableEmoji } from 'pretty-text/emoji'; @@ -322,11 +319,7 @@ export default Ember.Component.extend({ template: findRawTemplate('category-tag-autocomplete'), key: '#', transformComplete(obj) { - if (obj.model) { - return Category.slugFor(obj.model, SEPARATOR); - } else { - return `${obj.text}${TAG_HASHTAG_POSTFIX}`; - } + return obj.text; }, dataSource(term) { return searchCategoryTag(term, siteSettings); diff --git a/app/assets/javascripts/discourse/lib/category-tag-search.js.es6 b/app/assets/javascripts/discourse/lib/category-tag-search.js.es6 index d7895ff47a1..359e7660417 100644 --- a/app/assets/javascripts/discourse/lib/category-tag-search.js.es6 +++ b/app/assets/javascripts/discourse/lib/category-tag-search.js.es6 @@ -1,5 +1,7 @@ import { CANCELLED_STATUS } from 'discourse/lib/autocomplete'; import Category from 'discourse/models/category'; +import { TAG_HASHTAG_POSTFIX } from 'discourse/lib/tag-hashtags'; +import { SEPARATOR } from 'discourse/lib/category-hashtags'; var cache = {}; var cacheTime; @@ -27,7 +29,18 @@ function searchTags(term, categories, limit) { var returnVal = CANCELLED_STATUS; oldSearch.then((r) => { - var tags = r.results.map((tag) => { return { text: tag.text, count: tag.count }; }); + const categoryNames = cats.map(c => c.model.get('name')); + + const tags = r.results.map((tag) => { + const tagName = tag.text; + + return { + name: tagName, + text: (categoryNames.includes(tagName) ? `${tagName}${TAG_HASHTAG_POSTFIX}` : tagName), + count: tag.count, + }; + }); + returnVal = cats.concat(tags); }).always(() => { oldSearch = null; @@ -55,7 +68,10 @@ export function search(term, siteSettings) { const limit = 5; var categories = Category.search(term, { limit }); var numOfCategories = categories.length; - categories = categories.map((category) => { return { model: category }; }); + + categories = categories.map((category) => { + return { model: category, text: Category.slugFor(category, SEPARATOR) }; + }); if (numOfCategories !== limit && siteSettings.tagging_enabled) { return searchTags(term, categories, limit - numOfCategories); diff --git a/app/assets/javascripts/discourse/lib/search.js.es6 b/app/assets/javascripts/discourse/lib/search.js.es6 index 8939c173299..266f374a65d 100644 --- a/app/assets/javascripts/discourse/lib/search.js.es6 +++ b/app/assets/javascripts/discourse/lib/search.js.es6 @@ -1,7 +1,5 @@ import { ajax } from 'discourse/lib/ajax'; import { findRawTemplate } from 'discourse/lib/raw-templates'; -import { TAG_HASHTAG_POSTFIX } from 'discourse/lib/tag-hashtags'; -import { SEPARATOR } from 'discourse/lib/category-hashtags'; import Category from 'discourse/models/category'; import { search as searchCategoryTag } from 'discourse/lib/category-tag-search'; import userSearch from 'discourse/lib/user-search'; @@ -148,11 +146,7 @@ export function applySearchAutocomplete($input, siteSettings, appEvents, options width: '100%', treatAsTextarea: true, transformComplete(obj) { - if (obj.model) { - return Category.slugFor(obj.model, SEPARATOR); - } else { - return `${obj.text}${TAG_HASHTAG_POSTFIX}`; - } + return obj.text; }, dataSource(term) { return searchCategoryTag(term, siteSettings); diff --git a/app/assets/javascripts/discourse/templates/category-tag-autocomplete.raw.hbs b/app/assets/javascripts/discourse/templates/category-tag-autocomplete.raw.hbs index 1c429ddb39d..3f4359aab56 100644 --- a/app/assets/javascripts/discourse/templates/category-tag-autocomplete.raw.hbs +++ b/app/assets/javascripts/discourse/templates/category-tag-autocomplete.raw.hbs @@ -5,7 +5,7 @@ {{#if option.model}} {{category-link option.model allowUncategorized="true" link="false"}} {{else}} - {{d-icon 'tag'}}{{option.text}} x {{option.count}} + {{d-icon 'tag'}}{{option.name}} x {{option.count}} {{/if}} {{/each}} diff --git a/lib/pretty_text/helpers.rb b/lib/pretty_text/helpers.rb index fcf66188f05..22383b3cec6 100644 --- a/lib/pretty_text/helpers.rb +++ b/lib/pretty_text/helpers.rb @@ -91,7 +91,8 @@ module PrettyText if !is_tag && category = Category.query_from_hashtag_slug(text) [category.url_with_id, text] - elsif is_tag && tag = Tag.find_by_name(text.gsub!("#{tag_postfix}", '')) + elsif (!is_tag && tag = Tag.find_by(name: text)) || + (is_tag && tag = Tag.find_by(name: text.gsub!("#{tag_postfix}", ''))) ["#{Discourse.base_url}/tags/#{tag.name}", text] else nil diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 0cffc4862b5..ec62b0aee21 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -717,16 +717,22 @@ describe PrettyText do expect(cooked).to eq(n expected) end - it "produces tag links" do + it "produces hashtag links" do + category = Fabricate(:category, name: 'testing') + category2 = Fabricate(:category, name: 'known') Fabricate(:topic, tags: [Fabricate(:tag, name: 'known')]) - cooked = PrettyText.cook(" #unknown::tag #known::tag") + cooked = PrettyText.cook(" #unknown::tag #known #known::tag #testing") - html = <<~HTML -

#unknown::tag #known

- HTML + [ + "#unknown::tag", + "#known", + "#known", + "#testing" + ].each do |element| - expect(cooked).to eq(html.strip) + expect(cooked).to include(element) + end cooked = PrettyText.cook("[`a` #known::tag here](http://somesite.com)")