Skip to content

Commit 2ec436d

Browse files
committed
Simplify markdown preview
- Do not let unauthenticated people render a markdown preview - Remove Haml::Filters monkey patch, it just replaces some text. Do that in a MarkdownHelper instead. - Do not render markdown with MarkdownHelper, use a haml partial instead
1 parent 65c0fa5 commit 2ec436d

16 files changed

+53
-119
lines changed
+1-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
class MarkdownController < ApplicationController
2-
skip_before_action :authenticate_user!, only: [:preview]
3-
respond_to :js
4-
52
def preview
6-
if params[:source]
7-
markdown_source = params[:source].to_str.gsub(/(?<=^|\s):([\w+-]+):(?=\s|$)/) do |match|
8-
%(![add-emoji](https://github.githubassets.com/images/icons/emoji/#{match.to_str.tr(':', '')}.png))
9-
end
10-
end
11-
@rendered = MarkdownHelper.render markdown_source
12-
respond_with @rendered
3+
@markdown_source = helpers.enrich_markdown(markdown: params[:source])
134
end
145
end

app/helpers/application_helper.rb

-8
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,4 @@ def active_page_size(page_size, param = nil)
2121
'active'
2222
end
2323
end
24-
25-
def emojify(content)
26-
if content.present?
27-
content.to_str.gsub(/(?<=^|\s):([\w+-]+):(?=\s|$)/) do |match|
28-
%(![add-emoji](https://github.githubassets.com/images/icons/emoji/#{match.to_str.tr(':', '')}.png))
29-
end.html_safe
30-
end
31-
end
3224
end

app/helpers/markdown_helper.rb

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
module MarkdownHelper
2-
def self.render(markdown_source)
3-
Haml::Filters::Markdown.new.render markdown_source
2+
def mdpreview(markdown_source, lines: 3)
3+
markdown_source.lines[0..lines - 1].join
44
end
55

6-
def mdpreview(markdown_source, lines: 3)
7-
markdown_source.lines.grep_v(/\[comment\]/).grep(/\S/)[0..lines - 1].join
6+
def enrich_markdown(markdown:)
7+
# replace :smiley: with a link to github.com emojis
8+
markdown.gsub!(/(?<=^|\s):([\w+-]+):(?=\s|$)/) do |match|
9+
%(![add-emoji](https://github.githubassets.com/images/icons/emoji/#{match.to_str.tr(':', '')}.png))
10+
end
11+
# replace @hans with a link to the user with the login hans
12+
markdown.gsub!(/([^\w]|^)@([-\w]+)([^\w]|$)/) do
13+
"#{Regexp.last_match(1)}[@#{Regexp.last_match(2)}](#{::Rails.application.routes.url_helpers(only_path: true).user_path(Regexp.last_match(2))})#{Regexp.last_match(3)}"
14+
end
15+
# replace hw#my-project with a link to the project with the slug my-project
16+
markdown.gsub!(/([^\w]|^)hw#([-\w]+)([^\w]|$)/) do
17+
"#{Regexp.last_match(1)}[hw##{Regexp.last_match(2)}](#{::Rails.application.routes.url_helpers(only_path: true).project_path(Regexp.last_match(2))})#{Regexp.last_match(3)}"
18+
end
19+
20+
markdown
821
end
922
end

app/views/comments/_comment.html.haml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Edit
1717
%p
1818
:markdown
19-
#{ emojify comment.text }
19+
#{ enrich_markdown(markdown: comment.text) }
2020
- if !comment.comments.empty?
2121
%ul.media-list
2222
= render :partial => 'comments/comment', :collection => comment.comments, object: comment
@@ -46,7 +46,7 @@
4646
.modal-body
4747
%p
4848
:markdown
49-
#{ emojify comment.text }
49+
#{ enrich_markdown(markdown: comment.text) }
5050
%hr
5151
#replyform
5252
= render partial: 'comments/form', locals: { comment: @new_comment, parent: comment, id: rand(36**10).to_s(36).upcase[0,5] }

app/views/comments/_form.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
.comment-form-body
1414
.tab-content
1515
.tab-pane.active.fade.in{ role: 'tab-pane', id: "markdown-source#{id}" }
16-
= f.text_area :text, :placeholder => "Your comment. You can use markdown.", :class => 'form-control input-lg markdown-source-text', :required => "required"
16+
= f.text_area :text, :placeholder => "Your comment. You can use markdown.", :class => 'form-control input-lg', :required => "required"
1717
.tab-pane.fade{ role: 'tab-pane', id: "markdown-preview#{id}" }
1818
.loading-spinner
1919
= icon('fas', 'spinner pulse 3x')

app/views/comments/_help.html.haml

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
Use two asterisks for **strong emphasis**
1010

11-
* Use asterisks
12-
* for lists
11+
- Use hyphens
12+
- for unordereed
13+
- lists
1314

14-
This is an [example link](http://example.com/)
15+
This is an [link to example.com](http://example.com/)
1516

16-
This is an ![example image](http://paste.opensuse.org/view/raw/68957446)
17+
This is an image ![an openSUSE geeko icon](https://en.opensuse.org/images/d/d0/Icon-distribution.png)
1718

1819
This is a user link @hans
1920

app/views/keywords/edit.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
.form-group
1717
= f.label('Description (maximum 255 characters)')
18-
= f.text_area :description, maxlength: "255", rows: 5, id: 'keyword_description', class: 'form-control input-lg markdown-source-text'
18+
= f.text_area :description, maxlength: "255", rows: 5, id: 'keyword_description', class: 'form-control input-lg'
1919
.form-group
2020
= f.label('Keyword Logo (Will be resized to 150x150 Pixels)')
2121
= f.file_field :avatar

app/views/markdown/_preview.html.haml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:markdown
2+
#{markdown_source}

app/views/markdown/preview.js.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$('#<%= params[:form_parent]%> .preview-contents').html("<%=j raw @rendered %>");
1+
$('#<%= params[:form_parent]%> .preview-contents').html("<%= escape_javascript(render partial: 'preview', locals: { markdown_source: @markdown_source }) %>");
22
$('#<%= params[:form_parent]%> .loading-spinner').addClass('hidden');
33
$('#<%= params[:form_parent]%> .preview-contents').removeClass('hidden');
44
$('input[name="authenticity_token"]').val('<%= form_authenticity_token %>');

app/views/projects/_form.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#markdown-source.tab-pane.active.fade.in{ role: 'tab-pane' }
2929
.form-group
3030
= f.text_area :description, rows: 20, id: 'project_description',
31-
class: 'form-control input-lg markdown-source-text'
31+
class: 'form-control input-lg'
3232
#markdown-preview.tab-pane.fade{ role: 'tab-pane' }
3333
.loading-spinner
3434
= icon('fas', 'spinner pulse 3x')

app/views/projects/show.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
.row
2828
.col-sm-8.project-style
2929
:markdown
30-
#{ emojify @project.description }
30+
#{ enrich_markdown(markdown: @project.description) }
3131
.col-sm-4
3232
.row
3333
.col-sm-12

lib/haml/filters/markdown.rb

-36
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
require 'rails_helper'
22

33
RSpec.describe MarkdownController, type: :controller do
4-
describe 'GET #preview' do
5-
it 'correctly assigns rendered html' do
6-
source = '*italic*'
7-
8-
get :preview, xhr: true, params: { source: source }
4+
render_views
95

10-
expect(response).to be_successful
11-
expect(assigns(:rendered)).to eq "<p><em>italic</em></p>\n"
6+
describe 'GET #preview' do
7+
it 'renders a markdown preview' do
8+
sign_in create :user
9+
get :preview, xhr: true, params: { source: '**hans**' }
10+
expect(response.body).to include('$(\'# .preview-contents\').html("<p><strong>hans<\/strong><\/p>\n\n");')
1211
end
1312
end
1413
end

spec/helpers/markdown_helper_spec.rb

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
require 'rails_helper'
22

3-
# Specs in this file have access to a helper object that includes
4-
# the MarkdownHelper. For example:
5-
#
6-
# describe MarkdownHelper do
7-
# describe "string concat" do
8-
# it "concats two strings with spaces" do
9-
# expect(helper.concat_strings("this","that")).to eq("this that")
10-
# end
11-
# end
12-
# end
133
RSpec.describe MarkdownHelper, type: :helper do
14-
describe '#render' do
15-
it_behaves_like 'a markdown renderer'
4+
describe '.enrich_markdown' do
5+
it 'translates emoji' do
6+
text = 'I need :coffee: so badly, working openSUSE:Factory:Staging:F'
7+
expect(enrich_markdown(markdown: text)).to eq('I need ![add-emoji](https://github.githubassets.com/images/icons/emoji/coffee.png) so badly, working openSUSE:Factory:Staging:F')
8+
end
9+
10+
it 'translate @user links' do
11+
text = 'Hey @hans, how are you?'
12+
expect(enrich_markdown(markdown: text)).to eq('Hey [@hans](/users/hans), how are you?')
13+
end
14+
15+
it 'translates hw#slug links' do
16+
text = 'Have you seen hw#super-cool? Its awesome'
17+
expect(enrich_markdown(markdown: text)).to eq('Have you seen [hw#super-cool](/projects/super-cool)? Its awesome')
18+
end
1619
end
1720
end

spec/lib/haml/filters/markdown_spec.rb

-7
This file was deleted.

spec/support/shared_examples/a_markdown_renderer.rb

-24
This file was deleted.

0 commit comments

Comments
 (0)