Skip to content

Commit

Permalink
initial fix attempt for comment/answer markdown (#2605)
Browse files Browse the repository at this point in the history
* initial fix attempt

* additional tests for question comments and answer comments

* unit tests for comment/answer body_markdown

* test fix

* should resolve some tests

* fix note/node ref in test

* comma

* various test fixes

* functional test fixes

* one more functional test fix

* Update questions_controller_test.rb

* answer comment css selector

* test css nesting

* test css tweaks

* rearranging assert_select

* assert_select fixes

* Update _comment.html.erb

* Update _comment.html.erb

* Update notes_controller_test.rb

* Update notes_controller_test.rb

* Update notes_controller_test.rb

* Fix test for comment/answer markdown

* Clean up nesting

* Looks like a simpler solution

Courtesy of @sagarpreet-chadha

* Clean up

* Update notes_controller_test.rb
  • Loading branch information
jywarren authored Apr 11, 2018
1 parent af92c49 commit d37c7c0
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 14 deletions.
8 changes: 7 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ def insert_extras(body)
body
end

# we should move this to the Node model:
def render_map(lat, lon)
render partial: 'map/leaflet', locals: { lat: lat, lon: lon }
end

# we should move this to the Comment model:
# returns the comment body which is to be shown in the comments section
def render_comment_body(comment)
raw sanitize RDiscount.new(title_suggestion(comment)).to_html, attributes: %w(class style href data-method src)
raw RDiscount.new(
title_suggestion(comment),
:autolink
).to_html
end

# we should move this to the Comment model:
# replaces inline title suggestion(e.g: {New Title}) with the required link to change the title
def title_suggestion(comment)
comment.body.gsub(/\[propose:title\](.*?)\[\/propose\]/) do ||
Expand Down
4 changes: 4 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def body
finder = finder.gsub(Callouts.const_get(:HASHTAG), Callouts.const_get(:HASHLINKMD))
end

def body_markdown
RDiscount.new(body, :autolink).to_html
end

# users who like this answer
def likers
answer_selections
Expand Down
4 changes: 4 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def body
ApplicationController.helpers.emojify(finder)
end

def body_markdown
RDiscount.new(body, :autolink).to_html
end

def icon
"<i class='icon-comment'></i>"
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboard/_node_comment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<span><%= t('dashboard._node_comment.commented_on') %></span> <% unless node.aid == 0 %> an answer about<% end %> <% if node.parent.has_power_tag('question') %><a href="<%= node.parent.path(:question) %>"><% else %><a href="<%= node.parent.path %>"><% end %><%= node.parent.title %></a>
</p>

<p class="body"><%= raw strip_tags(sanitize(RDiscount.new(node.comment).to_html)).truncate(100) %></p>
<p class="body"><%= raw strip_tags(node.body_markdown).truncate(100) %></p>

<p class="meta">
<% if node.aid == 0 %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/notes/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="c<%= comment.cid %>">
<div id="c<%= comment.cid %>" class="comment">

<div class="navbar navbar-default">

Expand Down Expand Up @@ -64,7 +64,7 @@
</script>

<div id="c<%= comment.cid %>show">
<p><%= raw auto_link(render_comment_body(comment)) %></p>
<p class="comment-body" id="comment-body-<%= comment.cid %>"><%= raw render_comment_body(comment) %></p>
<% if comment.body.include?('?') %>
<p class="alert alert-info">Is this a question? <a href="/questions/new?title=<%= comment.body %>">Click here</a> to post it to the <a href="/questions">Questions page</a>.
</p>
Expand Down
2 changes: 1 addition & 1 deletion app/views/questions/_answer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</div>
</div>
<div id="c<%= answer.id %>show" style="padding:0px 20px">
<p><%= raw sanitize(auto_link(insert_extras(RDiscount.new(answer.body).to_html))) %></p>
<p><%= raw insert_extras(answer.body_markdown) %></p>
</div>
<% if current_user && params[:action]!="make_answer" %>
<%= render :partial => "comments/edit", :locals => { title: "Edit answer", comment: answer, placeholder: "Help users by posting an answer to this question" } %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/questions/_comment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% else %>
<% node = Answer.find(answer_id).node %>
<% end %>
<div id="answer-<%= answer_id %>-comment-<%= comment.id %>" class="answer-<%= answer_id %>-comments" style="padding-top: 10px;">
<div id="answer-<%= answer_id %>-comment-<%= comment.id %>" class="answer-<%= answer_id %>-comments comment answer-comment" style="padding-top: 10px;">
<div class="inline" style="vertical-align: top;">
<% if comment.author.user && comment.author.user.photo_file_name %>
<img class="img-circle" src="<%= comment.author.user.photo_path(:thumb) %>" />
Expand All @@ -15,8 +15,8 @@
<a href="/profile/<%= comment.author.name %>"><%= comment.author.name %></a>
<span class="hidden-xs">commented</span>
<a style="color:#aaa;" href="#answer-<%= answer_id %>-comment-<%= comment.id %>"><%= time_ago_in_words(comment.created_at) %> ago</a><br/>
<span class="content">
<%= raw sanitize(auto_link(insert_extras(RDiscount.new(comment.body).to_html))) %>
<span class="content question-comment-body" id="comment-body-<%= comment.cid %>">
<%= raw insert_extras(comment.body_markdown) %>
</span>
<div class="content-form" style="display: none;">
<form id="comment-<%= comment.id %>-edit-form" class="form-horizontal" action="/comment/update/<%= comment.id %>">
Expand Down
2 changes: 1 addition & 1 deletion app/views/tag/_tags.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% if tag.class == NodeTag %>

<% if power_tag ^ tag.name.include?(':') # XOR operator??? %>
<span id="tag_<%= tag.tid %>" class="label <%= label_name %>" style="cursor:pointer" data-toggle="popover" data-trigger="manual" data-count=0 data-placement="top" data-content="<p style='text-align:center;'><a href='/tag/<%= tag.name %>'><%= Tag.tagged_node_count(tag.name) || 0 %> notes</a> - <a href='/contributors/<%= tag.name %>'><%= Tag.contributors(tag.name).count %> people <br></a></p> <p style='text-align:center;font-size:12px;'><%if tag.description %><%= tag.description %> |<% end %> created by <a href='/profile/<%= tag.try(:author).try(:username) %>'><%= tag.try(:author).try(:username) %></a> <%= time_ago_in_words(Time.at(tag.date)) %> ago </p> <div style='text-align:center;'><a href='/subscribe/tag/<%= tag.name %>' class='btn btn-primary'>Follow</a></div>" data-html="true" title="<%= tag.name %>">
<span id="tag_<%= tag.tid %>" class="label <%= label_name %>" style="cursor:pointer" data-toggle="popover" data-trigger="focus" data-count=0 data-placement="top" data-content="<p style='text-align:center;'><a href='/tag/<%= tag.name %>'><%= Tag.tagged_node_count(tag.name) || 0 %> notes</a> - <a href='/contributors/<%= tag.name %>'><%= Tag.contributors(tag.name).count %> people <br></a></p> <p style='text-align:center;font-size:12px;'><%if tag.description %><%= tag.description %> |<% end %> created by <a href='/profile/<%= tag.try(:author).try(:username) %>'><%= tag.try(:author).try(:username) %></a> <%= time_ago_in_words(Time.at(tag.date)) %> ago </p> <div style='text-align:center;'><a href='/subscribe/tag/<%= tag.name %>' class='btn btn-primary'>Follow</a></div>" data-html="true" title="<%= tag.name %>">
<%= tag.name %>
<% if current_user && ( current_user.uid == @node.uid || current_user.uid == tag.uid || current_user.role == "admin" || current_user.role == "moderator") %>
<% if tag.name.include? ':' %>
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/drupal_content_type_map.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
map:
nid: 12
vid: 12
field_publication_date_value: 2017-12-07 22:35:05
field_capture_date_value: 2017-12-07 22:35:05
field_publication_date_value: <%= Time.now.to_s.slice(0, 19) %>
field_capture_date_value: <%= Time.now.to_s.slice(0, 19) %>
field_geotiff_url_value: publiclab.org
field_google_maps_url_value: map.google.com
field_openlayers_url_value: map.google.com
Expand Down
15 changes: 15 additions & 0 deletions test/functional/notes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ def teardown
assert_select '#other-activities', false
end

test 'comment markdown and autolinking works' do
node = Node.where(type: 'note', status: 1).first
assert node.comments.length > 0
comment = node.comments.last
comment.comment = 'Test **markdown** and http://links.com'
comment.save!

get :show, id: node.id

assert_select 'strong', 'markdown'
assert_select 'a', 'http://links.com'

assert_response :success
end

test 'notes record views with unique ips' do
note = nodes(:blog)
# clear impressions so we get a unique view
Expand Down
31 changes: 30 additions & 1 deletion test/functional/questions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,41 @@ def setup
assert_response :success
end

test 'show note by id' do
test 'show question by id' do
note = nodes(:question)
get :show, id: note.id
assert_response :success
end

test 'question comment markdown and autolinking works' do
node = nodes(:question)
assert node.comments.length.positive?
comment = node.comments.last
comment.comment = 'Test **markdown** and http://links.com'
comment.save!

get :show, id: node.id

assert_select 'strong', 'markdown'
assert_select 'a', 'http://links.com'
assert_response :success
end

test 'answer comment markdown and autolinking works' do
node = nodes(:question)
assert node.answers.first.comments.length.positive?
comment = node.answers.first.comments.last
comment.comment = 'Test **markdown2** and http://links2.com'
comment.save!

get :show, id: node.id

assert_select 'strong', 'markdown2'
assert_select 'a', 'http://links2.com'
assert_response :success
end


test 'should redirect notes other than question to note path' do
note = nodes(:one)
get :show,
Expand Down
4 changes: 3 additions & 1 deletion test/unit/answer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class AnswerTest < ActiveSupport::TestCase
end

test 'should save answer with a content' do
answer = Answer.new(content: 'A test answer')
answer = Answer.new(content: 'A test answer This has **markdown** and http://link.com')
assert answer.save
assert answer.body_markdown.match('<a href="http://link.com">http://link.com</a>')
assert answer.body_markdown.match('<strong>markdown</strong>')
end

test 'should relate answer to user and node' do
Expand Down
4 changes: 3 additions & 1 deletion test/unit/comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
class CommentTest < ActiveSupport::TestCase
test 'should save comment' do
comment = Comment.new
comment.comment = "My first thought is\n\nthat this is pretty good."
comment.comment = "My first thought is\n\nthat this is pretty good. **markdown** and http://link.com"
assert comment.save
assert comment.body_markdown.match('<a href="http://link.com">http://link.com</a>')
assert comment.body_markdown.match('<strong>markdown</strong>')
end

test 'comment mysql native fulltext search' do
Expand Down

0 comments on commit d37c7c0

Please sign in to comment.