2021-03-28 11:06:49 +02:00
|
|
|
import loadScript from "./load-script";
|
2021-05-04 23:06:01 +02:00
|
|
|
import { default as PrettyText, buildOptions } from "pretty-text/pretty-text";
|
2021-04-12 08:26:22 +02:00
|
|
|
import Handlebars from "handlebars";
|
2021-05-04 23:06:01 +02:00
|
|
|
import getURL from "discourse-common/lib/get-url";
|
|
|
|
import { getOwner } from "discourse-common/lib/get-owner";
|
2022-03-16 14:09:23 +01:00
|
|
|
import { Promise } from "rsvp";
|
|
|
|
import Session from "discourse/models/session";
|
2018-02-08 05:30:55 +01:00
|
|
|
|
|
|
|
export function cook(text, options) {
|
2021-05-04 23:06:01 +02:00
|
|
|
if (!options) {
|
|
|
|
options = buildOptions({
|
2021-10-28 03:55:50 +02:00
|
|
|
getURL,
|
2021-05-04 23:06:01 +02:00
|
|
|
siteSettings: getOwner(this).lookup("site-settings:main"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-08 05:30:55 +01:00
|
|
|
return new Handlebars.SafeString(new PrettyText(options).cook(text));
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything should eventually move to async API and this should be renamed
|
|
|
|
// cook
|
|
|
|
export function cookAsync(text, options) {
|
2022-03-16 14:09:23 +01:00
|
|
|
let markdownItURL = Session.currentProp("markdownItURL");
|
|
|
|
if (markdownItURL) {
|
|
|
|
return (
|
|
|
|
loadScript(markdownItURL)
|
|
|
|
.then(() => cook(text, options))
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
.catch((e) => console.error(e))
|
|
|
|
);
|
2018-02-08 05:30:55 +01:00
|
|
|
} else {
|
2022-03-16 14:09:23 +01:00
|
|
|
return Promise.resolve(cook(text));
|
2018-02-08 05:30:55 +01:00
|
|
|
}
|
|
|
|
}
|