From 441c68b8d73d36eece221e3d34c4ce967a3c551a Mon Sep 17 00:00:00 2001 From: Jason Zimdars Date: Thu, 30 Jan 2025 15:20:40 -0600 Subject: [PATCH] Add `data-turbo-frame` attribute to links in comments --- config/initializers/sanitization.rb | 1 + lib/markdown_renderer.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/config/initializers/sanitization.rb b/config/initializers/sanitization.rb index 8b602bcb4..a4e712142 100644 --- a/config/initializers/sanitization.rb +++ b/config/initializers/sanitization.rb @@ -1,3 +1,4 @@ Rails.application.config.after_initialize do Rails::HTML5::SafeListSanitizer.allowed_tags.merge(%w[ s table tr td th thead tbody details summary ]) + Rails::HTML5::SafeListSanitizer.allowed_attributes.merge(%w[ data-turbo-frame ]) end diff --git a/lib/markdown_renderer.rb b/lib/markdown_renderer.rb index 6f466434c..df9070bd5 100644 --- a/lib/markdown_renderer.rb +++ b/lib/markdown_renderer.rb @@ -36,9 +36,28 @@ class MarkdownRenderer < Redcarpet::Render::HTML block_code(code, language) # call Rouge Redcarpet plugin end + def link(url, title, content) + attributes = { href: url } + attributes[:title] = title if title + attributes["data-turbo-frame"] = "_top" + + "#{content}" + end + + def autolink(url, link_type) + attributes = { href: url } + attributes["data-turbo-frame"] = "_top" + + "#{url}" + end + private attr_reader :id_counts + def html_attributes(attributes) + attributes.map { |key, value| %Q(#{key}="#{value}") }.join(" ") + end + def unique_id(text) text.parameterize.then do |base_id| id_counts[base_id] += 1