DEV: Start renaming cookAsync() function to cook() (#23462)
Some time ago, we introduced the `cookAsync` instead of the existing `cook` function, and planned to migrate everything to it. Then after migrating, we wanted to raname the function to simply `cook`. I've checked Core and plugins, and currently we call `cookAsync` everywhere, there are no calls to the `cook` function anymore. So we're good to proceed with this refactoring. This PR makes the first step by making current cookAsync and cook functions do the same thing. Effectively now the `cook` function becomes an alias for the `cookAsync` function.
This commit is contained in:
parent
62c7a54f88
commit
fcc9d99ba2
|
@ -4,7 +4,7 @@ import didUpdate from "@ember/render-modifiers/modifiers/did-update";
|
||||||
import { action } from "@ember/object";
|
import { action } from "@ember/object";
|
||||||
import { tracked } from "@glimmer/tracking";
|
import { tracked } from "@glimmer/tracking";
|
||||||
import { loadOneboxes } from "discourse/lib/load-oneboxes";
|
import { loadOneboxes } from "discourse/lib/load-oneboxes";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
import { resolveAllShortUrls } from "pretty-text/upload-short-url";
|
import { resolveAllShortUrls } from "pretty-text/upload-short-url";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ export default class CookText extends Component {
|
||||||
|
|
||||||
@action
|
@action
|
||||||
async loadCookedText() {
|
async loadCookedText() {
|
||||||
const cooked = await cookAsync(this.args.rawText);
|
const cooked = await cook(this.args.rawText);
|
||||||
this.cooked = cooked;
|
this.cooked = cooked;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import Component from "@glimmer/component";
|
import Component from "@glimmer/component";
|
||||||
import { tracked } from "@glimmer/tracking";
|
import { tracked } from "@glimmer/tracking";
|
||||||
import { action } from "@ember/object";
|
import { action } from "@ember/object";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
import { isEmpty } from "@ember/utils";
|
import { isEmpty } from "@ember/utils";
|
||||||
|
|
||||||
export default class ChangePostNoticeModal extends Component {
|
export default class ChangePostNoticeModal extends Component {
|
||||||
|
@ -42,7 +42,7 @@ export default class ChangePostNoticeModal extends Component {
|
||||||
.updatePostField("notice", notice)
|
.updatePostField("notice", notice)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (notice) {
|
if (notice) {
|
||||||
return cookAsync(notice, { features: { onebox: false } });
|
return cook(notice, { features: { onebox: false } });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((cooked) =>
|
.then((cooked) =>
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { inject as service } from "@ember/service";
|
||||||
import { action } from "@ember/object";
|
import { action } from "@ember/object";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { popupAjaxError } from "discourse/lib/ajax-error";
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
import { shortDateNoYear } from "discourse/lib/formatter";
|
import { shortDateNoYear } from "discourse/lib/formatter";
|
||||||
import { bind } from "discourse-common/utils/decorators";
|
import { bind } from "discourse-common/utils/decorators";
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ export default class SummaryBox extends Component {
|
||||||
const topicSummary = update.topic_summary;
|
const topicSummary = update.topic_summary;
|
||||||
|
|
||||||
if (topicSummary.summarized_text) {
|
if (topicSummary.summarized_text) {
|
||||||
cookAsync(topicSummary.summarized_text).then((cooked) => {
|
cook(topicSummary.summarized_text).then((cooked) => {
|
||||||
this.summary = cooked;
|
this.summary = cooked;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import Controller from "@ember/controller";
|
||||||
import EmberObject, { action } from "@ember/object";
|
import EmberObject, { action } from "@ember/object";
|
||||||
import I18n from "I18n";
|
import I18n from "I18n";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
import { isEmpty } from "@ember/utils";
|
import { isEmpty } from "@ember/utils";
|
||||||
import { popupAjaxError } from "discourse/lib/ajax-error";
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
||||||
|
@ -128,7 +128,7 @@ export default Controller.extend({
|
||||||
return model
|
return model
|
||||||
.save(this.saveAttrNames)
|
.save(this.saveAttrNames)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
cookAsync(model.get("bio_raw"))
|
cook(model.get("bio_raw"))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
model.set("bio_cooked");
|
model.set("bio_cooked");
|
||||||
this.set("saved", true);
|
this.set("saved", true);
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { htmlSafe } from "@ember/template";
|
||||||
import loadScript from "discourse/lib/load-script";
|
import loadScript from "discourse/lib/load-script";
|
||||||
import { sanitize as textSanitize } from "pretty-text/sanitizer";
|
import { sanitize as textSanitize } from "pretty-text/sanitizer";
|
||||||
import { MentionsParser } from "discourse/lib/mentions-parser";
|
import { MentionsParser } from "discourse/lib/mentions-parser";
|
||||||
|
import deprecated from "discourse-common/lib/deprecated";
|
||||||
|
|
||||||
function getOpts(opts) {
|
function getOpts(opts) {
|
||||||
let context = helperContext();
|
let context = helperContext();
|
||||||
|
@ -33,15 +34,21 @@ function getOpts(opts) {
|
||||||
return buildOptions(opts);
|
return buildOptions(opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use this to easily create a pretty text instance with proper options
|
|
||||||
export function cook(text, options) {
|
export function cook(text, options) {
|
||||||
return htmlSafe(createPrettyText(options).cook(text));
|
return loadMarkdownIt().then(() => {
|
||||||
|
const cooked = createPrettyText(options).cook(text);
|
||||||
|
return htmlSafe(cooked);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// everything should eventually move to async API and this should be renamed
|
// todo drop this function after migrating everything to cook()
|
||||||
// cook
|
|
||||||
export function cookAsync(text, options) {
|
export function cookAsync(text, options) {
|
||||||
return loadMarkdownIt().then(() => cook(text, options));
|
deprecated("cookAsync() is deprecated, call cook() instead", {
|
||||||
|
since: "3.2.0.beta2",
|
||||||
|
dropFrom: "3.2.0.beta5",
|
||||||
|
id: "discourse.text.cook-async",
|
||||||
|
});
|
||||||
|
return cook(text, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warm up pretty text with a set of options and return a function
|
// Warm up pretty text with a set of options and return a function
|
||||||
|
|
|
@ -3,7 +3,7 @@ import RestModel from "discourse/models/rest";
|
||||||
import categoryFromId from "discourse-common/utils/category-macro";
|
import categoryFromId from "discourse-common/utils/category-macro";
|
||||||
import { userPath } from "discourse/lib/url";
|
import { userPath } from "discourse/lib/url";
|
||||||
import { reads } from "@ember/object/computed";
|
import { reads } from "@ember/object/computed";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
|
|
||||||
const PendingPost = RestModel.extend({
|
const PendingPost = RestModel.extend({
|
||||||
expandedExcerpt: null,
|
expandedExcerpt: null,
|
||||||
|
@ -12,7 +12,7 @@ const PendingPost = RestModel.extend({
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
this._super(...arguments);
|
this._super(...arguments);
|
||||||
cookAsync(this.raw_text).then((cooked) => {
|
cook(this.raw_text).then((cooked) => {
|
||||||
this.set("expandedExcerpt", cooked);
|
this.set("expandedExcerpt", cooked);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,7 +8,7 @@ import RestModel from "discourse/models/rest";
|
||||||
import Site from "discourse/models/site";
|
import Site from "discourse/models/site";
|
||||||
import User from "discourse/models/user";
|
import User from "discourse/models/user";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
import { fancyTitle } from "discourse/lib/topic-fancy-title";
|
import { fancyTitle } from "discourse/lib/topic-fancy-title";
|
||||||
import { isEmpty } from "@ember/utils";
|
import { isEmpty } from "@ember/utils";
|
||||||
|
@ -217,7 +217,7 @@ const Post = RestModel.extend({
|
||||||
this.post_number === 1
|
this.post_number === 1
|
||||||
? "topic.deleted_by_author_simple"
|
? "topic.deleted_by_author_simple"
|
||||||
: "post.deleted_by_author_simple";
|
: "post.deleted_by_author_simple";
|
||||||
promise = cookAsync(I18n.t(key)).then((cooked) => {
|
promise = cook(I18n.t(key)).then((cooked) => {
|
||||||
this.setProperties({
|
this.setProperties({
|
||||||
cooked,
|
cooked,
|
||||||
can_delete: false,
|
can_delete: false,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { cookAsync, emojiUnescape, excerpt } from "discourse/lib/text";
|
import { cook, emojiUnescape, excerpt } from "discourse/lib/text";
|
||||||
import { escapeExpression } from "discourse/lib/utilities";
|
import { escapeExpression } from "discourse/lib/utilities";
|
||||||
import {
|
import {
|
||||||
NEW_PRIVATE_MESSAGE_KEY,
|
NEW_PRIVATE_MESSAGE_KEY,
|
||||||
|
@ -68,7 +68,7 @@ export default RestModel.extend({
|
||||||
|
|
||||||
const promises = result.drafts.map((draft) => {
|
const promises = result.drafts.map((draft) => {
|
||||||
draft.data = JSON.parse(draft.data);
|
draft.data = JSON.parse(draft.data);
|
||||||
return cookAsync(draft.data.reply).then((cooked) => {
|
return cook(draft.data.reply).then((cooked) => {
|
||||||
draft.excerpt = excerpt(cooked.toString(), 300);
|
draft.excerpt = excerpt(cooked.toString(), 300);
|
||||||
draft.post_number = draft.data.postId || null;
|
draft.post_number = draft.data.postId || null;
|
||||||
if (
|
if (
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
import { module, test } from "qunit";
|
import { module, test } from "qunit";
|
||||||
import { setupTest } from "ember-qunit";
|
import { setupTest } from "ember-qunit";
|
||||||
import {
|
import { cook, excerpt, parseAsync, parseMentions } from "discourse/lib/text";
|
||||||
cookAsync,
|
|
||||||
excerpt,
|
|
||||||
parseAsync,
|
|
||||||
parseMentions,
|
|
||||||
} from "discourse/lib/text";
|
|
||||||
|
|
||||||
module("Unit | Utility | text", function (hooks) {
|
module("Unit | Utility | text", function (hooks) {
|
||||||
setupTest(hooks);
|
setupTest(hooks);
|
||||||
|
@ -21,22 +16,22 @@ module("Unit | Utility | text", function (hooks) {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("excerpt", async function (assert) {
|
test("excerpt", async function (assert) {
|
||||||
let cooked = await cookAsync("Hello! :wave:");
|
let cooked = await cook("Hello! :wave:");
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
await excerpt(cooked, 300),
|
await excerpt(cooked, 300),
|
||||||
'Hello! <img src="/images/emoji/twitter/wave.png?v=12" title=":wave:" class="emoji" alt=":wave:" loading="lazy" width="20" height="20">'
|
'Hello! <img src="/images/emoji/twitter/wave.png?v=12" title=":wave:" class="emoji" alt=":wave:" loading="lazy" width="20" height="20">'
|
||||||
);
|
);
|
||||||
|
|
||||||
cooked = await cookAsync("[:wave:](https://example.com)");
|
cooked = await cook("[:wave:](https://example.com)");
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
await excerpt(cooked, 300),
|
await excerpt(cooked, 300),
|
||||||
'<a href="https://example.com"><img src="/images/emoji/twitter/wave.png?v=12" title=":wave:" class="emoji only-emoji" alt=":wave:" loading="lazy" width="20" height="20"></a>'
|
'<a href="https://example.com"><img src="/images/emoji/twitter/wave.png?v=12" title=":wave:" class="emoji only-emoji" alt=":wave:" loading="lazy" width="20" height="20"></a>'
|
||||||
);
|
);
|
||||||
|
|
||||||
cooked = await cookAsync('<script>alert("hi")</script>');
|
cooked = await cook('<script>alert("hi")</script>');
|
||||||
assert.strictEqual(await excerpt(cooked, 300), "");
|
assert.strictEqual(await excerpt(cooked, 300), "");
|
||||||
|
|
||||||
cooked = await cookAsync("[`<script>alert('hi')</script>`]()");
|
cooked = await cook("[`<script>alert('hi')</script>`]()");
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
await excerpt(cooked, 300),
|
await excerpt(cooked, 300),
|
||||||
"<a><code><script>alert('hi')</script></code></a>"
|
"<a><code><script>alert('hi')</script></code></a>"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
||||||
import { test } from "qunit";
|
import { test } from "qunit";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
import Post from "discourse/models/post";
|
import Post from "discourse/models/post";
|
||||||
import { checklistSyntax } from "discourse/plugins/checklist/discourse/initializers/checklist";
|
import { checklistSyntax } from "discourse/plugins/checklist/discourse/initializers/checklist";
|
||||||
import { Promise } from "rsvp";
|
import { Promise } from "rsvp";
|
||||||
|
@ -8,7 +8,7 @@ import { Promise } from "rsvp";
|
||||||
let currentRaw;
|
let currentRaw;
|
||||||
|
|
||||||
async function prepare(raw) {
|
async function prepare(raw) {
|
||||||
const cooked = await cookAsync(raw, {
|
const cooked = await cook(raw, {
|
||||||
siteSettings: { checklist_enabled: true },
|
siteSettings: { checklist_enabled: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ import Component from "@ember/component";
|
||||||
import EmberObject, { action } from "@ember/object";
|
import EmberObject, { action } from "@ember/object";
|
||||||
import I18n from "I18n";
|
import I18n from "I18n";
|
||||||
import { INPUT_DELAY } from "discourse-common/config/environment";
|
import { INPUT_DELAY } from "discourse-common/config/environment";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
import { notEmpty } from "@ember/object/computed";
|
import { notEmpty } from "@ember/object/computed";
|
||||||
import { propertyNotEqual } from "discourse/lib/computed";
|
import { propertyNotEqual } from "discourse/lib/computed";
|
||||||
import { schedule } from "@ember/runloop";
|
import { schedule } from "@ember/runloop";
|
||||||
|
@ -59,7 +59,7 @@ export default Component.extend({
|
||||||
@debounce(INPUT_DELAY)
|
@debounce(INPUT_DELAY)
|
||||||
async _renderPreview() {
|
async _renderPreview() {
|
||||||
if (this.markup) {
|
if (this.markup) {
|
||||||
const result = await cookAsync(this.markup);
|
const result = await cook(this.markup);
|
||||||
this.set("currentPreview", result);
|
this.set("currentPreview", result);
|
||||||
|
|
||||||
schedule("afterRender", () => {
|
schedule("afterRender", () => {
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
import Component from "@ember/component";
|
import Component from "@ember/component";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { cook } from "discourse/lib/text";
|
||||||
|
|
||||||
export default Component.extend({
|
export default Component.extend({
|
||||||
didInsertElement() {
|
didInsertElement() {
|
||||||
this._super(...arguments);
|
this._super(...arguments);
|
||||||
|
|
||||||
const contents = $(this.element).html();
|
const contents = $(this.element).html();
|
||||||
cookAsync(contents).then((cooked) =>
|
cook(contents).then((cooked) => $(this.element).html(cooked.toString()));
|
||||||
$(this.element).html(cooked.toString())
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue