discourse/lib/quote_rewriter.rb
Régis Hanol 255c1dfaf3
FIX: Preserve display name in quotes when using the rich text editor (#38078)
When `display_name_on_posts` is enabled and `prioritize_username_in_ux`
is disabled, quoting a post in markdown mode correctly attributes the
quote to the user's full name (e.g. `[quote="Robin Ward, post:1,
topic:2, username:eviltrout"]`). However, the rich text editor lost the
display name during the markdown→ProseMirror→markdown round-trip,
falling back to just the username.

The ProseMirror quote node only stored `username` in its attributes.
When BBCode with a `username:` param was parsed, the display name was
discarded. On serialization, only the username survived.

This commit:
- Adds a `displayName` attribute to the ProseMirror quote node spec
- Emits `data-display-name` on the markdown-it aside token so the
ProseMirror parser can pick it up
- Updates the serializer to output the display name and `username:`
param when present
- Fixes a latent issue where `displayName` remained as an array instead
of being joined into a string when the name contains no commas

https://meta.discourse.org/t/397049/12
2026-02-26 21:04:07 +01:00

86 lines
2.7 KiB
Ruby

# frozen_string_literal: true
class QuoteRewriter
def initialize(user_id)
@user_id = user_id
end
def rewrite_raw_username(raw, old_username, new_username)
escaped_old_username = Regexp.escape(old_username)
pattern =
Regexp.union(
/(?<pre>\[quote\s*=\s*["'']?.*username:)#{escaped_old_username}(?<post>\,?[^\]]*\])/i,
/(?<pre>\[quote\s*=\s*["'']?)#{escaped_old_username}(?<post>\,?[^\]]*\])/i,
)
raw.gsub(pattern, "\\k<pre>#{new_username}\\k<post>")
end
def rewrite_cooked_username(cooked, old_username, new_username, avatar_img)
formatted_old_username = PrettyText::Helpers.format_username(old_username)
escaped_old_username = Regexp.escape(formatted_old_username)
pattern = /(?<=\s)#{escaped_old_username}(?=:)/i
cooked
.css("aside.quote")
.each do |aside|
next unless div = aside.at_css("div.title")
username_replaced = false
aside["data-username"] = new_username if aside["data-username"] == old_username
div.children.each do |child|
if child.text?
content = child.content
username_replaced = content.gsub!(pattern, new_username).present?
child.content = content if username_replaced
end
end
if username_replaced || quotes_correct_user?(aside)
div.at_css("img.avatar")&.replace(avatar_img)
end
end
end
def rewrite_raw_display_name(raw, old_display_name, new_display_name)
escaped_old_display_name = Regexp.escape(old_display_name)
pattern =
/(?<pre>\[quote\s*=\s*["'']?)#{escaped_old_display_name}(?<post>\,[^\]]*username[^\]]*\])/i
raw.gsub(pattern, "\\k<pre>#{new_display_name}\\k<post>")
end
def rewrite_cooked_display_name(cooked, old_display_name, new_display_name)
formatted_old_display_name = PrettyText::Helpers.format_username(old_display_name)
escaped_old_display_name = Regexp.escape(formatted_old_display_name)
pattern = /(?<=\s)#{escaped_old_display_name}(?=:)/i
cooked
.css("aside.quote")
.each do |aside|
next unless div = aside.at_css("div.title")
if aside["data-display-name"] == old_display_name
aside["data-display-name"] = new_display_name
end
div.children.each do |child|
if child.text?
content = child.content
display_name_replaced = content.gsub!(pattern, new_display_name).present?
child.content = content if display_name_replaced
end
end
end
end
private
attr_reader :user_id
def quotes_correct_user?(aside)
Post.exists?(topic_id: aside["data-topic"], post_number: aside["data-post"], user_id: user_id)
end
end