mirror of
https://github.com/zhigang1992/tsemple.git
synced 2026-04-30 21:52:18 +08:00
Move custom markdown helper to comments helper
This commit is contained in:
45
app/helpers/comments_helper.rb
Normal file
45
app/helpers/comments_helper.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
module CommentsHelper
|
||||
def format_comment(text)
|
||||
sanitize(link_comments(link_mentions(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.search('text()').each do |node|
|
||||
unless node.ancestors('a, pre, code').any?
|
||||
text = node.text
|
||||
|
||||
# link @username
|
||||
text.gsub!(/@(\w+)/) { |match|
|
||||
%Q|<a href="/~#{$1}">#{match}</a>|
|
||||
}
|
||||
|
||||
node.replace text
|
||||
end
|
||||
end
|
||||
|
||||
doc.to_html
|
||||
end
|
||||
|
||||
def link_comments(text)
|
||||
doc = Nokogiri::HTML.fragment(text)
|
||||
|
||||
doc.search('text()').each do |node|
|
||||
unless node.ancestors('a, pre, code').any?
|
||||
text = node.text
|
||||
|
||||
# link #comment_id
|
||||
text.gsub!(/#(\d+)/) { |match|
|
||||
%Q|<a href="?comment_id=#{$1}">#{match}</a>|
|
||||
}
|
||||
|
||||
node.replace text
|
||||
end
|
||||
end
|
||||
|
||||
doc.to_html
|
||||
end
|
||||
end
|
||||
@@ -17,48 +17,4 @@ module MarkdownHelper
|
||||
|
||||
markdown.render(text)
|
||||
end
|
||||
|
||||
def campo_markdown(text)
|
||||
sanitize(link_comments(link_mentions(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.search('text()').each do |node|
|
||||
unless node.ancestors('a, pre, code').any?
|
||||
text = node.text
|
||||
|
||||
# link @username
|
||||
text.gsub!(/@(\w+)/) { |match|
|
||||
%Q|<a href="/~#{$1}">#{match}</a>|
|
||||
}
|
||||
|
||||
node.replace text
|
||||
end
|
||||
end
|
||||
|
||||
doc.to_html
|
||||
end
|
||||
|
||||
def link_comments(text)
|
||||
doc = Nokogiri::HTML.fragment(text)
|
||||
|
||||
doc.search('text()').each do |node|
|
||||
unless node.ancestors('a, pre, code').any?
|
||||
text = node.text
|
||||
|
||||
# link #comment_id
|
||||
text.gsub!(/#(\d+)/) { |match|
|
||||
%Q|<a href="?comment_id=#{$1}">#{match}</a>|
|
||||
}
|
||||
|
||||
node.replace text
|
||||
end
|
||||
end
|
||||
|
||||
doc.to_html
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user