Skip to content

Commit 00f1507

Browse files
committed
Render comments markdown
1 parent 2a2cf4d commit 00f1507

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ gem 'puma'
5555
# Ruby on Rails framework
5656
gem 'rails'
5757

58+
# Markdown renderer
59+
gem 'redcarpet'
60+
5861
# Background job library on top of Redis
5962
gem 'resque'
6063

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ GEM
241241
ffi (~> 1.0)
242242
rdoc (6.6.3.1)
243243
psych (>= 4.0.0)
244+
redcarpet (3.6.0)
244245
redis (5.0.7)
245246
redis-client (>= 0.9.0)
246247
redis-client (0.16.0)
@@ -388,6 +389,7 @@ DEPENDENCIES
388389
pg
389390
puma
390391
rails
392+
redcarpet
391393
resque
392394
rspec-rails
393395
rubocop

app/models/comment.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Comment < ApplicationRecord
88

99
default_scope { order(:id) }
1010

11-
auto_strip_attributes :text, squish: true
11+
auto_strip_attributes :text
1212

1313
def formatted_date
1414
created_at.strftime('%e %b @ %H:%M').squish

app/views/comments/_form.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
<%= f.hidden_field :home_id, value: comment.home_id %>
55

66
<div class="input-group">
7-
<%= f.text_field :text,
7+
<%= f.text_area :text,
88
class: 'form-control',
99
placeholder: ['I think...', 'Love this!', 'What about ...', 'No. Way.', 'Ermahgerd!'].sample,
1010
required: true,
11+
rows: comment.new_record? ? 3 : 10,
1112
autocomplete: 'off' %>
1213
<div class="input-group-append">
1314
<%= f.submit comment.new_record? ? 'Comment' : 'Update',

app/views/comments/_list.html.erb

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true) %>
12
<% if comments.each do |comment| %>
23
<div class="media mt-2 mb-2 comment">
34
<%= image_tag comment.user.gravatar_url(70),
@@ -8,19 +9,19 @@
89
<%= comment.user.display_name %> :
910
<small>
1011
<%= comment.formatted_date %>
12+
<% if comment.user == current_user %>
13+
<span class="manage-comment-controls">
14+
<%= link_to fa_icon_inline('edit', type: :duotone),
15+
edit_comment_path(comment) %>
16+
<%= link_to fa_icon_inline('times', type: :duotone),
17+
comment_path(comment),
18+
method: :delete,
19+
data: { confirm: 'Really delete comment?' } %>
20+
</span>
21+
<% end %>
1122
</small>
1223
</h5>
13-
<%= comment.text %>
14-
<% if comment.user == current_user %>
15-
<span class="manage-comment-controls">
16-
<%= link_to fa_icon_inline('edit', type: :duotone),
17-
edit_comment_path(comment) %>
18-
<%= link_to fa_icon_inline('times', type: :duotone),
19-
comment_path(comment),
20-
method: :delete,
21-
data: { confirm: 'Really delete comment?' } %>
22-
</span>
23-
<% end %>
24+
<%= markdown.render(comment.text).html_safe %>
2425
</div>
2526
</div>
2627
<% end.empty? %>

0 commit comments

Comments
 (0)