mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Fix race condition when resolving tag and category hashtags (#10153)
* FIX: Fix race condition when resolving tag and category hashtags If the category hashtags were resolved first and then tag hashtags, then the tags would overwrite the categories. Similarly, if the category hashtags were resolved last it would overwrite even hashtags which ended with '::tag'. * DEV: Add test * DEV: Fix test
This commit is contained in:
@@ -5,9 +5,10 @@ import {
|
||||
inCodeBlock
|
||||
} from "discourse/lib/utilities";
|
||||
|
||||
export function replaceSpan($elem, categorySlug, categoryLink) {
|
||||
export function replaceSpan($elem, categorySlug, categoryLink, type) {
|
||||
type = type ? ` data-type="${type}"` : "";
|
||||
$elem.replaceWith(
|
||||
`<a href="${categoryLink}" class="hashtag">#<span>${categorySlug}</span></a>`
|
||||
`<a href="${categoryLink}" class="hashtag"${type}>#<span>${categorySlug}</span></a>`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,7 @@ import { replaceSpan } from "discourse/lib/category-hashtags";
|
||||
|
||||
const validCategoryHashtags = {};
|
||||
const checkedCategoryHashtags = [];
|
||||
const testedKey = "tested";
|
||||
const testedClass = `hashtag-${testedKey}`;
|
||||
const testedClass = `hashtag-category-tested`;
|
||||
|
||||
function updateFound($hashtags, categorySlugs) {
|
||||
schedule("afterRender", () => {
|
||||
@@ -15,16 +14,20 @@ function updateFound($hashtags, categorySlugs) {
|
||||
const $hashtag = $(hashtag);
|
||||
|
||||
if (link) {
|
||||
replaceSpan($hashtag, categorySlug, link);
|
||||
if ($hashtag.data("type") !== "tag") {
|
||||
replaceSpan($hashtag, categorySlug, link, "category");
|
||||
}
|
||||
} else if (checkedCategoryHashtags.indexOf(categorySlug) !== -1) {
|
||||
$hashtag.addClass(testedClass);
|
||||
if (hashtag.tagName !== "A") {
|
||||
$hashtag.addClass(testedClass);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function linkSeenCategoryHashtags($elem) {
|
||||
const $hashtags = $(`span.hashtag:not(.${testedClass})`, $elem);
|
||||
const $hashtags = $(`span.hashtag:not(.${testedClass}), a.hashtag`, $elem);
|
||||
const unseen = [];
|
||||
|
||||
if ($hashtags.length) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { TAG_HASHTAG_POSTFIX } from "discourse/lib/tag-hashtags";
|
||||
|
||||
const validTagHashtags = {};
|
||||
const checkedTagHashtags = [];
|
||||
const testedClass = "tag-hashtag-tested";
|
||||
const testedClass = "hashtag-tag-tested";
|
||||
|
||||
function updateFound($hashtags, tagValues) {
|
||||
schedule("afterRender", () => {
|
||||
@@ -15,7 +15,9 @@ function updateFound($hashtags, tagValues) {
|
||||
const $hashtag = $(hashtag);
|
||||
|
||||
if (link) {
|
||||
replaceSpan($hashtag, tagValue, link);
|
||||
if (!$hashtag.data("type") || $hashtag.data("type") === "tag") {
|
||||
replaceSpan($hashtag, tagValue, link, $hashtag.data("type"));
|
||||
}
|
||||
} else if (checkedTagHashtags.indexOf(tagValue) !== -1) {
|
||||
$hashtag.addClass(testedClass);
|
||||
}
|
||||
@@ -29,10 +31,12 @@ export function linkSeenTagHashtags($elem) {
|
||||
|
||||
if ($hashtags.length) {
|
||||
const tagValues = $hashtags.map((_, hashtag) => {
|
||||
return $(hashtag)
|
||||
.text()
|
||||
.substr(1)
|
||||
.replace(TAG_HASHTAG_POSTFIX, "");
|
||||
let text = $(hashtag).text();
|
||||
if (text.endsWith(TAG_HASHTAG_POSTFIX)) {
|
||||
text = text.slice(0, -TAG_HASHTAG_POSTFIX.length);
|
||||
$(hashtag).data("type", "tag");
|
||||
}
|
||||
return text.substr(1);
|
||||
});
|
||||
|
||||
if (tagValues.length) {
|
||||
|
||||
Reference in New Issue
Block a user