Use the same extensions in all markdown renderers

We were using two different sets of extensions but, since the markdown
code is always written by administrators, IMHO it makes sense to be
consistent and always render markdown code the same way.
This commit is contained in:
Javi Martín
2023-06-29 17:25:27 +02:00
committed by karim-semmoud
parent 3faaa8521d
commit 79120209da
3 changed files with 13 additions and 16 deletions

View File

@@ -10,22 +10,24 @@ module ApplicationHelper
%i[ar fa he].include?(locale) %i[ar fa he].include?(locale)
end end
def markdown(text) def markdown(text, **render_options)
return text if text.blank? return text if text.blank?
render_options = { default_render_options = {
filter_html: false, filter_html: false,
hard_wrap: true, hard_wrap: true,
link_attributes: { target: "_blank" } link_attributes: { target: "_blank" }
} }
renderer = Redcarpet::Render::HTML.new(render_options) renderer = Redcarpet::Render::HTML.new(default_render_options.merge(render_options))
extensions = { extensions = {
autolink: true, autolink: true,
fenced_code_blocks: true, fenced_code_blocks: true,
lax_spacing: true, lax_spacing: true,
no_intra_emphasis: true, no_intra_emphasis: true,
strikethrough: true, strikethrough: true,
superscript: true superscript: true,
tables: true
} }
AdminLegislationSanitizer.new.sanitize(Redcarpet::Markdown.new(renderer, extensions).render(text)) AdminLegislationSanitizer.new.sanitize(Redcarpet::Markdown.new(renderer, extensions).render(text))

View File

@@ -22,15 +22,7 @@ class Legislation::DraftVersion < ApplicationRecord
scope :published, -> { where(status: "published").order("id DESC") } scope :published, -> { where(status: "published").order("id DESC") }
def body_html def body_html
render_options = { ApplicationController.helpers.markdown(body, with_toc_data: true)
with_toc_data: true
}
renderer = Redcarpet::Render::HTML.new(render_options)
extensions = {
tables: true
}
Redcarpet::Markdown.new(renderer, extensions).render(body)
end end
def toc_html def toc_html

View File

@@ -129,14 +129,16 @@ describe Legislation::DraftVersion do
<p>Something about this.</p> <p>Something about this.</p>
<table><thead> <table>
<thead>
<tr> <tr>
<th>id</th> <th>id</th>
<th>name</th> <th>name</th>
<th>age</th> <th>age</th>
<th>gender</th> <th>gender</th>
</tr> </tr>
</thead><tbody> </thead>
<tbody>
<tr> <tr>
<td>1</td> <td>1</td>
<td>Roberta</td> <td>Roberta</td>
@@ -149,7 +151,8 @@ describe Legislation::DraftVersion do
<td>25</td> <td>25</td>
<td>F</td> <td>F</td>
</tr> </tr>
</tbody></table> </tbody>
</table>
BODY_HTML BODY_HTML
end end