From 032b1f871b23ceaf09db644a196aba289e9afc3a Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 8 Nov 2024 14:43:28 +0900 Subject: [PATCH] DEV: cook grid bbcode in chat (#29658) This change will only prevent a cooked message with [grid] to show [grid] instead the content will be wrapped in `div class="d-image-grid"`. This is only enabled on messages made by bot, as regular users could use grid but have no reason to use it ATM. It will also not apply the decoration which shouldn't change the behavior more than just remove grid markup from the message --- plugins/chat/app/models/chat/message.rb | 10 +++++++--- plugins/chat/spec/models/chat/message_spec.rb | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rb index 7e642fe30b9..3766fbe0ead 100644 --- a/plugins/chat/app/models/chat/message.rb +++ b/plugins/chat/app/models/chat/message.rb @@ -223,8 +223,13 @@ module Chat ] def self.cook(message, opts = {}) + bot = opts[:user_id] && opts[:user_id].negative? + + features = MARKDOWN_FEATURES.dup + features << "image-grid" if bot + rules = MARKDOWN_IT_RULES.dup - rules << "heading" if opts[:user_id] && opts[:user_id].negative? + rules << "heading" if bot # A rule in our Markdown pipeline may have Guardian checks that require a # user to be present. The last editing user of the message will be more @@ -235,8 +240,7 @@ module Chat cooked = PrettyText.cook( message, - features_override: - MARKDOWN_FEATURES + DiscoursePluginRegistry.chat_markdown_features.to_a, + features_override: features + DiscoursePluginRegistry.chat_markdown_features.to_a, markdown_it_rules: rules, force_quote_link: true, user_id: opts[:user_id], diff --git a/plugins/chat/spec/models/chat/message_spec.rb b/plugins/chat/spec/models/chat/message_spec.rb index 32e696b6219..3c66a186863 100644 --- a/plugins/chat/spec/models/chat/message_spec.rb +++ b/plugins/chat/spec/models/chat/message_spec.rb @@ -99,6 +99,16 @@ describe Chat::Message do
h6
HTML end + + it "cooks the grid bbcode" do + cooked = described_class.cook("[grid]\ntest\n[/grid]", user_id: -1) + + expect(cooked).to match_html <<~HTML +
+

test

+
+ HTML + end end it "doesn't support headings" do @@ -109,6 +119,14 @@ describe Chat::Message do HTML end + it "doesn't support grid" do + cooked = described_class.cook("[grid]\ntest\n[/grid]") + + expect(cooked).to match_html <<~HTML +

[grid]
test
[/grid]

+ HTML + end + it "supports horizontal replacement" do cooked = described_class.cook("---")