From 1419827f66544e22b33aab07702da954e6ba7c67 Mon Sep 17 00:00:00 2001 From: Philipp Rothmann Date: Mon, 30 Jan 2023 13:14:37 +0100 Subject: [PATCH] feat(messages): render markdown in message body uses redcarpet gem to render markdown to html. Also a html email template is added. --- Gemfile | 3 +++ Gemfile.lock | 4 +++- app/helpers/application_helper.rb | 22 +++++++++++++++++++ plugins/messages/app/models/message.rb | 2 +- plugins/messages/app/views/messages/show.haml | 2 +- .../messages/app/views/messages/thread.haml | 2 +- .../foodsoft_message.html.haml | 6 +++++ 7 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 plugins/messages/app/views/messages_mailer/foodsoft_message.html.haml diff --git a/Gemfile b/Gemfile index 42ac26db..82c2ea2a 100644 --- a/Gemfile +++ b/Gemfile @@ -75,6 +75,7 @@ gem 'foodsoft_polls', path: 'plugins/polls' # gem 'foodsoft_printer', path: 'plugins/printer' # gem 'foodsoft_uservoice', path: 'plugins/uservoice' + group :development do gem 'sqlite3', '~> 1.3.6' gem 'mailcatcher' @@ -126,3 +127,5 @@ group :test do gem 'rswag-specs' gem 'hashie', '~> 3.4.6', require: false # https://github.com/westfieldlabs/apivore/issues/114 end + +gem "redcarpet" diff --git a/Gemfile.lock b/Gemfile.lock index f55e3397..f4d3ed09 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -413,6 +413,7 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) + redcarpet (3.6.0) redis (5.0.5) redis-client (>= 0.9.0) redis-client (0.11.2) @@ -652,6 +653,7 @@ DEPENDENCIES rails_tokeninput ransack recurring_select! + redcarpet resque roo roo-xls @@ -686,4 +688,4 @@ DEPENDENCIES whenever BUNDLED WITH - 2.4.3 + 2.4.5 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de207901..2f281022 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -263,4 +263,26 @@ module ApplicationHelper stylesheet_link_tag foodcoop_css_path, media: 'all' end end + + # renders html from markdown input + def markdown(text) + options = { + filter_html: true, + hard_wrap: true, + link_attributes: { rel: 'nofollow', target: "_blank" }, + fenced_code_blocks: true + } + extensions = { + autolink: true, + superscript: true, + disable_indented_code_blocks: true, + tables: true, + strikethrough: true, + footnotes: true + } + + renderer = ::Redcarpet::Render::HTML.new(options) + markdown = ::Redcarpet::Markdown.new(renderer, extensions) + markdown.render(text).html_safe + end end diff --git a/plugins/messages/app/models/message.rb b/plugins/messages/app/models/message.rb index f6b03c10..08913d8d 100644 --- a/plugins/messages/app/models/message.rb +++ b/plugins/messages/app/models/message.rb @@ -137,4 +137,4 @@ class Message < ApplicationRecord def create_salt self.salt = [Array.new(6) { rand(256).chr }.join].pack("m").chomp end -end +end \ No newline at end of file diff --git a/plugins/messages/app/views/messages/show.haml b/plugins/messages/app/views/messages/show.haml index 36e7b570..7a845c68 100644 --- a/plugins/messages/app/views/messages/show.haml +++ b/plugins/messages/app/views/messages/show.haml @@ -33,7 +33,7 @@ - if @message.can_toggle_private?(current_user) = link_to t('.change_visibility'), toggle_private_message_path(@message), method: :post, class: 'btn btn-mini' %hr/ - %p= simple_format(h(@message.body)) + = markdown(@message.body) %hr/ %p = link_to t('.reply'), new_message_path(:message => {:reply_to => @message.id}), class: 'btn' diff --git a/plugins/messages/app/views/messages/thread.haml b/plugins/messages/app/views/messages/thread.haml index 4bc36bcb..181bbd4a 100644 --- a/plugins/messages/app/views/messages/thread.haml +++ b/plugins/messages/app/views/messages/thread.haml @@ -9,7 +9,7 @@ .panel-heading %b= h(message.sender_name) = format_time(message.created_at) - .panel-body= simple_format(h(message.body)) + .panel-body= markdown(message.body) %p = link_to t('.reply'), new_message_path(:message => {:reply_to => thread_message.id}), class: 'btn' diff --git a/plugins/messages/app/views/messages_mailer/foodsoft_message.html.haml b/plugins/messages/app/views/messages_mailer/foodsoft_message.html.haml new file mode 100644 index 00000000..4577c76e --- /dev/null +++ b/plugins/messages/app/views/messages_mailer/foodsoft_message.html.haml @@ -0,0 +1,6 @@ += markdown(@message.body) +%hr/ +%pre + - if @message.group + = raw t '.footer_group', group: @message.group.name + = raw t '.footer', reply_url: new_message_url('message[reply_to]' => @message.id), msg_url: message_url(@message), profile_url: my_profile_url