Commit 5b2a42a0 authored by Vinnie Okada's avatar Vinnie Okada

Preserve link href in truncated note view

Notes on the dashboard views are truncated to 150 characters; this
change ensures that when a link's text is truncated it still points to
the correct URL.
parent 66dd61ef
...@@ -136,9 +136,8 @@ module EventsHelper ...@@ -136,9 +136,8 @@ module EventsHelper
end end
def event_note(text) def event_note(text)
text = first_line_in_markdown(text) text = first_line_in_markdown(text, 150)
text = truncate(text, length: 150) sanitize(text, tags: %w(a img b pre p))
sanitize(markdown(text), tags: %w(a img b pre p))
end end
def event_commit_title(message) def event_commit_title(message)
......
...@@ -51,12 +51,21 @@ module GitlabMarkdownHelper ...@@ -51,12 +51,21 @@ module GitlabMarkdownHelper
@markdown.render(text).html_safe @markdown.render(text).html_safe
end end
def first_line_in_markdown(text) # Return the first line of +text+, up to +max_chars+, after parsing the line
line = text.split("\n").detect do |i| # as Markdown. HTML tags in the parsed output are not counted toward the
# +max_chars+ limit. If the length limit falls within a tag's contents, then
# the tag contents are truncated without removing the closing tag.
def first_line_in_markdown(text, max_chars = nil)
line = text.split("\n").find do |i|
i.present? && markdown(i).present? i.present? && markdown(i).present?
end end
line += '...' unless line.nil?
line if line
md = markdown(line)
truncated = truncate_visible(md, max_chars || md.length)
end
truncated
end end
def render_wiki_content(wiki_page) def render_wiki_content(wiki_page)
...@@ -204,4 +213,30 @@ module GitlabMarkdownHelper ...@@ -204,4 +213,30 @@ module GitlabMarkdownHelper
def correct_ref def correct_ref
@ref ? @ref : "master" @ref ? @ref : "master"
end end
private
# Return +text+, truncated to +max_chars+ characters, excluding any HTML
# tags.
def truncate_visible(text, max_chars)
doc = Nokogiri::HTML.fragment(text)
content_length = 0
doc.traverse do |node|
if node.text? || node.content.empty?
if content_length >= max_chars
node.remove
next
end
num_remaining = max_chars - content_length
if node.content.length > num_remaining
node.content = node.content.truncate(num_remaining)
end
content_length += node.content.length
end
end
doc.to_html
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment