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
+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