From 02a5e7029e55d3b5d6a91a8d1949ca4a2199b35d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 30 Dec 2021 18:13:52 +0100 Subject: [PATCH] Add linkfilter logic to existing renderers --- app/helpers/markdown_helper.rb | 5 +++++ app/services/flavored_markdown.rb | 1 + app/services/shared_markers.rb | 9 +++++++++ config/initializers/redcarpet.rb | 5 +++++ config/justask.yml.example | 7 +++++++ 5 files changed, 27 insertions(+) create mode 100644 app/services/shared_markers.rb diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index bc49c89b..23af88a1 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -15,6 +15,11 @@ module MarkdownHelper CGI.unescape_html(Sanitize.fragment(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 + end + def raw_markdown(content) md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, RAW_MARKDOWN_OPTS) raw md.render content diff --git a/app/services/flavored_markdown.rb b/app/services/flavored_markdown.rb index a356d07e..0110954f 100644 --- a/app/services/flavored_markdown.rb +++ b/app/services/flavored_markdown.rb @@ -1,5 +1,6 @@ class FlavoredMarkdown < Redcarpet::Render::HTML include Rails.application.routes.url_helpers + include SharedMarkers def preprocess(text) wrap_mentions(text) diff --git a/app/services/shared_markers.rb b/app/services/shared_markers.rb new file mode 100644 index 00000000..556ce655 --- /dev/null +++ b/app/services/shared_markers.rb @@ -0,0 +1,9 @@ +module SharedMarkers + def autolink(link, _link_type) + if ALLOWED_HOSTS.include? URI(link).host + return "#{link}" + end + + "#{link}" + end +end \ No newline at end of file diff --git a/config/initializers/redcarpet.rb b/config/initializers/redcarpet.rb index da4a6a8e..031dcb13 100644 --- a/config/initializers/redcarpet.rb +++ b/config/initializers/redcarpet.rb @@ -25,3 +25,8 @@ RAW_MARKDOWN_OPTS = { strikethrough: true, superscript: false } + +ALLOWED_HOSTS = [ + APP_CONFIG['hostname'], + *APP_CONFIG['allowed_hosts'] +] \ No newline at end of file diff --git a/config/justask.yml.example b/config/justask.yml.example index 2d2d2f64..6a112f8e 100644 --- a/config/justask.yml.example +++ b/config/justask.yml.example @@ -73,5 +73,12 @@ accepted_image_formats: - image/gif - .gif +# This list controls which hosts are excempt from the linkfilter +# Note: `hostname` is always included by default +allowed_hosts: + - twitter.com + - youtube.com + - youtu.be + # Sentry connection string sentry_dsn: ''