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
Sam 234694b50f Feature: CommonMark support
This adds the markdown.it engine to Discourse.
https://github.com/markdown-it/markdown-it

As the migration is going to take a while the new engine is default
disabled. To enable it you must change the hidden site setting:
enable_experimental_markdown_it.

This commit is a squash of many other commits, it also includes some
improvements to autospec (ability to run plugins), and a dev dependency
on the og gem for html normalization.
2017-06-23 12:01:33 -04:00

58 lines
1.7 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';
function getOpts(opts) {
const siteSettings = Discourse.__container__.lookup('site-settings:main');
opts = _.merge({
getURL: Discourse.getURLWithCDN,
currentUser: Discourse.__container__.lookup('current-user:main'),
siteSettings
}, 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(new PrettyText(getOpts(options)).cook(text));
}
// everything should eventually move to async API and this should be renamed
// cook
export function cookAsync(text, options) {
if (Discourse.MarkdownItURL) {
return loadScript(Discourse.MarkdownItURL)
.then(()=>cook(text, options));
} else {
return Ember.RSVP.Promise.resolve(cook(text));
}
}
export function sanitize(text) {
return textSanitize(text, new WhiteLister(getOpts()));
}
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);
}
}