From 1d93092f8a9d20b1947560125db3a51e3c402fa1 Mon Sep 17 00:00:00 2001 From: rodolfobandeira Date: Mon, 11 Oct 2021 00:48:37 -0400 Subject: [PATCH] Add Kramdown for events description --- Gemfile | 4 ++-- Gemfile.lock | 3 +++ app/helpers/application_helper.rb | 10 ++++++++++ app/views/events/_list.html.erb | 2 +- app/views/events/show.html.erb | 8 +++++--- spec/factories/events.rb | 2 +- spec/helpers/application_helper_spec.rb | 24 ++++++++++++++++++++++++ spec/requests/events_spec.rb | 8 ++++++++ 8 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 spec/helpers/application_helper_spec.rb diff --git a/Gemfile b/Gemfile index 54b3c04..4bad413 100644 --- a/Gemfile +++ b/Gemfile @@ -9,8 +9,8 @@ ruby "3.0.1" gem "rails", "~> 6.1.3", ">= 6.1.3.2" gem "devise" # Use to authenticate user -# Use postgresql as the database for Active Record -gem "pg", "~> 1.1" +gem "kramdown", "~> 2.3", ">= 2.3.1" +gem "pg", "~> 1.1" # Use postgresql as the database for Active Record gem "puma", "~> 5.0" # Use Puma as the app server gem "pundit" # use pundit to control app premissions and policies gem "sass-rails", ">= 6" # Use SCSS for stylesheets diff --git a/Gemfile.lock b/Gemfile.lock index 8c453b9..f4d57ba 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -101,6 +101,8 @@ GEM concurrent-ruby (~> 1.0) jbuilder (2.11.2) activesupport (>= 5.0.0) + kramdown (2.3.1) + rexml listen (3.5.1) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) @@ -273,6 +275,7 @@ DEPENDENCIES factory_bot factory_bot_rails jbuilder (~> 2.7) + kramdown (~> 2.3, >= 2.3.1) listen (~> 3.3) pg (~> 1.1) pry diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 68ed09d..c4230ea 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -38,4 +38,14 @@ def alert_color return "is-success" if notice "is-primary" end + + # Helper method to convert Markwodn to HTML in the views + def kramdown(text) + sanitize Kramdown::Document.new(text).to_html + end + + # Converts to html and then that removes all html tags in order to get pure text + def kramdown_pure_text(text) + sanitize kramdown(text), { tags: [] } + end end diff --git a/app/views/events/_list.html.erb b/app/views/events/_list.html.erb index 9ef92dd..c8e6129 100644 --- a/app/views/events/_list.html.erb +++ b/app/views/events/_list.html.erb @@ -11,7 +11,7 @@ <% events.each do |event| %> <%= link_to event, event %> - <%= event.description %> + <%= kramdown_pure_text(event.description) %> <%= date_range(event) %> <%= buttons(event) %> diff --git a/app/views/events/show.html.erb b/app/views/events/show.html.erb index a29100e..a4f282a 100644 --- a/app/views/events/show.html.erb +++ b/app/views/events/show.html.erb @@ -4,10 +4,12 @@

<%= @event.name %>

<%= date_range(@event) %>

- <%= @event.description %> + <%= kramdown(@event.description) %>

- <%= buttons(@event, include_nav: true) %> +

+ <%= buttons(@event, include_nav: true) %> +

@@ -16,7 +18,7 @@ <%= button_to "Not Attending", { controller: :event_attendees, action: :destroy, - id: current_user&.profile&.event_attendee(@event)&.first&.id }, + id: current_user&.profile&.event_attendee(@event)&.first&.id }, method: :delete, class: "button is-danger" %> <% elsif current_user %> diff --git a/spec/factories/events.rb b/spec/factories/events.rb index ab2dd59..f7d29f5 100644 --- a/spec/factories/events.rb +++ b/spec/factories/events.rb @@ -3,7 +3,7 @@ FactoryBot.define do factory :event do name { "RubyConf" } - description { "[RubyConf 2020](https://rubyconf.org/) will be held in Denver." } + description { "[RubyConf 2020](https://rubyconf.org/) will be held in `Denver`." } start_at { "2021-11-08 00:00:00" } end_at { "2021-11-10 00:00:00" } diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 0000000..b1f0924 --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe ApplicationHelper do + context "when kramdown is used to convert markdown to html" do + it "if the inline code markdown code is passed" do + markdown_source = "`inline code`" + expect(kramdown(markdown_source)).to eq "

inline code

\n" + end + + it "if the multiline code markdown code is passed" do + markdown_source = "* list" + expect(kramdown(markdown_source)).to eq "\n" + end + end + + context "when kramdown_pure_text is used to remove any markdown code" do + it "if the inline code markdown code is passed" do + markdown_source = "`inline code`" + expect(kramdown_pure_text(markdown_source)).to eq "inline code\n" + end + end +end diff --git a/spec/requests/events_spec.rb b/spec/requests/events_spec.rb index c67927d..8fbb5cf 100644 --- a/spec/requests/events_spec.rb +++ b/spec/requests/events_spec.rb @@ -24,6 +24,14 @@ let(:event) { create :event } let(:format) { :html } + context "when description has markdown" do + it "converts markdown to html" do + get event_url(event) + expected_str = 'RubyConf 2020 will be held in Denver.' + expect(response.body).to include(expected_str) + end + end + context "when format is html" do let(:format) { :html }