From 64568d3a3d3a68fb4556801c964187194a6a3abf Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Thu, 9 Dec 2021 14:08:54 -0600 Subject: [PATCH] Use the rss item's url when handling relative image links (#15497) * Use the feed source url when the feed url (from links) is empty This probably should just be using @feed_source_url always to make a base path for absolute paths without a hostname, rather than expecting this from the feed. An example why we should not be using @feed.url would be individual posts at domain/slug but the "index" page at domain/blog (with a generic screen on /), and images hosted from /assets/ without /blog/ anywhere in the asset path. Partial fix for #15488 * Use item.url as base_url for relative links It's possible the rss feed and the contained articles are served from separate hosts/subdomains/paths. Don't assume the entries are related to the feed or the feed_source_url, use the item's url for relative links in that item. * Update failing spec to add url to item double --- app/services/feeds/assemble_article_markdown.rb | 9 ++++++++- spec/services/feeds/assemble_article_markdown_spec.rb | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/services/feeds/assemble_article_markdown.rb b/app/services/feeds/assemble_article_markdown.rb index efab42335..0aebd5817 100644 --- a/app/services/feeds/assemble_article_markdown.rb +++ b/app/services/feeds/assemble_article_markdown.rb @@ -31,6 +31,13 @@ module Feeds private + def base_url + @base_url ||= + URI.parse(@item.url).then do |uri| + "#{uri.scheme}://#{uri.host}" + end + end + def processed_title @title.truncate(128, omission: "...", separator: " ") end @@ -43,7 +50,7 @@ module Feeds def assemble_body_markdown cleaned_content = Feeds::CleanHtml.call(get_content) - cleaned_content = thorough_parsing(cleaned_content, @feed.url) + cleaned_content = thorough_parsing(cleaned_content, base_url) content = ReverseMarkdown .convert(cleaned_content, github_flavored: true) diff --git a/spec/services/feeds/assemble_article_markdown_spec.rb b/spec/services/feeds/assemble_article_markdown_spec.rb index 04a44d1b1..d58d891a0 100644 --- a/spec/services/feeds/assemble_article_markdown_spec.rb +++ b/spec/services/feeds/assemble_article_markdown_spec.rb @@ -18,6 +18,7 @@ RSpec.describe Feeds::AssembleArticleMarkdown, type: :service do categories: %w[tag1 tag2 tag3 tag4 tag5], published: "2020-12-20", content: content, + url: "https://feed.source", ) end