Relativize absolute links to the instance on export

This commit is contained in:
Stanko K.R.
2026-02-24 08:22:01 +01:00
parent 4eadce8ffe
commit 2f9d4533c5
2 changed files with 80 additions and 3 deletions
@@ -20,7 +20,7 @@ class Account::DataTransfer::ActionText::RichTextRecordSet < Account::DataTransf
end
def export_record(rich_text)
data = rich_text.as_json.merge("body" => convert_sgids_to_gids(rich_text.body))
data = rich_text.as_json.merge("body" => transform_body_for_export(rich_text.body))
zip.add_file "data/action_text_rich_texts/#{rich_text.id}.json", data.to_json
end
@@ -54,9 +54,14 @@ class Account::DataTransfer::ActionText::RichTextRecordSet < Account::DataTransf
check_associations_dont_exist(data)
end
def convert_sgids_to_gids(content)
def transform_body_for_export(content)
return nil if content.blank?
html = convert_sgids_to_gids(content)
relativize_urls(html)
end
def convert_sgids_to_gids(content)
content.send(:attachment_nodes).each do |node|
sgid = SignedGlobalID.parse(node["sgid"], for: ::ActionText::Attachable::LOCATOR_NAME)
@@ -75,6 +80,25 @@ class Account::DataTransfer::ActionText::RichTextRecordSet < Account::DataTransf
content.fragment.source.to_html
end
def relativize_urls(html)
host = Rails.application.routes.default_url_options[:host]
return html unless host
fragment = Nokogiri::HTML.fragment(html)
fragment.css("a[href]").each do |link|
uri = URI.parse(link["href"]) rescue nil
if uri.respond_to?(:host) && uri.host == host
link["href"] = uri.path
link["href"] += "?#{uri.query}" if uri.query
link["href"] += "##{uri.fragment}" if uri.fragment
end
end
fragment.to_html
end
def transform_body_for_import(body)
return body if body.blank?
@@ -112,7 +136,7 @@ class Account::DataTransfer::ActionText::RichTextRecordSet < Account::DataTransf
if match
path = match.post_match.presence || "/"
valid_path = Rails.application.routes.recognize_path(path) rescue nil
link["href"] = "#{account.slug}#{remaining_path}" if valid_path
link["href"] = "#{account.slug}#{path}" if valid_path
end
end