Add target=blank to links in formatted text via js
Some checks failed
/ Run tests (push) Failing after 15s
/ Run system tests (push) Failing after 14s
/ Build, push and deploy image (push) Has been skipped

This commit is contained in:
david 2024-10-26 20:57:41 +02:00
parent d90f1b86b8
commit 812c192910
5 changed files with 18 additions and 28 deletions

View file

@ -1,24 +0,0 @@
# frozen_string_literal: true
module RichTextTargetBlank
extend ActiveSupport::Concern
class_methods do
# Override has_rich_text to include target="_blank" functionality
def has_rich_text(name)
super # Call the original has_rich_text to set up the rich text association
# Define the before_save callback to modify the links
before_save do
next # FIXME: This adds additional newlines for each blank line.
rich_text_attribute = send(name)
if rich_text_attribute.present?
doc = Nokogiri::HTML::DocumentFragment.parse(rich_text_attribute.body.to_html)
doc.css("a").each { |a| a["target"] ||= "_blank" }
rich_text_attribute.body = doc.to_html
end
end
end
end
end