From f7302bc8fc2f47c5104861b60771411d1e6334bc 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 | 4 ++++ Gemfile.lock | 12 +++++++++++- plugins/messages/app/models/message.rb | 8 +++++++- plugins/messages/app/views/messages/show.haml | 2 +- .../views/messages_mailer/foodsoft_message.html.haml | 6 ++++++ 5 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 plugins/messages/app/views/messages_mailer/foodsoft_message.html.haml diff --git a/Gemfile b/Gemfile index 42ac26db..cd96ea12 100644 --- a/Gemfile +++ b/Gemfile @@ -60,6 +60,7 @@ gem 'mime-types' gem 'midi-smtp-server' gem 'rswag-api' gem 'rswag-ui' +gem 'maildown' # we use the git version of acts_as_versioned, and need to include it in this Gemfile gem 'acts_as_versioned', git: 'https://github.com/technoweenie/acts_as_versioned.git' @@ -75,6 +76,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 +128,5 @@ group :test do gem 'rswag-specs' gem 'hashie', '~> 3.4.6', require: false # https://github.com/westfieldlabs/apivore/issues/114 end + +gem "redcarpet", "~> 3.6" diff --git a/Gemfile.lock b/Gemfile.lock index f55e3397..26c19b62 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -283,6 +283,10 @@ GEM activerecord kaminari-core (= 1.2.2) kaminari-core (1.2.2) + kramdown (2.4.0) + rexml + kramdown-parser-gfm (1.1.0) + kramdown (~> 2.0) less (2.6.0) commonjs (~> 0.2.7) less-rails (5.0.0) @@ -308,6 +312,9 @@ GEM skinny (>= 0.1.2) sqlite3-ruby thin + maildown (3.3.1) + actionmailer (>= 4.0.0) + kramdown-parser-gfm marcel (1.0.2) matrix (0.4.2) method_source (1.0.0) @@ -413,6 +420,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) @@ -635,6 +643,7 @@ DEPENDENCIES listen mail (~> 2.7.1) mailcatcher + maildown midi-smtp-server mime-types mysql2 @@ -652,6 +661,7 @@ DEPENDENCIES rails_tokeninput ransack recurring_select! + redcarpet (~> 3.6) resque roo roo-xls @@ -686,4 +696,4 @@ DEPENDENCIES whenever BUNDLED WITH - 2.4.3 + 2.4.5 diff --git a/plugins/messages/app/models/message.rb b/plugins/messages/app/models/message.rb index f6b03c10..b936c0f9 100644 --- a/plugins/messages/app/models/message.rb +++ b/plugins/messages/app/models/message.rb @@ -132,9 +132,15 @@ class Message < ApplicationRecord user.role_admin? end + def render_markdown + markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true, strikethrough: true, footnotes: true) + return markdown.render(body) + end + private 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..975c7c09 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)) + = raw @message.render_markdown %hr/ %p = link_to t('.reply'), new_message_path(:message => {:reply_to => @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..48824acf --- /dev/null +++ b/plugins/messages/app/views/messages_mailer/foodsoft_message.html.haml @@ -0,0 +1,6 @@ += raw @message.render_markdown +%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