Pass Markdown renderer options into renderer

This commit is contained in:
Karina Kwiatek 2023-02-15 23:50:48 +01:00
parent c44ea79cae
commit e85aaadb63
2 changed files with 47 additions and 38 deletions

View File

@ -1,37 +1,41 @@
module MarkdownHelper
# frozen_string_literal: true
module MarkdownHelper
def markdown(content)
md = Redcarpet::Markdown.new(FlavoredMarkdown, MARKDOWN_OPTS)
Sanitize.fragment(md.render(content), EVIL_TAGS).html_safe
renderer = FlavoredMarkdown.new(**MARKDOWN_RENDERER_OPTS)
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
Sanitize.fragment(md.render(content), EVIL_TAGS).strip.html_safe
end
def strip_markdown(content)
md = Redcarpet::Markdown.new(Redcarpet::Render::StripDown, MARKDOWN_OPTS)
renderer = Redcarpet::Render::StripDown.new
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
CGI.unescape_html(Sanitize.fragment(CGI.escape_html(md.render(content)), EVIL_TAGS)).strip
end
def twitter_markdown(content)
md = Redcarpet::Markdown.new(TwitteredMarkdown, MARKDOWN_OPTS)
renderer = TwitteredMarkdown.new
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
CGI.unescape_html(Sanitize.fragment(CGI.escape_html(md.render(content)), EVIL_TAGS)).strip
end
def question_markdown(content)
md = Redcarpet::Markdown.new(QuestionMarkdown.new, MARKDOWN_OPTS)
Sanitize.fragment(md.render(content), EVIL_TAGS).html_safe
renderer = QuestionMarkdown.new
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
Sanitize.fragment(md.render(content), EVIL_TAGS).strip.html_safe
end
def raw_markdown(content)
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, RAW_MARKDOWN_OPTS)
renderer = Redcarpet::Render::HTML.new(MARKDOWN_RENDERER_OPTS)
md = Redcarpet::Markdown.new(renderer, RAW_MARKDOWN_OPTS)
raw md.render content
end
def get_markdown(path, relative_to = Rails.root)
begin
File.read relative_to.join(path)
rescue Errno::ENOENT
"# Error reading #{relative_to.join(path)}"
end
end
def markdown_io(path, relative_to = Rails.root)
markdown get_markdown path, relative_to

View File

@ -1,6 +1,18 @@
require 'redcarpet/render_strip'
# frozen_string_literal: true
require "redcarpet/render_strip"
MARKDOWN_OPTS = {
no_intra_emphasis: true,
tables: true,
fenced_code_blocks: true,
autolink: true,
disable_indented_code_blocks: true,
strikethrough: true,
superscript: false,
}.freeze
MARKDOWN_RENDERER_OPTS = {
filter_html: true,
escape_html: true,
no_images: true,
@ -8,14 +20,7 @@ MARKDOWN_OPTS = {
safe_links_only: true,
xhtml: false,
hard_wrap: true,
no_intra_emphasis: true,
tables: true,
fenced_code_blocks: true,
autolink: true,
disable_indented_code_blocks: true,
strikethrough: true,
superscript: false
}
}.freeze
RAW_MARKDOWN_OPTS = {
tables: true,
@ -23,10 +28,10 @@ RAW_MARKDOWN_OPTS = {
autolink: true,
disable_indented_code_blocks: true,
strikethrough: true,
superscript: false
}
superscript: false,
}.freeze
ALLOWED_HOSTS_IN_MARKDOWN = [
APP_CONFIG['hostname'],
*APP_CONFIG['allowed_hosts_in_markdown']
]
APP_CONFIG["hostname"],
*APP_CONFIG["allowed_hosts_in_markdown"]
].freeze