From e85aaadb63cd980b427b444d21c21c56a5142dc9 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 15 Feb 2023 23:50:48 +0100 Subject: [PATCH] Pass Markdown renderer options into renderer --- app/helpers/markdown_helper.rb | 30 +++++++++-------- config/initializers/redcarpet.rb | 55 +++++++++++++++++--------------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index 6d80f255..41c9bb8c 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -1,36 +1,40 @@ -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 + File.read relative_to.join(path) + rescue Errno::ENOENT + "# Error reading #{relative_to.join(path)}" end def markdown_io(path, relative_to = Rails.root) diff --git a/config/initializers/redcarpet.rb b/config/initializers/redcarpet.rb index aed5e5ff..79c26b1a 100644 --- a/config/initializers/redcarpet.rb +++ b/config/initializers/redcarpet.rb @@ -1,32 +1,37 @@ -require 'redcarpet/render_strip' +# frozen_string_literal: true + +require "redcarpet/render_strip" MARKDOWN_OPTS = { - filter_html: true, - escape_html: true, - no_images: true, - no_styles: true, - 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 -} + 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, + no_styles: true, + safe_links_only: true, + xhtml: false, + hard_wrap: true, +}.freeze RAW_MARKDOWN_OPTS = { - tables: true, - fenced_code_blocks: true, - autolink: true, + tables: true, + fenced_code_blocks: true, + autolink: true, disable_indented_code_blocks: true, - strikethrough: true, - superscript: false -} + strikethrough: true, + superscript: false, +}.freeze ALLOWED_HOSTS_IN_MARKDOWN = [ - APP_CONFIG['hostname'], - *APP_CONFIG['allowed_hosts_in_markdown'] -] \ No newline at end of file + APP_CONFIG["hostname"], + *APP_CONFIG["allowed_hosts_in_markdown"] +].freeze