discourse/lib/pretty_text/shims.js
Godfrey Chan 9a1695ccc1
DEV: remove markdown-it-bundle and custom build code (#23859)
With Embroider, we can rely on async `import()` to do the splitting
for us.

This commit extracts from `pretty-text` all the parts that are
meant to be loaded async into a new `discourse-markdown-it` package
that is also a V2 addon (meaning that all files are presumed unused
until they are imported, aka "static").

Mostly I tried to keep the very discourse specific stuff (accessing
site settings and loading plugin features) inside discourse proper,
while the new package aims to have some resembalance of a general
purpose library, a MarkdownIt++ if you will. It is far from perfect
because of how all the "options" stuff work but I think it's a good
start for more refactorings (clearing up the interfaces) to happen
later.

With this, pretty-text and app/lib/text are mostly a kitchen sink
of loosely related text processing utilities.

After the refactor, a lot more code related to setting up the
engine are now loaded lazily, which should be a pretty nice win. I
also noticed that we are currently pulling in the `xss` library at
initial load to power the "sanitize" stuff, but I suspect with a
similar refactoring effort those usages can be removed too. (See
also #23790).

This PR does not attempt to fix the sanitize issue, but I think it
sets things up on the right trajectory for that to happen later.

Co-authored-by: David Taylor <david@taylorhq.com>
2023-11-06 16:59:49 +00:00

143 lines
3.6 KiB
JavaScript

I18n = {
t(a, b) {
return __helpers.t(a, b);
},
};
define("I18n", ["exports"], function (exports) {
exports.default = I18n;
});
define("discourse-i18n", ["exports"], function (exports) {
exports.default = I18n;
});
define("discourse-common/lib/helpers", ["exports"], function (exports) {
exports.helperContext = function () {
return {
siteSettings: { avatar_sizes: __optInput.avatar_sizes },
};
};
});
define("pretty-text/engines/discourse-markdown/bbcode-block", [
"exports",
"discourse-markdown-it/features/bbcode-block",
], function (exports, { parseBBCodeTag }) {
exports.parseBBCodeTag = parseBBCodeTag;
});
__emojiUnicodeReplacer = null;
__setUnicode = function (replacements) {
const regexp = new RegExp(__emojiReplacementRegex, "g");
__emojiUnicodeReplacer = function (text) {
regexp.lastIndex = 0;
let m;
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 + ":";
const before = text.charAt(m.index - 1);
if (!/\B/.test(before)) {
replacement = "\u200b" + replacement;
}
text = text.replace(match, replacement);
}
// fixes Safari VARIATION SELECTOR-16 issue with some emojis
// https://meta.discourse.org/t/emojis-selected-on-ios-displaying-additional-rectangles/86132
text = text.replace(/\ufe0f/g, "");
return text;
};
};
__paths = {};
function __getURLNoCDN(url) {
if (!url) {
return url;
}
// if it's a non relative URL, return it.
if (url !== "/" && !/^\/[^\/]/.test(url)) {
return url;
}
if (url.startsWith(`${__paths.baseUri}/`) || url === __paths.baseUri) {
return url;
}
if (url[0] !== "/") {
url = "/" + url;
}
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;
}
function __lookupUploadUrls(urls) {
return __helpers.lookup_upload_urls(urls);
}
function __getTopicInfo(i) {
return __helpers.get_topic_info(i);
}
function __hashtagLookup(slug, cookingUserId, typesInPriorityOrder) {
return __helpers.hashtag_lookup(slug, cookingUserId, typesInPriorityOrder);
}
function __lookupAvatar(p) {
return require("discourse-common/lib/avatar-utils").avatarImg(
{ size: "tiny", avatarTemplate: __helpers.avatar_template(p) },
__getURL
);
}
function __formatUsername(username) {
return __helpers.format_username(username);
}
function __lookupPrimaryUserGroup(username) {
return __helpers.lookup_primary_user_group(username);
}
function __getCurrentUser(userId) {
return __helpers.get_current_user(userId);
}
__DiscourseMarkdownIt = require("discourse-markdown-it").default;
__buildOptions = require("discourse-markdown-it/options").default;
__performEmojiUnescape = require("pretty-text/emoji").performEmojiUnescape;
__emojiReplacementRegex = require("pretty-text/emoji").emojiReplacementRegex;
__performEmojiEscape = require("pretty-text/emoji").performEmojiEscape;
__resetTranslationTree =
require("discourse-markdown-it/features/emoji").resetTranslationTree;
__loadPluginFeatures = require("discourse/static/markdown-it/features").default;