-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
- Loading branch information
1 parent
65c0fa5
commit 5f9f7f6
Showing
15 changed files
with
48 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,5 @@ | ||
class MarkdownController < ApplicationController | ||
skip_before_action :authenticate_user!, only: [:preview] | ||
respond_to :js | ||
|
||
def preview | ||
if params[:source] | ||
markdown_source = params[:source].to_str.gsub(/(?<=^|\s):([\w+-]+):(?=\s|$)/) do |match| | ||
%(![add-emoji](https://github.githubassets.com/images/icons/emoji/#{match.to_str.tr(':', '')}.png)) | ||
end | ||
end | ||
@rendered = MarkdownHelper.render markdown_source | ||
respond_with @rendered | ||
@markdown_source = helpers.enrich_markdown(markdown: params[:source]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
module MarkdownHelper | ||
def self.render(markdown_source) | ||
Haml::Filters::Markdown.new.render markdown_source | ||
def mdpreview(markdown_source, lines: 3) | ||
markdown_source.lines[0..lines - 1].join | ||
end | ||
|
||
def mdpreview(markdown_source, lines: 3) | ||
markdown_source.lines.grep_v(/\[comment\]/).grep(/\S/)[0..lines - 1].join | ||
def enrich_markdown(markdown:) | ||
# replace :smiley: with a link to github.com emojis | ||
markdown.gsub!(/(?<=^|\s):([\w+-]+):(?=\s|$)/) do |match| | ||
%(![add-emoji](https://github.githubassets.com/images/icons/emoji/#{match.to_str.tr(':', '')}.png)) | ||
end | ||
# replace @hans with a link to the user with the login hans | ||
markdown.gsub!(/([^\w]|^)@([-\w]+)([^\w]|$)/) do | ||
"#{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)}" | ||
end | ||
# replace hw#my-project with a link to the project with the slug my-project | ||
markdown.gsub!(/([^\w]|^)hw#([-\w]+)([^\w]|$)/) do | ||
"#{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)}" | ||
end | ||
|
||
markdown | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:markdown | ||
#{markdown_source} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe MarkdownController, type: :controller do | ||
describe 'GET #preview' do | ||
it 'correctly assigns rendered html' do | ||
source = '*italic*' | ||
|
||
get :preview, xhr: true, params: { source: source } | ||
render_views | ||
|
||
expect(response).to be_successful | ||
expect(assigns(:rendered)).to eq "<p><em>italic</em></p>\n" | ||
describe 'GET #preview' do | ||
it 'renders a markdown preview' do | ||
sign_in create :user | ||
get :preview, xhr: true, params: { source: '**hans**' } | ||
expect(response.body).to include('$(\'# .preview-contents\').html("<p><strong>hans<\/strong><\/p>\n\n");') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
require 'rails_helper' | ||
|
||
# Specs in this file have access to a helper object that includes | ||
# the MarkdownHelper. For example: | ||
# | ||
# describe MarkdownHelper do | ||
# describe "string concat" do | ||
# it "concats two strings with spaces" do | ||
# expect(helper.concat_strings("this","that")).to eq("this that") | ||
# end | ||
# end | ||
# end | ||
RSpec.describe MarkdownHelper, type: :helper do | ||
describe '#render' do | ||
it_behaves_like 'a markdown renderer' | ||
describe '.enrich_markdown' do | ||
it 'translates emoji' do | ||
text = 'I need :coffee: so badly, working openSUSE:Factory:Staging:F' | ||
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') | ||
end | ||
|
||
it 'translate @user links' do | ||
text = 'Hey @hans, how are you?' | ||
expect(enrich_markdown(markdown: text)).to eq('Hey [@hans](/users/hans), how are you?') | ||
end | ||
|
||
it 'translates hw#slug links' do | ||
text = 'Have you seen hw#super-cool? Its awesome' | ||
expect(enrich_markdown(markdown: text)).to eq('Have you seen [hw#super-cool](/projects/super-cool)? Its awesome') | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.