Use redcarpet as markdown renderer

This commit is contained in:
Jose Farias
2024-12-18 16:37:33 -06:00
parent 0d2e4ed604
commit 0139f71bf2
5 changed files with 51 additions and 50 deletions
+2 -2
View File
@@ -10,7 +10,6 @@ gem "propshaft"
gem "stimulus-rails"
gem "turbo-rails"
gem "hotwire_combobox", github: "josefarias/hotwire_combobox", branch: :main
gem "house_md", bc: "house", branch: :renderer
# Deployment and drivers
gem "bootsnap", require: false
@@ -21,7 +20,8 @@ gem "thruster", require: false
# Features
gem "bcrypt", "~> 3.1.7"
gem "rqrcode"
gem "rouge", "~> 4.5"
gem "redcarpet"
gem "rouge"
gem "jbuilder"
# Telemetry
+3 -11
View File
@@ -1,12 +1,3 @@
GIT
remote: https://github.com/basecamp/house
revision: a8b19e70bbb95a7a0d27e38985a99c2d697a6650
branch: renderer
specs:
house_md (1.0.0)
rails
zeitwerk
GIT
remote: https://github.com/josefarias/hotwire_combobox.git
revision: 57ee6c9087320fab9383a07fa919fe75e0ea72ba
@@ -261,6 +252,7 @@ GEM
ffi (~> 1.0)
rdoc (6.8.1)
psych (>= 4.0.0)
redcarpet (3.6.0)
regexp_parser (2.9.3)
reline (0.5.12)
io-console (~> 0.5)
@@ -375,13 +367,13 @@ DEPENDENCIES
debug
hotwire-spark
hotwire_combobox!
house_md!
importmap-rails
jbuilder
propshaft
puma (>= 5.0)
rails!
rouge (~> 4.5)
redcarpet
rouge
rqrcode
rubocop-rails-omakase
selenium-webdriver
+1 -1
View File
@@ -1,4 +1,4 @@
ActiveSupport.on_load :action_text_markdown do
require "markdown_renderer"
ActionText::Markdown.html_renderer = -> { MarkdownRenderer.new }
ActionText::Markdown.html_renderer = ->(content) { MarkdownRenderer.build.render(content) }
end
+35 -34
View File
@@ -1,47 +1,48 @@
require "rouge/plugins/redcarpet"
class MarkdownRenderer < HouseMd::Renderer
class Generator < HouseMd::Generator::Html
include Rouge::Plugins::Redcarpet
class MarkdownRenderer < Redcarpet::Render::HTML
include Rouge::Plugins::Redcarpet
def initialize
@id_counts = Hash.new 0
end
def self.build
Redcarpet::Markdown.new MarkdownRenderer.new(filter_html: false),
autolink: true, highlight: true, no_intra_emphasis: true,
fenced_code_blocks: true, lax_spacing: true, strikethrough: true, tables: true
end
def header(text, header_level)
unique_id(text).then do |id|
<<~HTML.chomp
<h#{header_level} id="#{id}">
#{text} <a href="##{id}" class="heading__link" aria-hidden="true">#</a>
</h#{header_level}>
HTML
end
end
def initialize(...)
super
@id_counts = Hash.new 0
end
def image(url, alt_text)
def header(text, header_level)
unique_id(text).then do |id|
<<~HTML.chomp
<a href="#{url}" data-action="lightbox#open:prevent" data-lightbox-target="image" data-lightbox-url-value="#{url}?disposition=attachment">
<img src="#{url}" alt="#{alt_text}">
</a>
<h#{header_level} id="#{id}">
#{text} <a href="##{id}" class="heading__link" aria-hidden="true">#</a>
</h#{header_level}>
HTML
end
end
def code_block(code, language)
block_code(code, language) # call Rouge Redcarpet plugin
end
def image(url, title, alt_text)
<<~HTML.chomp
<a title="#{title}" href="#{url}" data-action="lightbox#open:prevent" data-lightbox-target="image" data-lightbox-url-value="#{url}?disposition=attachment">
<img src="#{url}" alt="#{alt_text}">
</a>
HTML
end
private
attr_reader :id_counts
def code_block(code, language)
block_code(code, language) # call Rouge Redcarpet plugin
end
def unique_id(text)
text.parameterize.then do |base_id|
id_counts[base_id] += 1
id_counts[base_id] > 1 ? "#{base_id}-#{id_counts[base_id]}" : base_id
end
private
attr_reader :id_counts
def unique_id(text)
text.parameterize.then do |base_id|
id_counts[base_id] += 1
id_counts[base_id] > 1 ? "#{base_id}-#{id_counts[base_id]}" : base_id
end
end
def initialize
@generator = Generator.new
end
end
end
+10 -2
View File
@@ -10,11 +10,19 @@ module ActionText
end
def to_unsafe_html
(html_renderer.try(:call) || html_renderer).render(content)
if html_renderer.respond_to? :call
html_renderer.call content
else
html_renderer.render content
end
end
def to_plain_text
(plain_renderer.try(:call) || plain_renderer).render(content)
if plain_renderer.respond_to? :call
plain_renderer.call content
else
plain_renderer.render content
end
end
end
end