Fix links in status posts

This commit is contained in:
Daniel Nitsikopoulos
2024-10-19 19:33:07 +11:00
parent a2e7b33363
commit 1bee7ecc68
3 changed files with 57 additions and 6 deletions

View File

@@ -12,6 +12,8 @@ module Main
module Decorators module Decorators
module Posts module Posts
class Decorator < SimpleDelegator class Decorator < SimpleDelegator
REGEXP = URI::DEFAULT_PARSER.make_regexp
def syndicated? def syndicated?
!syndication_sources.empty? !syndication_sources.empty?
end end
@@ -111,6 +113,14 @@ module Main
photos? ? "<div>#{photos.map { |p| "<img src='#{p["value"]}'/>" }.join("")} #{content}</div>" : content photos? ? "<div>#{photos.map { |p| "<img src='#{p["value"]}'/>" }.join("")} #{content}</div>" : content
end end
def rendered_content
html_text = wrap_anchors_in_object_tags(replace_urls_with_anchors(content))
res = Sanitize.fragment(html_text,
elements: ["img", "p", "object", "a"],
attributes: {"img" => ["alt", "src", "title"], "a" => ["href"]})
res.gsub(prefix_emoji[0], "") if prefix_emoji
end
def raw_content def raw_content
res = Sanitize.fragment(content) res = Sanitize.fragment(content)
res.gsub(prefix_emoji[0], "") if prefix_emoji res.gsub(prefix_emoji[0], "") if prefix_emoji
@@ -174,6 +184,24 @@ module Main
private private
def replace_urls_with_anchors(text)
url_regex = %r{(?<!<a href="|img src="|video src=")(https?://[^\s]+)(?![^<>]*(</a>|/>))}
text.gsub(url_regex) do |url|
%(<object><a href="#{url}">#{url}</a></object>)
end
end
def wrap_anchors_in_object_tags(text)
# Regular expression to match <a> tags
anchor_regex = /(<a\s+[^>]*>.*?<\/a>)/
# Replace the matched anchor tags with <object> wrapped anchor tags
text.gsub(anchor_regex) do |anchor_tag|
%(<object>#{anchor_tag}</object>)
end
end
# e.g. geo:-37.75188,144.90417;u=35 # e.g. geo:-37.75188,144.90417;u=35
def geo def geo
loc = location.split(":")[1] loc = location.split(":")[1]
@@ -211,8 +239,8 @@ module Main
results << "</#{tag}>" results << "</#{tag}>"
end end
results results
rescue REXML::ParseException => e rescue REXML::ParseException
return "<p>No excerpt</p>" "<p>No excerpt</p>"
end end
def attrs_to_s(attrs) def attrs_to_s(attrs)

View File

@@ -10,13 +10,36 @@ module Main
module Decorators module Decorators
module Statuses module Statuses
class Decorator < Main::Decorators::Posts::Decorator class Decorator < Main::Decorators::Posts::Decorator
REGEXP = URI::DEFAULT_PARSER.make_regexp
def raw_content def raw_content
res = Sanitize.fragment(content, html_text = wrap_anchors_in_object_tags(replace_urls_with_anchors(content))
elements: ["img", "p"], res = Sanitize.fragment(html_text,
attributes: {"img" => ["alt", "src", "title"]}) elements: ["img", "p", "object", "a"],
attributes: {"img" => ["alt", "src", "title"], "a" => ["href"]})
res.gsub(prefix_emoji[0], "") if prefix_emoji res.gsub(prefix_emoji[0], "") if prefix_emoji
end end
private
def replace_urls_with_anchors(text)
url_regex = %r{(?<!<a href="|img src="|video src=")(https?://[^\s]+)(?![^<>]*(</a>|/>))}
text.gsub(url_regex) do |url|
%(<object><a href="#{url}">#{url}</a></object>)
end
end
def wrap_anchors_in_object_tags(text)
# Regular expression to match <a> tags
anchor_regex = /(<a\s+[^>]*>.*?<\/a>)/
# Replace the matched anchor tags with <object> wrapped anchor tags
text.gsub(anchor_regex) do |anchor_tag|
%(<object>#{anchor_tag}</object>)
end
end
end end
end end
end end

View File

@@ -42,7 +42,7 @@ div class="mb-8 h-entry border border-gray-200 m-2 p-4 bg-gray-50 hover:bg-gray-
video class="rounded w-max" autoplay=false loop=false muted=true controls=true video class="rounded w-max" autoplay=false loop=false muted=true controls=true
source type="video/mp4" src="#{video["value"]}" source type="video/mp4" src="#{video["value"]}"
div class="mb-8" div class="mb-8"
p class="text-sm text-blue-400 dark:text-indigo-400" p class="mt-4 text-sm text-blue-400 dark:text-indigo-400"
a class="u-url float-left mr-0 border-dotted border-b-2 border-blue-100 hover:border-blue-200 dark:border-indigo-900 dark:hover:border-indigo-800" href="#{post.permalink}" a class="u-url float-left mr-0 border-dotted border-b-2 border-blue-100 hover:border-blue-200 dark:border-indigo-900 dark:hover:border-indigo-800" href="#{post.permalink}"
time class="dt-published" datetime=post.machine_published_at time class="dt-published" datetime=post.machine_published_at
= post.display_published_at = post.display_published_at