2023-02-15 23:50:48 +01:00
|
|
|
# frozen_string_literal: true
|
2014-12-28 19:06:49 +01:00
|
|
|
|
2023-02-15 23:50:48 +01:00
|
|
|
module MarkdownHelper
|
2014-12-28 19:06:49 +01:00
|
|
|
def markdown(content)
|
2023-02-15 23:50:48 +01:00
|
|
|
renderer = FlavoredMarkdown.new(**MARKDOWN_RENDERER_OPTS)
|
|
|
|
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
|
2023-02-16 00:00:04 +01:00
|
|
|
# As the string has been sanitized we can mark it as HTML safe
|
|
|
|
Sanitize.fragment(md.render(content), EVIL_TAGS).strip.html_safe # rubocop:disable Rails/OutputSafety
|
2014-12-28 19:06:49 +01:00
|
|
|
end
|
2015-01-12 13:00:00 +01:00
|
|
|
|
|
|
|
def strip_markdown(content)
|
2023-02-15 23:50:48 +01:00
|
|
|
renderer = Redcarpet::Render::StripDown.new
|
|
|
|
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
|
2022-07-30 19:41:47 +02:00
|
|
|
CGI.unescape_html(Sanitize.fragment(CGI.escape_html(md.render(content)), EVIL_TAGS)).strip
|
2015-01-12 13:00:00 +01:00
|
|
|
end
|
2015-01-29 16:45:58 +01:00
|
|
|
|
|
|
|
def twitter_markdown(content)
|
2023-02-15 23:50:48 +01:00
|
|
|
renderer = TwitteredMarkdown.new
|
|
|
|
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
|
2022-07-30 19:41:47 +02:00
|
|
|
CGI.unescape_html(Sanitize.fragment(CGI.escape_html(md.render(content)), EVIL_TAGS)).strip
|
2015-01-29 16:45:58 +01:00
|
|
|
end
|
2015-05-24 14:48:54 +02:00
|
|
|
|
2021-12-30 18:13:52 +01:00
|
|
|
def question_markdown(content)
|
2023-02-15 23:50:48 +01:00
|
|
|
renderer = QuestionMarkdown.new
|
|
|
|
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
|
2023-02-16 00:00:04 +01:00
|
|
|
# As the string has been sanitized we can mark it as HTML safe
|
|
|
|
Sanitize.fragment(md.render(content), EVIL_TAGS).strip.html_safe # rubocop:disable Rails/OutputSafety
|
2021-12-30 18:13:52 +01:00
|
|
|
end
|
|
|
|
|
2015-05-24 14:48:54 +02:00
|
|
|
def raw_markdown(content)
|
2023-02-18 20:19:31 +01:00
|
|
|
renderer = Redcarpet::Render::HTML.new(**MARKDOWN_RENDERER_OPTS)
|
|
|
|
md = Redcarpet::Markdown.new(renderer, **MARKDOWN_OPTS)
|
2023-10-18 23:26:10 +02:00
|
|
|
raw md.render content # rubocop:disable Rails/OutputSafety
|
2015-05-24 14:48:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_markdown(path, relative_to = Rails.root)
|
2023-02-15 23:50:48 +01:00
|
|
|
File.read relative_to.join(path)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
"# Error reading #{relative_to.join(path)}"
|
2015-05-24 14:48:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def markdown_io(path, relative_to = Rails.root)
|
|
|
|
markdown get_markdown path, relative_to
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_markdown_io(path, relative_to = Rails.root)
|
|
|
|
strip_markdown get_markdown path, relative_to
|
|
|
|
end
|
|
|
|
|
|
|
|
def twitter_markdown_io(path, relative_to = Rails.root)
|
|
|
|
twitter_markdown get_markdown path, relative_to
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_markdown_io(path, relative_to = Rails.root)
|
|
|
|
raw_markdown get_markdown path, relative_to
|
|
|
|
end
|
|
|
|
end
|