-
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.
Merge pull request #1647 from hennevogel/refactoring/md-parsing
Simplify markdown preview
- Loading branch information
Showing
18 changed files
with
67 additions
and
134 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
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 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,20 +1,8 @@ | ||
[comment]: # (Please use the project description to give an overview and updates) | ||
[comment]: # (about the current state of your project) | ||
[comment]: # (Why do you start this project, what's your interest?) | ||
## Description | ||
|
||
## Project Description | ||
|
||
## Goals | ||
|
||
[comment]: # (What are your goals to be achieved at the end of this Hackweek?) | ||
|
||
## Goal for this Hackweek | ||
|
||
|
||
[comment]: # (Please link to sources and other data here.) | ||
[comment]: # (Prefer public repositories, such as GitHub!) | ||
|
||
## Resources | ||
|
||
[comment]: # (After creating the project, please add some keywords:) | ||
[comment]: # (* What type of project mates are you looking for, which skills do you need or lack?) | ||
[comment]: # (* Which keywords will help other people to find your project?) |
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.