Skip to content

Commit

Permalink
Introduce some save markdown rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
hennevogel committed Feb 21, 2017
1 parent fe7242f commit 6ad4636
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 20 deletions.
13 changes: 13 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'markdown_renderer'

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

Expand All @@ -14,6 +16,17 @@ class ApplicationController < ActionController::Base

rescue_from ActionController::ParameterMissing, with: :parameter_empty

def save_markdown(markdown_source)
# Initializes a Markdown parser, if needed
@md_parser ||= Redcarpet::Markdown.new(Hackweek::MarkdownRenderer.new(no_styles: true),
autolink: true,
no_intra_emphasis: true,
fenced_code_blocks: true, disable_indented_code_blocks: true
)
@md_parser.render(markdown_source.to_s).html_safe
end
helper_method :save_markdown

protected

# store last visited url unless it's the login/sign up path,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/markdown_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MarkdownController < ApplicationController

def preview
markdown_source = params[:source]
@rendered = MarkdownHelper.render markdown_source
@rendered = save_markdown markdown_source
respond_with @rendered
end
end
8 changes: 0 additions & 8 deletions app/helpers/markdown_helper.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/views/comments/_comment.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
|
= link_to 'Reply', new_comment_comment_path( comment )
%p
:markdown
#{comment.text}
= save_markdown(comment.text)
- if !comment.comments.empty?
%ul.media-list
= render :partial => 'comments/comment', :collection => comment.comments, object: comment
3 changes: 1 addition & 2 deletions app/views/projects/_list_item.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
= link_to(user_path(project.originator.id)) do
#{project.originator.name}
= render :partial => "projects/like_button", :locals => {:project => project }
:markdown
#{truncate(project.description, length: 140)}
= save_markdown(truncate(project.description, length: 140))
- unless project.users.empty?
.well.well-sm
- project.users.each do |user|
Expand Down
3 changes: 1 addition & 2 deletions app/views/projects/_tile.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
= project.title
= render :partial => "projects/like_button", :locals => {:project => project }
.panel-body
:markdown
#{truncate(project.description, length: 140)}
= save_markdown(truncate(project.description, length: 140))
.user-list{:style=>"padding-top: 10px;"}
- if project.users.empty?
.alert.alert-warning
Expand Down
3 changes: 1 addition & 2 deletions app/views/projects/random.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
=fa_icon :pause, id: 'play-pause-icon'
.row
.col-sm-8
:markdown
#{@project.description}
= save_markdown(@project.description)
.col-sm-4
.row
.col-sm-12
Expand Down
5 changes: 2 additions & 3 deletions app/views/projects/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
= render :partial => "projects/info", :locals => {:project => @project }
.row
.col-sm-8.project-style
:markdown
#{@project.description}
= save_markdown(@project.description)
.col-sm-4
.row
.col-sm-12
Expand Down Expand Up @@ -62,7 +61,7 @@
%p
- if @project.keywords.empty?
Nothing? Add some keywords!
- else
- else
= render :partial => "keywords/show", :locals => {:keywords => @project.keywords }
-if @project.originator == current_user or @project.users.include? current_user
= render :partial => "keywords/new", :locals => {:what => @project }
Expand Down
16 changes: 16 additions & 0 deletions lib/markdown_renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Hackweek
class MarkdownRenderer < Redcarpet::Render::HTML
include Rails.application.routes.url_helpers(only_path: true)

def preprocess(fulldoc)
# @user links
#fulldoc.gsub!(/([^\w]|^)@([-\w]+)([^\w]|$)/) \
# {"#{Regexp.last_match(1)}[@#{Regexp.last_match(2)}](#{user_path(Regexp.last_match(2))})#{Regexp.last_match(3)}" }
# sanitize the HTML we get
Sanitize.fragment(fulldoc, Sanitize::Config.merge(Sanitize::Config::RESTRICTED,
elements: Sanitize::Config::RESTRICTED[:elements] + ['pre'],
remove_contents: true
))
end
end
end

0 comments on commit 6ad4636

Please sign in to comment.