Skip to content

A platform for community discussion. Free, open, simple.

FIX: Render small post actions in Markdown endpoints (#45237)

Penar Musarajc77628905fd2
diff --git a/lib/markdown_endpoint/timestamp.rb b/lib/markdown_endpoint/timestamp.rb
index caf64f838a6..9d699629abf 100644
--- a/lib/markdown_endpoint/timestamp.rb
+++ b/lib/markdown_endpoint/timestamp.rb
@@ -8,11 +8,13 @@ module MarkdownEndpoint
def render(time, url:)
local = time.in_time_zone(@timezone)
- label =
- "#{I18n.l(local, format: :long)} #{local.strftime("%Z")}"
- .squish
- .gsub(/[\\`*_\[\]<>]/) { |character| "\\#{character}" }
+ label = text(local).gsub(/[\\`*_\[\]<>]/) { |character| "\\#{character}" }
%([#{label}](#{url} "#{local.iso8601}"))
end
+
+ def text(time)
+ local = time.in_time_zone(@timezone)
+ "#{I18n.l(local, format: :long)} #{local.strftime("%Z")}".squish
+ end
end
end
diff --git a/lib/markdown_endpoint/topic_renderer.rb b/lib/markdown_endpoint/topic_renderer.rb
index 3c3dcf14eef..0d2d8b5ba8c 100644
--- a/lib/markdown_endpoint/topic_renderer.rb
+++ b/lib/markdown_endpoint/topic_renderer.rb
@@ -95,10 +95,34 @@ module MarkdownEndpoint
)
lines << metadata.join("\\\n")
lines.concat(["", "</div>", ""])
+ if (description = action_description(post)).present?
+ lines.concat([description, ""])
+ end
lines << cached_body(cooked, post_url: post.full_url)
lines.join("\n")
end
+ def action_description(post)
+ return if post.action_code.blank?
+
+ serializer = PostSerializer.new(post, scope: @guardian, root: false)
+ serializer.topic_view = @topic_view
+ who = serializer.action_code_who
+ description =
+ I18n.t(
+ "js.action_codes.#{serializer.action_code}",
+ who: ERB::Util.html_escape(who.present? ? "@#{who}" : ""),
+ when:
+ ERB::Util.html_escape(
+ I18n.t("js.dates.wrap_on", date: @timestamp.text(post.created_at)),
+ ),
+ path: ERB::Util.html_escape(serializer.action_code_path || @topic.relative_url),
+ default: "",
+ )
+
+ escape_text(Nokogiri::HTML5.fragment(description).text.strip)
+ end
+
def cached_body(cooked, post_url:)
context = [
CookedProcessor::VERSION,
diff --git a/spec/requests/markdown_endpoints_spec.rb b/spec/requests/markdown_endpoints_spec.rb
index 0b2bb58a2ee..838dc3df034 100644
--- a/spec/requests/markdown_endpoints_spec.rb
+++ b/spec/requests/markdown_endpoints_spec.rb
@@ -361,6 +361,165 @@ RSpec.describe "Markdown endpoints" do
expect(response.body).not_to include(post.raw, "[Next page]", "[Previous page]")
end
+ it "renders every core small action description even when its body is empty" do
+ descriptions = {
+ "topic_created" => "Created this topic",
+ "public_topic" => "Made this topic public",
+ "open_topic" => "Converted this to a topic",
+ "private_topic" => "Made this topic a personal message",
+ "split_topic" => "Split this topic",
+ "invited_user" => "Invited @action\\_target",
+ "invited_group" => "Invited @action\\_target",
+ "user_left" => "@action\\_target removed themselves from this message",
+ "removed_user" => "Removed @action\\_target",
+ "removed_group" => "Removed @action\\_target",
+ "autobumped" => "Automatically bumped",
+ "tags_changed" => "Tags updated",
+ "category_changed" => "Category updated",
+ "autoclosed.enabled" => "Closed",
+ "autoclosed.disabled" => "Opened",
+ "closed.enabled" => "Closed",
+ "closed.disabled" => "Opened",
+ "archived.enabled" => "Archived",
+ "archived.disabled" => "Unarchived",
+ "pinned.enabled" => "Pinned",
+ "pinned.disabled" => "Unpinned",
+ "pinned_globally.enabled" => "Pinned globally",
+ "pinned_globally.disabled" => "Unpinned",
+ "visible.enabled" => "Listed",
+ "visible.disabled" => "Unlisted",
+ "banner.enabled" => "Made this a banner",
+ "banner.disabled" => "Removed this banner",
+ "forwarded" => "Forwarded the above email",
+ }
+ descriptions.each_key do |action_code|
+ Fabricate(
+ :small_action,
+ topic:,
+ user:,
+ action_code:,
+ skip_validation: true,
+ raw: "",
+ cooked: "",
+ custom_fields: {
+ "action_code_who" => "action_target",
+ },
+ created_at: Time.utc(2026, 7, 15, 16, 30),
+ )
+ end
+
+ stub_const(TopicView, :CHUNK_SIZE, 50) { get "/t/#{topic.slug}/#{topic.id}.md" }
+
+ expect(response).to have_http_status(:ok)
+ bodies = response.body.scan(%r{</div>\n\n(.*?)(?=\n\n---|\z)}m).flatten.map(&:strip)
+ expected =
+ descriptions.values.map { |description| "#{description} on July 15, 2026, 4:30pm UTC" }
+ expected[-3] += ". It will appear at the top of every page until it is dismissed by the user."
+ expected[-2] += ". It will no longer appear at the top of every page."
+ expected[-1] = "Forwarded the above email"
+ expect(bodies).to eq([post.raw, *expected])
+ end
+
+ it "renders small action descriptions before custom messages in topics and individual posts" do
+ action =
+ Fabricate(
+ :small_action,
+ topic:,
+ user:,
+ action_code: "closed.enabled",
+ raw: "Please **stay on topic**.",
+ )
+
+ [
+ "/t/#{topic.slug}/#{topic.id}.md",
+ "/t/#{topic.slug}/#{topic.id}/#{action.post_number}.md",
+ ].each do |path|
+ get path
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to match(
+ %r{</div>\n\nClosed on [^\n]+\n\nPlease \*\*stay on topic\*\*\.},
+ )
+ end
+ end
+
+ it "uses the app's conversion wording on login-required sites and the reader's timezone" do
+ SiteSetting.login_required = true
+ user.user_option.update!(timezone: "America/Toronto")
+ sign_in(user)
+ action =
+ Fabricate(
+ :small_action,
+ topic:,
+ user:,
+ action_code: "public_topic",
+ created_at: Time.utc(2026, 7, 15, 16, 30),
+ )
+
+ get "/t/#{topic.slug}/#{topic.id}/#{action.post_number}.md"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include("Converted this to a topic on July 15, 2026, 12:30pm EDT")
+ end
+
+ it "renders plugin action translations as text and escapes action targets" do
+ I18n.backend.store_translations(
+ :en,
+ js: {
+ action_codes: {
+ test_action: "Assigned %{who} to <a href='%{path}'>this &amp; that</a> %{when}",
+ },
+ },
+ )
+ action =
+ Fabricate(
+ :small_action,
+ topic:,
+ user:,
+ action_code: "test_action",
+ custom_fields: {
+ "action_code_who" => "<b>target_name</b>",
+ "action_code_path" => "/t/#{topic.id}",
+ },
+ )
+
+ get "/t/#{topic.slug}/#{topic.id}/#{action.post_number}.md"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include("Assigned @\\<b\\>target\\_name\\</b\\> to this & that on ")
+ ensure
+ I18n.reload!
+ end
+
+ it "renders action descriptions on whispers only for readers allowed to see them" do
+ action =
+ Fabricate(:whisper, topic:, user:, action_code: "closed.enabled", raw: "Private action")
+
+ get "/t/#{topic.slug}/#{topic.id}.md"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).not_to include(action.raw, "Closed on ")
+
+ SiteSetting.whispers_allowed_groups = Group::AUTO_GROUPS[:trust_level_2]
+ sign_in(user)
+
+ get "/t/#{topic.slug}/#{topic.id}/#{action.post_number}.md"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include("Closed on ", action.raw)
+ end
+
+ it "preserves the message when a small action code is absent or has no translation" do
+ [nil, "unknown_action"].each do |action_code|
+ action = Fabricate(:small_action, topic:, user:, action_code:, raw: "Custom action message")
+
+ get "/t/#{topic.slug}/#{topic.id}/#{action.post_number}.md"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include("</div>\n\n#{action.raw}\n\n---")
+ end
+ end
+
it "links topic and post authors to subfolder-safe user profiles" do
set_subfolder "/forum"
SiteSetting.external_system_avatars_url = ""