This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/text.js.es6
Joffrey JAFFEUX f9648de897
DEV: upgrades from Ember 2.13 to Ember 3.5.1 (#6808)
Co-Authored-By: Bianca Nenciu <nbianca@users.noreply.github.com>
Co-Authored-By: David Taylor <david@taylorhq.com>
2019-01-10 11:06:01 +01:00

82 lines
2.3 KiB
JavaScript

import { default as PrettyText, buildOptions } from "pretty-text/pretty-text";
import { performEmojiUnescape, buildEmojiUrl } from "pretty-text/emoji";
import WhiteLister from "pretty-text/white-lister";
import { sanitize as textSanitize } from "pretty-text/sanitizer";
import loadScript from "discourse/lib/load-script";
import { formatUsername } from "discourse/lib/utilities";
function getOpts(opts) {
const siteSettings = Discourse.__container__.lookup("site-settings:main"),
site = Discourse.__container__.lookup("site:main");
opts = _.merge(
{
getURL: Discourse.getURLWithCDN,
currentUser: Discourse.__container__.lookup("current-user:main"),
censoredWords: site.censored_words,
siteSettings,
formatUsername
},
opts
);
return buildOptions(opts);
}
// Use this to easily create a pretty text instance with proper options
export function cook(text, options) {
return new Handlebars.SafeString(createPrettyText(options).cook(text));
}
// everything should eventually move to async API and this should be renamed
// cook
export function cookAsync(text, options) {
return loadMarkdownIt().then(() => cook(text, options));
}
export function sanitize(text, options) {
return textSanitize(text, new WhiteLister(options));
}
export function sanitizeAsync(text, options) {
return new loadMarkdownIt().then(() => {
return createPrettyText(options).sanitize(text);
});
}
function loadMarkdownIt() {
if (Discourse.MarkdownItURL) {
return loadScript(Discourse.MarkdownItURL).catch(e => {
// eslint-disable-next-line no-console
console.error(e);
});
} else {
return Ember.RSVP.Promise.resolve();
}
}
function createPrettyText(options) {
return new PrettyText(getOpts(options));
}
function emojiOptions() {
const siteSettings = Discourse.__container__.lookup("site-settings:main");
if (!siteSettings.enable_emoji) {
return;
}
return { getURL: Discourse.getURLWithCDN, emojiSet: siteSettings.emoji_set };
}
export function emojiUnescape(string, options) {
const opts = _.extend(emojiOptions(), options || {});
return opts ? performEmojiUnescape(string, opts) : string;
}
export function emojiUrlFor(code) {
const opts = emojiOptions();
if (opts) {
return buildEmojiUrl(code, opts);
}
}