From 7c6f51f7caad66fb33707713ba46a16411445d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Sat, 30 Dec 2023 11:15:42 +0800 Subject: [PATCH] Fix relative links in the announcement contents. --- app/models/site_feed_annc.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/models/site_feed_annc.rb b/app/models/site_feed_annc.rb index f3339fe..95a3b96 100644 --- a/app/models/site_feed_annc.rb +++ b/app/models/site_feed_annc.rb @@ -182,6 +182,24 @@ class SiteFeedAnnc tmp["bulletin_files"] = files tmp["title"] = tmp["title_translations"][locale] tmp["subtitle"] = tmp["subtitle_translations"][locale] + if tmp["text_translations"].present? + tmp["text_translations"].each do |l, text| + doc = Nokogiri::HTML.fragment(text) + doc.css('[src]').each do |src_el| + src = (src_el.attributes["src"].value rescue "") + if src.present? && src.match(/^\/[^\/]+/) + src_el.attributes["src"].value = self.remote_site_url + src + end + end + doc.css('[href]').each do |href_el| + href = href_el.attribute('href').value rescue "" + if href.present? && href.match(/^\/[^\/]+/) + href.attributes["href"].value = self.remote_site_url + href + end + end + tmp["text_translations"][l] = doc.to_html + end + end tmp["source-site-link"] = tmp["source-site"] tmp["source-site"] = "#{tmp["source-site-title"]}" tmp["link_to_show"] = !tmp["external_link"].blank? ? tmp["external_link"] : nil