A platform for community discussion. Free, open, simple.
DEV: Add a transformer for build quote (#34297)
Amanda Alves Branquinho10eb60ff55e9
diff --git a/app/assets/javascripts/discourse/app/lib/quote.js b/app/assets/javascripts/discourse/app/lib/quote.jsindex 59af4357bd6..874e8f75fa1 100644--- a/app/assets/javascripts/discourse/app/lib/quote.js+++ b/app/assets/javascripts/discourse/app/lib/quote.js@@ -1,5 +1,6 @@import { helperContext } from "discourse/lib/helpers";import { prioritizeNameFallback } from "discourse/lib/settings";+import { applyValueTransformer } from "discourse/lib/transformer";export const QUOTE_REGEXP =/\[quote=([^\]]*)\]((?:[\s\S](?!\[quote=[^\]]*\]))*?)\[\/quote\]/im;@@ -37,5 +38,12 @@ export function buildQuote(post, contents, opts = {}) {params.push(`username:${opts.username || post.username}`);}- return `[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;+ const transformedParams = applyValueTransformer("quote-params", params, {+ post,+ contents,+ opts,+ });++ const paramsString = transformedParams.join(", ");+ return `[quote="${paramsString}"]\n${contents.trim()}\n[/quote]\n\n`;}diff --git a/app/assets/javascripts/discourse/app/lib/transformer/registry.js b/app/assets/javascripts/discourse/app/lib/transformer/registry.jsindex 4a4d753eb19..ef610d9eb3c 100644--- a/app/assets/javascripts/discourse/app/lib/transformer/registry.js+++ b/app/assets/javascripts/discourse/app/lib/transformer/registry.js@@ -63,6 +63,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["post-small-action-icon","poster-name-user-title","poster-name-class",+ "quote-params","small-user-attrs","tag-separator","topic-list-class",diff --git a/app/assets/javascripts/discourse/tests/unit/lib/build-quote-test.js b/app/assets/javascripts/discourse/tests/unit/lib/build-quote-test.jsindex 562daac9d4f..f10116ea7cd 100644--- a/app/assets/javascripts/discourse/tests/unit/lib/build-quote-test.js+++ b/app/assets/javascripts/discourse/tests/unit/lib/build-quote-test.js@@ -1,6 +1,7 @@import { getOwner } from "@ember/owner";import { setupTest } from "ember-qunit";import { module, test } from "qunit";+import { withPluginApi } from "discourse/lib/plugin-api";import { buildQuote } from "discourse/lib/quote";import DiscourseMarkdownIt from "discourse-markdown-it";@@ -79,4 +80,28 @@ module("Unit | Utility | build-quote", function (hooks) {"allows quoting a quote");});++ test("applying a value transformation", async function (assert) {+ const store = getOwner(this).lookup("service:store");+ const post = store.createRecord("post", {+ cooked: "<p><b>lorem</b> ipsum</p>",+ username: "eviltrout",+ post_number: 1,+ topic_id: 2,+ });++ withPluginApi((api) => {+ api.registerValueTransformer("quote-params", ({ value }) => {+ value[0] = "New transformed name";+ return value;+ });+ });++ const quote = buildQuote(post, "hello");+ assert.strictEqual(+ quote,+ '[quote="New transformed name, post:1, topic:2"]\nhello\n[/quote]\n\n',+ "it transforms the quote params name"+ );+ });});