diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb index 42febefee..a5356e2ec 100644 --- a/app/helpers/markdownify_helper.rb +++ b/app/helpers/markdownify_helper.rb @@ -7,6 +7,7 @@ require File.expand_path("#{Rails.root}/lib/diaspora/markdownify") module MarkdownifyHelper def markdownify(message, render_options={}) return '' if message.blank? + markdown_options = { :autolink => true, :fenced_code_blocks => true, @@ -20,7 +21,25 @@ module MarkdownifyHelper renderer = Diaspora::Markdownify::HTML.new(render_options) markdown = Redcarpet::Markdown.new(renderer, markdown_options) + + # This ugly little hack basically means + # "Give me the rawest contents of target available" + if target.respond_to?(:raw_message) + message = target.raw_message + elsif target.respond_to?(:text) + message = target.text + else + message = target.to_s + end + message = markdown.render(message) + + if target.respond_to?(:format_mentions) + message = target.format_mentions(message) + end + + message = Diaspora::Taggable.format_tags(message, :no_escape => true) + return message.html_safe end diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index b92be2366..4ecef5d38 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -14,7 +14,7 @@ – %span{:class => direction_for(comment.text)} - = markdownify(Diaspora::Taggable.format_tags(comment.text), :youtube_maps => comment.youtube_titles) + = markdownify(comment, :youtube_maps => comment.youtube_titles) .comment_info %time.timeago{:datetime => comment.created_at} diff --git a/app/views/comments/_comment.mobile.haml b/app/views/comments/_comment.mobile.haml index 6056839f0..19738dc6b 100644 --- a/app/views/comments/_comment.mobile.haml +++ b/app/views/comments/_comment.mobile.haml @@ -15,5 +15,5 @@ .from = person_link(comment.author) %p{ :class => direction_for(comment.text) } - = markdownify(comment.text, :youtube_maps => comment[:youtube_titles]) + = markdownify(comment, :youtube_maps => comment[:youtube_titles]) diff --git a/app/views/messages/_message.haml b/app/views/messages/_message.haml index da5d1f506..7b5ff3264 100644 --- a/app/views/messages/_message.haml +++ b/app/views/messages/_message.haml @@ -12,5 +12,5 @@ = how_long_ago(message) %p{ :class => direction_for(message.text) } - = markdownify(message.text) + = markdownify(message) diff --git a/app/views/photos/show.html.haml b/app/views/photos/show.html.haml index 2d8f2885d..f68e919a7 100644 --- a/app/views/photos/show.html.haml +++ b/app/views/photos/show.html.haml @@ -40,7 +40,7 @@ .span-8.last %p - = markdownify(photo.status_message.text) + = markdownify(photo.status_message) %span{:style=>'font-size:smaller'} =link_to t('.collection_permalink'), post_path(photo.status_message) %br diff --git a/app/views/posts/show.mobile.haml b/app/views/posts/show.mobile.haml index af3a960a1..faa1bf245 100644 --- a/app/views/posts/show.mobile.haml +++ b/app/views/posts/show.mobile.haml @@ -11,7 +11,7 @@ = render 'reshares/reshare', :reshare => @post, :post => @post.root - else %p - = markdownify(@post.text, :youtube_maps => @post[:youtube_titles]) + = markdownify(@post, :youtube_maps => @post[:youtube_titles]) - for photo in @post.photos = link_to (image_tag photo.url(:thumb_small), :class => 'thumb_small'), photo.url(:thumb_medium) diff --git a/app/views/status_messages/_status_message.haml b/app/views/status_messages/_status_message.haml index c3be5bad9..73b43c248 100644 --- a/app/views/status_messages/_status_message.haml +++ b/app/views/status_messages/_status_message.haml @@ -16,4 +16,4 @@ = link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link' %p{:class => direction_for(post.text)} - != markdownify(post.text, :youtube_maps => post[:youtube_titles]) + != markdownify(post, :youtube_maps => post[:youtube_titles]) diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb index 0d2d01f72..8515cf881 100644 --- a/lib/diaspora/markdownify.rb +++ b/lib/diaspora/markdownify.rb @@ -8,7 +8,6 @@ module Diaspora def initialize(options={}) super - @expand_tags = options.fetch(:expand_tabs, true) @newlines = options.fetch(:newlines, true) @specialchars = options.fetch(:specialchars, true) @youtube_maps = options.fetch(:youtube_maps, {}) @@ -100,10 +99,6 @@ module Diaspora end def paragraph(text) - if @expand_tags - text = Diaspora::Taggable.format_tags(text, :no_escape => true) - end - if @newlines br = linebreak @@ -118,6 +113,15 @@ module Diaspora end def preprocess(full_document) + entities = { + '>' => '>', + '<' => '<', + '&' => '&', + } + entities.each do |k,v| + full_document = full_document.gsub(k, v) + end + if @specialchars full_document = specialchars(full_document) end @@ -152,10 +156,10 @@ module Diaspora def specialchars(text) if @specialchars map = [ - ["<3", "♥"], - ["<->", "↔"], - ["->", "→"], - ["<-", "←"], + ["<3", "♥"], + ["<->", "↔"], + ["->", "→"], + ["<-", "←"], ["\.\.\.", "…"], ["(tm)", "™"], ["(r)", "®"], diff --git a/spec/helpers/markdownify_helper_spec.rb b/spec/helpers/markdownify_helper_spec.rb index e7f07f5a5..0734633cc 100644 --- a/spec/helpers/markdownify_helper_spec.rb +++ b/spec/helpers/markdownify_helper_spec.rb @@ -5,10 +5,11 @@ require 'spec_helper' describe MarkdownifyHelper do + describe "#markdownify" do describe "autolinks" do it "should not allow basic XSS/HTML" do - markdownify("").should == "

alert('XSS is evil')

" + markdownify("").should == "

<script>alert('XSS is evil')</script>

" end it "should recognize basic http links (1/3)" do @@ -250,12 +251,6 @@ describe MarkdownifyHelper do markdownify(message).should == '

This text with many links tests http, FTP, any protocol

' end - it "should leave #tag links intact" do - message = %{#tagged} - markdownify(message).should == "

#{message}

" - message = %{alice - 1 - #tagged} - markdownify(message).should == "

#{message}

" - end end describe "nested emphasis and links tags" do @@ -294,6 +289,26 @@ describe MarkdownifyHelper do markdownify(nil).should == '' end + context 'when formatting status messages' do + + it "should leave tags intact" do + message = Factory.create(:status_message, + :author => alice.person, + :text => "I love #markdown") + formatted = markdownify(message) + formatted.should =~ %r{#markdown} + end + + it "should leave mentions intact" do + message = Factory.create(:status_message, + :author => alice.person, + :text => "Hey @{Bob; #{bob.diaspora_handle}}!") + formatted = markdownify(message) + formatted.should =~ /hovercard/ + end + + end + context 'performance' do before do @message = "HHello,Hello_, I _am a strong robot.*Hello, I am *a strong robot.Hello, I am a strong robot.Hello, I am a strong robot.Hello, I am a strong robot.Hello, I am a **strong robot.Hello, I am _a _strong *robot**.Hello*, I am a strong "