Improve code block support
This commit is contained in:
parent
2303686e31
commit
6e248c2d5c
4 changed files with 31 additions and 12 deletions
|
|
@ -8,6 +8,8 @@ module MarkdownifyHelper
|
||||||
def markdownify(message, render_options={})
|
def markdownify(message, render_options={})
|
||||||
markdown_options = {
|
markdown_options = {
|
||||||
:autolink => true,
|
:autolink => true,
|
||||||
|
:fenced_code_blocks => true,
|
||||||
|
:space_after_headers => true
|
||||||
}
|
}
|
||||||
|
|
||||||
render_options[:filter_html] = true
|
render_options[:filter_html] = true
|
||||||
|
|
|
||||||
|
|
@ -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.text, :youtube_maps => post[:youtube_titles])
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
require 'erb'
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
|
||||||
module Diaspora
|
module Diaspora
|
||||||
|
|
@ -36,13 +37,14 @@ module Diaspora
|
||||||
if link =~ regex
|
if link =~ regex
|
||||||
video_id = $1
|
video_id = $1
|
||||||
if @vimeo_maps[video_id]
|
if @vimeo_maps[video_id]
|
||||||
title = h(CGI::unescape(@vimeo_maps[video_id]))
|
title = ERB::Util.h(CGI::unescape(@vimeo_maps[video_id]))
|
||||||
else
|
else
|
||||||
title = I18n.t 'application.helper.video_title.unknown'
|
title = I18n.t 'application.helper.video_title.unknown'
|
||||||
end
|
end
|
||||||
return ' <a class="video-link" data-host="vimeo.com" data-video-id="' +
|
return ' <a class="video-link" data-host="vimeo.com" data-video-id="' +
|
||||||
video_id + '" href="' + link + '" target="_blank">Vimeo: ' + title + '</a>'
|
video_id + '" href="' + link + '" target="_blank">Vimeo: ' + title + '</a>'
|
||||||
end
|
end
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
def autolink_youtube(link)
|
def autolink_youtube(link)
|
||||||
|
|
@ -51,7 +53,7 @@ module Diaspora
|
||||||
anchor = $2 || ''
|
anchor = $2 || ''
|
||||||
|
|
||||||
if @youtube_maps[video_id]
|
if @youtube_maps[video_id]
|
||||||
title = h(CGI::unescape(@youtube_maps[video_id]))
|
title = ERB::Util.h(CGI::unescape(@youtube_maps[video_id]))
|
||||||
else
|
else
|
||||||
title = I18n.t 'application.helper.video_title.unknown'
|
title = I18n.t 'application.helper.video_title.unknown'
|
||||||
end
|
end
|
||||||
|
|
@ -59,6 +61,11 @@ module Diaspora
|
||||||
video_id + '" data-anchor="' + anchor +
|
video_id + '" data-anchor="' + anchor +
|
||||||
'" href="'+ link + '" target="_blank">Youtube: ' + title + '</a>'
|
'" href="'+ link + '" target="_blank">Youtube: ' + title + '</a>'
|
||||||
end
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
def block_code(text, language)
|
||||||
|
"<pre><code>\n#{text}</code></pre>"
|
||||||
end
|
end
|
||||||
|
|
||||||
def double_emphasis(text)
|
def double_emphasis(text)
|
||||||
|
|
@ -95,9 +102,18 @@ module Diaspora
|
||||||
text = Diaspora::Taggable.format_tags(text, :no_escape => true)
|
text = Diaspora::Taggable.format_tags(text, :no_escape => true)
|
||||||
end
|
end
|
||||||
|
|
||||||
return "<p>#{text}</p>"
|
if @newlines
|
||||||
|
br = linebreak
|
||||||
|
|
||||||
|
# in very clear cases, let newlines become <br /> tags
|
||||||
|
# Grabbed from Github flavored Markdown
|
||||||
|
text = text.gsub(/^[\w\<][^\n]*\n+/) do |x|
|
||||||
|
x =~ /\n{2}/ ? x : (x = x.strip; x << br)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return "<p>#{text}</p>"
|
||||||
|
end
|
||||||
|
|
||||||
def preprocess(full_document)
|
def preprocess(full_document)
|
||||||
if @specialchars
|
if @specialchars
|
||||||
|
|
@ -123,17 +139,11 @@ module Diaspora
|
||||||
%Q{[#{content}](#{link}#{title_chunk})}
|
%Q{[#{content}](#{link}#{title_chunk})}
|
||||||
end
|
end
|
||||||
|
|
||||||
if @newlines
|
|
||||||
# in very clear cases, let newlines become <br /> tags
|
|
||||||
# Grabbed from Github flavored Markdown
|
|
||||||
full_document = full_document.gsub(/^[\w\<][^\n]*\n+/) do |x|
|
|
||||||
x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return full_document
|
return full_document
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def single_emphasis(text)
|
def single_emphasis(text)
|
||||||
"<em>#{text}</em>"
|
"<em>#{text}</em>"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,13 @@ describe MarkdownifyHelper do
|
||||||
markdownify(full_url).should == %Q{<p><a target="_blank" href="#{full_url}">#{url}</a></p>}
|
markdownify(full_url).should == %Q{<p><a target="_blank" href="#{full_url}">#{url}</a></p>}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "doesn't muck up code text" do
|
||||||
|
message = %{`puts "Hello"`}
|
||||||
|
markdownify(message).should =~ %r{<p><code>puts "Hello"</code></p>}
|
||||||
|
message = %Q{~~~\nA\nB\n~~~\n}
|
||||||
|
markdownify(message).should =~ %r{<pre><code>\nA\nB\n</code></pre>}
|
||||||
|
end
|
||||||
|
|
||||||
it "doesn't double parse video links" do
|
it "doesn't double parse video links" do
|
||||||
message = "http://www.vimeo.com/17449557
|
message = "http://www.vimeo.com/17449557
|
||||||
http://www.youtube.com/watch?v=0x__dDWdf23&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1
|
http://www.youtube.com/watch?v=0x__dDWdf23&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue