2018-11-22 01:28:48 -05:00
|
|
|
__PrettyText = require("pretty-text/pretty-text").default;
|
|
|
|
__buildOptions = require("pretty-text/pretty-text").buildOptions;
|
|
|
|
__performEmojiUnescape = require("pretty-text/emoji").performEmojiUnescape;
|
2021-04-22 02:43:06 -04:00
|
|
|
__emojiReplacementRegex = require("pretty-text/emoji").emojiReplacementRegex;
|
2019-03-21 04:11:33 -04:00
|
|
|
__performEmojiEscape = require("pretty-text/emoji").performEmojiEscape;
|
2022-07-06 04:37:54 -04:00
|
|
|
__resetTranslationTree =
|
|
|
|
require("pretty-text/engines/discourse-markdown/emoji").resetTranslationTree;
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2020-05-13 16:23:41 -04:00
|
|
|
I18n = {
|
|
|
|
t(a, b) {
|
|
|
|
return __helpers.t(a, b);
|
2020-09-15 10:59:41 -04:00
|
|
|
},
|
2020-05-13 16:23:41 -04:00
|
|
|
};
|
|
|
|
|
2020-09-15 10:59:41 -04:00
|
|
|
define("I18n", ["exports"], function (exports) {
|
2020-05-13 16:23:41 -04:00
|
|
|
exports.default = I18n;
|
|
|
|
});
|
|
|
|
|
2020-09-15 10:59:41 -04:00
|
|
|
define("discourse-common/lib/helpers", ["exports"], function (exports) {
|
|
|
|
exports.helperContext = function () {
|
2023-05-31 20:00:01 -04:00
|
|
|
return {
|
|
|
|
siteSettings: { avatar_sizes: __optInput.avatar_sizes },
|
|
|
|
};
|
2020-08-19 16:11:36 -04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-06-28 13:47:22 -04:00
|
|
|
__emojiUnicodeReplacer = null;
|
|
|
|
|
2020-09-15 10:59:41 -04:00
|
|
|
__setUnicode = function (replacements) {
|
2021-04-22 02:43:06 -04:00
|
|
|
const regexp = new RegExp(__emojiReplacementRegex, "g");
|
2017-06-28 13:47:22 -04:00
|
|
|
|
2020-09-15 10:59:41 -04:00
|
|
|
__emojiUnicodeReplacer = function (text) {
|
2021-04-22 02:43:06 -04:00
|
|
|
regexp.lastIndex = 0;
|
|
|
|
|
2017-06-28 13:47:22 -04:00
|
|
|
let m;
|
2021-04-22 02:43:06 -04:00
|
|
|
while ((m = regexp.exec(text)) !== null) {
|
|
|
|
let match = m[0];
|
|
|
|
|
|
|
|
let replacement = replacements[match];
|
|
|
|
|
|
|
|
if (!replacement) {
|
|
|
|
// if we can't find replacement for an emoji match
|
|
|
|
// attempts to look for the same without trailing variation selector
|
|
|
|
match = match.replace(/\ufe0f$/g, "");
|
|
|
|
replacement = replacements[match];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!replacement) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
replacement = ":" + replacement + ":";
|
2018-11-22 01:28:48 -05:00
|
|
|
const before = text.charAt(m.index - 1);
|
2017-06-28 13:47:22 -04:00
|
|
|
if (!/\B/.test(before)) {
|
|
|
|
replacement = "\u200b" + replacement;
|
|
|
|
}
|
2021-04-22 02:43:06 -04:00
|
|
|
text = text.replace(match, replacement);
|
2017-06-28 13:47:22 -04:00
|
|
|
}
|
2018-10-07 22:32:25 -04:00
|
|
|
|
|
|
|
// fixes Safari VARIATION SELECTOR-16 issue with some emojis
|
|
|
|
// https://meta.discourse.org/t/emojis-selected-on-ios-displaying-additional-rectangles/86132
|
2018-11-22 01:28:48 -05:00
|
|
|
text = text.replace(/\ufe0f/g, "");
|
2018-10-07 22:32:25 -04:00
|
|
|
|
2017-06-28 13:47:22 -04:00
|
|
|
return text;
|
|
|
|
};
|
|
|
|
};
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
__paths = {};
|
|
|
|
|
|
|
|
function __getURLNoCDN(url) {
|
2021-04-22 02:43:06 -04:00
|
|
|
if (!url) {
|
|
|
|
return url;
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
// if it's a non relative URL, return it.
|
2018-11-22 01:28:48 -05:00
|
|
|
if (url !== "/" && !/^\/[^\/]/.test(url)) {
|
|
|
|
return url;
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2023-03-30 08:35:06 -04:00
|
|
|
if (url.startsWith(`${__paths.baseUri}/`) || url === __paths.baseUri) {
|
2018-11-22 01:28:48 -05:00
|
|
|
return url;
|
|
|
|
}
|
2021-04-22 02:43:06 -04:00
|
|
|
if (url[0] !== "/") {
|
|
|
|
url = "/" + url;
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
return __paths.baseUri + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function __getURL(url) {
|
|
|
|
url = __getURLNoCDN(url);
|
|
|
|
// only relative urls
|
|
|
|
if (__paths.CDN && /^\\\/[^\\\/]/.test(url)) {
|
|
|
|
url = __paths.CDN + url;
|
|
|
|
} else if (__paths.S3CDN) {
|
|
|
|
url = url.replace(__paths.S3BaseUrl, __paths.S3CDN);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2019-05-28 21:00:25 -04:00
|
|
|
function __lookupUploadUrls(urls) {
|
|
|
|
return __helpers.lookup_upload_urls(urls);
|
2017-08-22 11:46:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
function __getTopicInfo(i) {
|
|
|
|
return __helpers.get_topic_info(i);
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:34:25 -05:00
|
|
|
function __hashtagLookup(slug, cookingUserId, typesInPriorityOrder) {
|
|
|
|
return __helpers.hashtag_lookup(slug, cookingUserId, typesInPriorityOrder);
|
FEATURE: Generic hashtag autocomplete lookup and markdown cooking (#18937)
This commit fleshes out and adds functionality for the new `#hashtag` search and
lookup system, still hidden behind the `enable_experimental_hashtag_autocomplete`
feature flag.
**Serverside**
We have two plugin API registration methods that are used to define data sources
(`register_hashtag_data_source`) and hashtag result type priorities depending on
the context (`register_hashtag_type_in_context`). Reading the comments in plugin.rb
should make it clear what these are doing. Reading the `HashtagAutocompleteService`
in full will likely help a lot as well.
Each data source is responsible for providing its own **lookup** and **search**
method that returns hashtag results based on the arguments provided. For example,
the category hashtag data source has to take into account parent categories and
how they relate, and each data source has to define their own icon to use for the
hashtag, and so on.
The `Site` serializer has two new attributes that source data from `HashtagAutocompleteService`.
There is `hashtag_icons` that is just a simple array of all the different icons that
can be used for allowlisting in our markdown pipeline, and there is `hashtag_context_configurations`
that is used to store the type priority orders for each registered context.
When sending emails, we cannot render the SVG icons for hashtags, so
we need to change the HTML hashtags to the normal `#hashtag` text.
**Markdown**
The `hashtag-autocomplete.js` file is where I have added the new `hashtag-autocomplete`
markdown rule, and like all of our rules this is used to cook the raw text on both the clientside
and on the serverside using MiniRacer. Only on the server side do we actually reach out to
the database with the `hashtagLookup` function, on the clientside we just render a plainer
version of the hashtag HTML. Only in the composer preview do we do further lookups based
on this.
This rule is the first one (that I can find) that uses the `currentUser` based on a passed
in `user_id` for guardian checks in markdown rendering code. This is the `last_editor_id`
for both the post and chat message. In some cases we need to cook without a user present,
so the `Discourse.system_user` is used in this case.
**Chat Channels**
This also contains the changes required for chat so that chat channels can be used
as a data source for hashtag searches and lookups. This data source will only be
used when `enable_experimental_hashtag_autocomplete` is `true`, so we don't have
to worry about channel results suddenly turning up.
------
**Known Rough Edges**
- Onebox excerpts will not render the icon svg/use tags, I plan to address that in a follow up PR
- Selecting a hashtag + pressing the Quote button will result in weird behaviour, I plan to address that in a follow up PR
- Mixed hashtag contexts for hashtags without a type suffix will not work correctly, e.g. #ux which is both a category and a channel slug will resolve to a category when used inside a post or within a [chat] transcript in that post. Users can get around this manually by adding the correct suffix, for example ::channel. We may get to this at some point in future
- Icons will not show for the hashtags in emails since SVG support is so terrible in email (this is not likely to be resolved, but still noting for posterity)
- Additional refinements and review fixes wil
2022-11-20 17:37:06 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
function __lookupAvatar(p) {
|
2023-07-12 04:06:16 -04:00
|
|
|
return require("discourse-common/lib/avatar-utils").avatarImg(
|
2018-11-22 01:28:48 -05:00
|
|
|
{ size: "tiny", avatarTemplate: __helpers.avatar_template(p) },
|
|
|
|
__getURL
|
|
|
|
);
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
2018-03-13 13:07:51 -04:00
|
|
|
function __formatUsername(username) {
|
|
|
|
return __helpers.format_username(username);
|
2017-11-20 16:28:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-13 13:07:51 -04:00
|
|
|
function __lookupPrimaryUserGroup(username) {
|
|
|
|
return __helpers.lookup_primary_user_group(username);
|
2017-11-03 09:51:40 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 03:52:56 -04:00
|
|
|
function __getCurrentUser(userId) {
|
|
|
|
return __helpers.get_current_user(userId);
|
|
|
|
}
|