mirror of
https://github.com/zhigang1992/tsemple.git
synced 2026-04-28 20:55:23 +08:00
Combine link mention and link comment helper
This commit is contained in:
@@ -17,22 +17,28 @@ module MarkdownHelper
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class MentionVisitor < TextReplaceVisitor
|
||||
def process(text)
|
||||
text.gsub(/@([a-z0-9][a-z0-9-]*)/) { |match|
|
||||
# link mention
|
||||
# @username => <a href="/~username">@username</a>
|
||||
text.gsub!(/@([a-z0-9][a-z0-9-]*)/) { |match|
|
||||
%Q|<a href="/~#{$1}">#{match}</a>|
|
||||
}
|
||||
|
||||
# link comments
|
||||
# #123 => <a href="?comment_id=123#comment-123">#123</a>
|
||||
text.gsub!(/#(\d+)/) { |match|
|
||||
%Q|<a href="?comment_id=#{$1}#comment-#{$1}">#{match}</a>|
|
||||
}
|
||||
|
||||
text
|
||||
end
|
||||
end
|
||||
|
||||
class CommentVisitor < TextReplaceVisitor
|
||||
def process(text)
|
||||
text.gsub(/#(\d+)/) { |match|
|
||||
%Q|<a href="?comment_id=#{$1}#comment-#{$1}">#{match}</a>|
|
||||
}
|
||||
end
|
||||
def markdown_text_replace(html)
|
||||
doc = Nokogiri::HTML.fragment(html)
|
||||
doc.accept(TextReplaceVisitor.new)
|
||||
doc.to_html
|
||||
end
|
||||
|
||||
def markdown(text)
|
||||
@@ -49,23 +55,11 @@ module MarkdownHelper
|
||||
end
|
||||
|
||||
def markdown_format(text)
|
||||
sanitize(link_comments(link_mentions(markdown(text))),
|
||||
sanitize(markdown_text_replace(markdown(text)),
|
||||
tags: %w(p br img h1 h2 h3 h4 blockquote pre code strong em a ul ol li span),
|
||||
attributes: %w(href src class title alt target rel))
|
||||
end
|
||||
|
||||
def link_mentions(text)
|
||||
doc = Nokogiri::HTML.fragment(text)
|
||||
doc.accept(MentionVisitor.new)
|
||||
doc.to_html
|
||||
end
|
||||
|
||||
def link_comments(text)
|
||||
doc = Nokogiri::HTML.fragment(text)
|
||||
doc.accept(CommentVisitor.new)
|
||||
doc.to_html
|
||||
end
|
||||
|
||||
def markdown_area(form, name, options = {})
|
||||
render partial: 'markdown/area', locals: options.merge(form: form, name: name)
|
||||
end
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
require 'test_helper'
|
||||
|
||||
class MarkdownHelperTest < ActionView::TestCase
|
||||
test "link mentions" do
|
||||
assert_equal %q|<p><a href="/~username">@username</a></p>|, link_mentions('<p>@username</p>')
|
||||
assert_equal %q|<a href="http://example.org/">@username</a>|, link_mentions(%q|<a href="http://example.org/">@username</a>|)
|
||||
assert_equal %q|<pre>@username</pre>|, link_mentions(%q|<pre>@username</pre>|)
|
||||
assert_equal %q|<code>@username</code>|, link_mentions(%q|<code>@username</code>|)
|
||||
test "should link mentions" do
|
||||
assert_equal %q|<p><a href="/~username">@username</a></p>|, markdown_text_replace('<p>@username</p>')
|
||||
assert_equal %q|<a href="http://example.org/">@username</a>|, markdown_text_replace(%q|<a href="http://example.org/">@username</a>|)
|
||||
assert_equal %q|<pre>@username</pre>|, markdown_text_replace(%q|<pre>@username</pre>|)
|
||||
assert_equal %q|<code>@username</code>|, markdown_text_replace(%q|<code>@username</code>|)
|
||||
end
|
||||
|
||||
test "should link floor" do
|
||||
assert_equal %Q|<p><a href="?comment_id=1#comment-1">#1</a></p>|, link_comments('<p>#1</p>')
|
||||
assert_equal %q|<a href="http://example.org/">#1</a>|, link_mentions(%q|<a href="http://example.org/">#1</a>|)
|
||||
assert_equal %q|<pre>#1</pre>|, link_mentions(%q|<pre>#1</pre>|)
|
||||
assert_equal %q|<code>#1</code>|, link_mentions(%q|<code>#1</code>|)
|
||||
test "should link comments" do
|
||||
assert_equal %Q|<p><a href="?comment_id=1#comment-1">#1</a></p>|, markdown_text_replace('<p>#1</p>')
|
||||
assert_equal %q|<a href="http://example.org/">#1</a>|, markdown_text_replace(%q|<a href="http://example.org/">#1</a>|)
|
||||
assert_equal %q|<pre>#1</pre>|, markdown_text_replace(%q|<pre>#1</pre>|)
|
||||
assert_equal %q|<code>#1</code>|, markdown_text_replace(%q|<code>#1</code>|)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user