From fcc9d99ba256ba323eb1ebaa8b256d759eadded7 Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Mon, 9 Oct 2023 20:22:46 +0400 Subject: [PATCH] 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. --- .../discourse/app/components/cook-text.gjs | 4 ++-- .../app/components/modal/change-post-notice.js | 4 ++-- .../discourse/app/components/summary-box.js | 4 ++-- .../app/controllers/preferences/profile.js | 4 ++-- .../javascripts/discourse/app/lib/text.js | 17 ++++++++++++----- .../discourse/app/models/pending-post.js | 4 ++-- .../javascripts/discourse/app/models/post.js | 4 ++-- .../discourse/app/models/user-drafts-stream.js | 4 ++-- .../discourse/tests/unit/lib/text-test.js | 15 +++++---------- .../test/javascripts/lib/checklist-test.js | 4 ++-- .../components/modal/local-dates-create.js | 4 ++-- .../discourse/components/styleguide-markdown.js | 6 ++---- 12 files changed, 37 insertions(+), 37 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/cook-text.gjs b/app/assets/javascripts/discourse/app/components/cook-text.gjs index fd074a24f4e..51d5f11409e 100644 --- a/app/assets/javascripts/discourse/app/components/cook-text.gjs +++ b/app/assets/javascripts/discourse/app/components/cook-text.gjs @@ -4,7 +4,7 @@ import didUpdate from "@ember/render-modifiers/modifiers/did-update"; import { action } from "@ember/object"; import { tracked } from "@glimmer/tracking"; 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 { ajax } from "discourse/lib/ajax"; @@ -32,7 +32,7 @@ export default class CookText extends Component { @action async loadCookedText() { - const cooked = await cookAsync(this.args.rawText); + const cooked = await cook(this.args.rawText); this.cooked = cooked; } diff --git a/app/assets/javascripts/discourse/app/components/modal/change-post-notice.js b/app/assets/javascripts/discourse/app/components/modal/change-post-notice.js index 74c69ff58b1..fd67c931aa9 100644 --- a/app/assets/javascripts/discourse/app/components/modal/change-post-notice.js +++ b/app/assets/javascripts/discourse/app/components/modal/change-post-notice.js @@ -1,7 +1,7 @@ import Component from "@glimmer/component"; import { tracked } from "@glimmer/tracking"; import { action } from "@ember/object"; -import { cookAsync } from "discourse/lib/text"; +import { cook } from "discourse/lib/text"; import { isEmpty } from "@ember/utils"; export default class ChangePostNoticeModal extends Component { @@ -42,7 +42,7 @@ export default class ChangePostNoticeModal extends Component { .updatePostField("notice", notice) .then(() => { if (notice) { - return cookAsync(notice, { features: { onebox: false } }); + return cook(notice, { features: { onebox: false } }); } }) .then((cooked) => diff --git a/app/assets/javascripts/discourse/app/components/summary-box.js b/app/assets/javascripts/discourse/app/components/summary-box.js index 691f1342934..b08472006cf 100644 --- a/app/assets/javascripts/discourse/app/components/summary-box.js +++ b/app/assets/javascripts/discourse/app/components/summary-box.js @@ -5,7 +5,7 @@ import { inject as service } from "@ember/service"; import { action } from "@ember/object"; import { ajax } from "discourse/lib/ajax"; 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 { bind } from "discourse-common/utils/decorators"; @@ -44,7 +44,7 @@ export default class SummaryBox extends Component { const topicSummary = update.topic_summary; if (topicSummary.summarized_text) { - cookAsync(topicSummary.summarized_text).then((cooked) => { + cook(topicSummary.summarized_text).then((cooked) => { this.summary = cooked; }); } diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/profile.js b/app/assets/javascripts/discourse/app/controllers/preferences/profile.js index 147e5c4800e..a816630fc3f 100644 --- a/app/assets/javascripts/discourse/app/controllers/preferences/profile.js +++ b/app/assets/javascripts/discourse/app/controllers/preferences/profile.js @@ -2,7 +2,7 @@ import Controller from "@ember/controller"; import EmberObject, { action } from "@ember/object"; import I18n from "I18n"; 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 { isEmpty } from "@ember/utils"; import { popupAjaxError } from "discourse/lib/ajax-error"; @@ -128,7 +128,7 @@ export default Controller.extend({ return model .save(this.saveAttrNames) .then(() => { - cookAsync(model.get("bio_raw")) + cook(model.get("bio_raw")) .then(() => { model.set("bio_cooked"); this.set("saved", true); diff --git a/app/assets/javascripts/discourse/app/lib/text.js b/app/assets/javascripts/discourse/app/lib/text.js index 2fb0e8aa28f..36301bf71b1 100644 --- a/app/assets/javascripts/discourse/app/lib/text.js +++ b/app/assets/javascripts/discourse/app/lib/text.js @@ -10,6 +10,7 @@ import { htmlSafe } from "@ember/template"; import loadScript from "discourse/lib/load-script"; import { sanitize as textSanitize } from "pretty-text/sanitizer"; import { MentionsParser } from "discourse/lib/mentions-parser"; +import deprecated from "discourse-common/lib/deprecated"; function getOpts(opts) { let context = helperContext(); @@ -33,15 +34,21 @@ function getOpts(opts) { return buildOptions(opts); } -// Use this to easily create a pretty text instance with proper 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 -// cook +// todo drop this function after migrating everything to cook() 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 diff --git a/app/assets/javascripts/discourse/app/models/pending-post.js b/app/assets/javascripts/discourse/app/models/pending-post.js index 65f723a3f75..91161474956 100644 --- a/app/assets/javascripts/discourse/app/models/pending-post.js +++ b/app/assets/javascripts/discourse/app/models/pending-post.js @@ -3,7 +3,7 @@ import RestModel from "discourse/models/rest"; import categoryFromId from "discourse-common/utils/category-macro"; import { userPath } from "discourse/lib/url"; import { reads } from "@ember/object/computed"; -import { cookAsync } from "discourse/lib/text"; +import { cook } from "discourse/lib/text"; const PendingPost = RestModel.extend({ expandedExcerpt: null, @@ -12,7 +12,7 @@ const PendingPost = RestModel.extend({ init() { this._super(...arguments); - cookAsync(this.raw_text).then((cooked) => { + cook(this.raw_text).then((cooked) => { this.set("expandedExcerpt", cooked); }); }, diff --git a/app/assets/javascripts/discourse/app/models/post.js b/app/assets/javascripts/discourse/app/models/post.js index 1f9f1573ddc..beb49ea7ec5 100644 --- a/app/assets/javascripts/discourse/app/models/post.js +++ b/app/assets/javascripts/discourse/app/models/post.js @@ -8,7 +8,7 @@ import RestModel from "discourse/models/rest"; import Site from "discourse/models/site"; import User from "discourse/models/user"; 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 { fancyTitle } from "discourse/lib/topic-fancy-title"; import { isEmpty } from "@ember/utils"; @@ -217,7 +217,7 @@ const Post = RestModel.extend({ this.post_number === 1 ? "topic.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({ cooked, can_delete: false, diff --git a/app/assets/javascripts/discourse/app/models/user-drafts-stream.js b/app/assets/javascripts/discourse/app/models/user-drafts-stream.js index 30e91fd2045..a48e1f1ef1f 100644 --- a/app/assets/javascripts/discourse/app/models/user-drafts-stream.js +++ b/app/assets/javascripts/discourse/app/models/user-drafts-stream.js @@ -1,6 +1,6 @@ import discourseComputed from "discourse-common/utils/decorators"; 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 { NEW_PRIVATE_MESSAGE_KEY, @@ -68,7 +68,7 @@ export default RestModel.extend({ const promises = result.drafts.map((draft) => { 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.post_number = draft.data.postId || null; if ( diff --git a/app/assets/javascripts/discourse/tests/unit/lib/text-test.js b/app/assets/javascripts/discourse/tests/unit/lib/text-test.js index 5e2b3a47bdc..68e7e662b37 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/text-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/text-test.js @@ -1,11 +1,6 @@ import { module, test } from "qunit"; import { setupTest } from "ember-qunit"; -import { - cookAsync, - excerpt, - parseAsync, - parseMentions, -} from "discourse/lib/text"; +import { cook, excerpt, parseAsync, parseMentions } from "discourse/lib/text"; module("Unit | Utility | text", function (hooks) { setupTest(hooks); @@ -21,22 +16,22 @@ module("Unit | Utility | text", function (hooks) { }); test("excerpt", async function (assert) { - let cooked = await cookAsync("Hello! :wave:"); + let cooked = await cook("Hello! :wave:"); assert.strictEqual( await excerpt(cooked, 300), 'Hello! :wave:' ); - cooked = await cookAsync("[:wave:](https://example.com)"); + cooked = await cook("[:wave:](https://example.com)"); assert.strictEqual( await excerpt(cooked, 300), ':wave:' ); - cooked = await cookAsync(''); + cooked = await cook(''); assert.strictEqual(await excerpt(cooked, 300), ""); - cooked = await cookAsync("[``]()"); + cooked = await cook("[``]()"); assert.strictEqual( await excerpt(cooked, 300), "<script>alert('hi')</script>" diff --git a/plugins/checklist/test/javascripts/lib/checklist-test.js b/plugins/checklist/test/javascripts/lib/checklist-test.js index 243340a410e..61abcacdb17 100644 --- a/plugins/checklist/test/javascripts/lib/checklist-test.js +++ b/plugins/checklist/test/javascripts/lib/checklist-test.js @@ -1,6 +1,6 @@ import { acceptance } from "discourse/tests/helpers/qunit-helpers"; import { test } from "qunit"; -import { cookAsync } from "discourse/lib/text"; +import { cook } from "discourse/lib/text"; import Post from "discourse/models/post"; import { checklistSyntax } from "discourse/plugins/checklist/discourse/initializers/checklist"; import { Promise } from "rsvp"; @@ -8,7 +8,7 @@ import { Promise } from "rsvp"; let currentRaw; async function prepare(raw) { - const cooked = await cookAsync(raw, { + const cooked = await cook(raw, { siteSettings: { checklist_enabled: true }, }); diff --git a/plugins/discourse-local-dates/assets/javascripts/discourse/components/modal/local-dates-create.js b/plugins/discourse-local-dates/assets/javascripts/discourse/components/modal/local-dates-create.js index ddf42ef1951..7d8f76b06c5 100644 --- a/plugins/discourse-local-dates/assets/javascripts/discourse/components/modal/local-dates-create.js +++ b/plugins/discourse-local-dates/assets/javascripts/discourse/components/modal/local-dates-create.js @@ -6,7 +6,7 @@ import Component from "@ember/component"; import EmberObject, { action } from "@ember/object"; import I18n from "I18n"; 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 { propertyNotEqual } from "discourse/lib/computed"; import { schedule } from "@ember/runloop"; @@ -59,7 +59,7 @@ export default Component.extend({ @debounce(INPUT_DELAY) async _renderPreview() { if (this.markup) { - const result = await cookAsync(this.markup); + const result = await cook(this.markup); this.set("currentPreview", result); schedule("afterRender", () => { diff --git a/plugins/styleguide/assets/javascripts/discourse/components/styleguide-markdown.js b/plugins/styleguide/assets/javascripts/discourse/components/styleguide-markdown.js index 8847cfc8990..df5b1d8edea 100644 --- a/plugins/styleguide/assets/javascripts/discourse/components/styleguide-markdown.js +++ b/plugins/styleguide/assets/javascripts/discourse/components/styleguide-markdown.js @@ -1,13 +1,11 @@ import Component from "@ember/component"; -import { cookAsync } from "discourse/lib/text"; +import { cook } from "discourse/lib/text"; export default Component.extend({ didInsertElement() { this._super(...arguments); const contents = $(this.element).html(); - cookAsync(contents).then((cooked) => - $(this.element).html(cooked.toString()) - ); + cook(contents).then((cooked) => $(this.element).html(cooked.toString())); }, });