Skip to content
Extraits de code Groupes Projets
Valider 272ab554 rédigé par Maxwell Salzberg's avatar Maxwell Salzberg
Parcourir les fichiers

Merge branch '1844-mentions-in-markdown' of...

Merge branch '1844-mentions-in-markdown' of https://github.com/brianwisti/diaspora into brianwisti-1844-mentions-in-markdown
parents 2b31999f d2f1f2fb
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
require File.expand_path("#{Rails.root}/lib/diaspora/markdownify") require File.expand_path("#{Rails.root}/lib/diaspora/markdownify")
module MarkdownifyHelper module MarkdownifyHelper
def markdownify(message, render_options={}) def markdownify(target, render_options={})
return '' if message.blank?
markdown_options = { markdown_options = {
:autolink => true, :autolink => true,
:fenced_code_blocks => true, :fenced_code_blocks => true,
...@@ -18,9 +18,30 @@ module MarkdownifyHelper ...@@ -18,9 +18,30 @@ module MarkdownifyHelper
render_options[:filter_html] = true render_options[:filter_html] = true
# 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
return '' if message.blank?
renderer = Diaspora::Markdownify::HTML.new(render_options) renderer = Diaspora::Markdownify::HTML.new(render_options)
markdown = Redcarpet::Markdown.new(renderer, markdown_options) markdown = Redcarpet::Markdown.new(renderer, markdown_options)
message = markdown.render(message) 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 return message.html_safe
end end
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
%span{:class => direction_for(comment.text)} %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 .comment_info
%time.timeago{:datetime => comment.created_at} %time.timeago{:datetime => comment.created_at}
......
...@@ -15,5 +15,5 @@ ...@@ -15,5 +15,5 @@
.from .from
= person_link(comment.author) = person_link(comment.author)
%p{ :class => direction_for(comment.text) } %p{ :class => direction_for(comment.text) }
= markdownify(comment.text, :youtube_maps => comment[:youtube_titles]) = markdownify(comment, :youtube_maps => comment[:youtube_titles])
...@@ -12,5 +12,5 @@ ...@@ -12,5 +12,5 @@
= how_long_ago(message) = how_long_ago(message)
%p{ :class => direction_for(message.text) } %p{ :class => direction_for(message.text) }
= markdownify(message.text) = markdownify(message)
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
.span-8.last .span-8.last
%p %p
= markdownify(photo.status_message.text) = markdownify(photo.status_message)
%span{:style=>'font-size:smaller'} %span{:style=>'font-size:smaller'}
=link_to t('.collection_permalink'), post_path(photo.status_message) =link_to t('.collection_permalink'), post_path(photo.status_message)
%br %br
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
= render 'reshares/reshare', :reshare => @post, :post => @post.root = render 'reshares/reshare', :reshare => @post, :post => @post.root
- else - else
%p %p
= markdownify(@post.text, :youtube_maps => @post[:youtube_titles]) = markdownify(@post, :youtube_maps => @post[:youtube_titles])
- for photo in @post.photos - for photo in @post.photos
= link_to (image_tag photo.url(:thumb_small), :class => 'thumb_small'), photo.url(:thumb_medium) = link_to (image_tag photo.url(:thumb_small), :class => 'thumb_small'), photo.url(:thumb_medium)
......
...@@ -16,4 +16,4 @@ ...@@ -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' = 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)} %p{:class => direction_for(post.text)}
!= markdownify(post.text, :youtube_maps => post[:youtube_titles]) != markdownify(post, :youtube_maps => post[:youtube_titles])
...@@ -8,7 +8,6 @@ module Diaspora ...@@ -8,7 +8,6 @@ module Diaspora
def initialize(options={}) def initialize(options={})
super super
@expand_tags = options.fetch(:expand_tabs, true)
@newlines = options.fetch(:newlines, true) @newlines = options.fetch(:newlines, true)
@specialchars = options.fetch(:specialchars, true) @specialchars = options.fetch(:specialchars, true)
@youtube_maps = options.fetch(:youtube_maps, {}) @youtube_maps = options.fetch(:youtube_maps, {})
...@@ -100,10 +99,6 @@ module Diaspora ...@@ -100,10 +99,6 @@ module Diaspora
end end
def paragraph(text) def paragraph(text)
if @expand_tags
text = Diaspora::Taggable.format_tags(text, :no_escape => true)
end
if @newlines if @newlines
br = linebreak br = linebreak
...@@ -118,6 +113,15 @@ module Diaspora ...@@ -118,6 +113,15 @@ module Diaspora
end end
def preprocess(full_document) def preprocess(full_document)
entities = {
'>' => '>',
'<' => '&lt;',
'&' => '&amp;',
}
entities.each do |k,v|
full_document = full_document.gsub(k, v)
end
if @specialchars if @specialchars
full_document = specialchars(full_document) full_document = specialchars(full_document)
end end
...@@ -152,10 +156,10 @@ module Diaspora ...@@ -152,10 +156,10 @@ module Diaspora
def specialchars(text) def specialchars(text)
if @specialchars if @specialchars
map = [ map = [
["<3", "&hearts;"], ["&lt;3", "&hearts;"],
["<->", "&#8596;"], ["&lt;-&gt;", "&#8596;"],
["->", "&rarr;"], ["-&gt;", "&rarr;"],
["<-", "&larr;"], ["&lt;-", "&larr;"],
["\.\.\.", "&hellip;"], ["\.\.\.", "&hellip;"],
["(tm)", "&trade;"], ["(tm)", "&trade;"],
["(r)", "&reg;"], ["(r)", "&reg;"],
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
require 'spec_helper' require 'spec_helper'
describe MarkdownifyHelper do describe MarkdownifyHelper do
describe "#markdownify" do describe "#markdownify" do
describe "autolinks" do describe "autolinks" do
it "should not allow basic XSS/HTML" do it "should not allow basic XSS/HTML" do
markdownify("<script>alert('XSS is evil')</script>").should == "<p>alert('XSS is evil')</p>" markdownify("<script>alert('XSS is evil')</script>").should == "<p>&lt;script&gt;alert('XSS is evil')&lt;/script&gt;</p>"
end end
it "should recognize basic http links (1/3)" do it "should recognize basic http links (1/3)" do
...@@ -250,12 +251,6 @@ describe MarkdownifyHelper do ...@@ -250,12 +251,6 @@ describe MarkdownifyHelper do
markdownify(message).should == '<p>This <a target="_blank" href="http://en.wikipedia.org/wiki/Text_%28literary_theory%29"><em>text</em></a> with many <a target="_blank" href="http://google.com">links</a> tests <a target="_blank" href="http://google.com/search?q=with_multiple__underscores*and**asterisks"><em>http</em></a>, <a target="_blank" href="ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4" title="File Transfer Protocol"><em><strong>FTP</strong></em></a>, <a target="_blank" href="foo://bar.example.org/yes_it*makes*no_sense"><strong>any protocol</strong></a></p>' markdownify(message).should == '<p>This <a target="_blank" href="http://en.wikipedia.org/wiki/Text_%28literary_theory%29"><em>text</em></a> with many <a target="_blank" href="http://google.com">links</a> tests <a target="_blank" href="http://google.com/search?q=with_multiple__underscores*and**asterisks"><em>http</em></a>, <a target="_blank" href="ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4" title="File Transfer Protocol"><em><strong>FTP</strong></em></a>, <a target="_blank" href="foo://bar.example.org/yes_it*makes*no_sense"><strong>any protocol</strong></a></p>'
end end
it "should leave #tag links intact" do
message = %{<a href="/tags/tagged" class="tag">#tagged</a>}
markdownify(message).should == "<p>#{message}</p>"
message = %{alice - 1 - <a href="/tags/tagged" class="tag">#tagged</a>}
markdownify(message).should == "<p>#{message}</p>"
end
end end
describe "nested emphasis and links tags" do describe "nested emphasis and links tags" do
...@@ -294,6 +289,26 @@ describe MarkdownifyHelper do ...@@ -294,6 +289,26 @@ describe MarkdownifyHelper do
markdownify(nil).should == '' markdownify(nil).should == ''
end 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{<a href="/tags/markdown" class="tag">#markdown</a>}
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 context 'performance' do
before 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 " @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 "
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter