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.
This commit is contained in:
parent
66dd61ef0f
commit
5b2a42a091
2 changed files with 41 additions and 7 deletions
|
@ -136,9 +136,8 @@ module EventsHelper
|
|||
end
|
||||
|
||||
def event_note(text)
|
||||
text = first_line_in_markdown(text)
|
||||
text = truncate(text, length: 150)
|
||||
sanitize(markdown(text), tags: %w(a img b pre p))
|
||||
text = first_line_in_markdown(text, 150)
|
||||
sanitize(text, tags: %w(a img b pre p))
|
||||
end
|
||||
|
||||
def event_commit_title(message)
|
||||
|
|
|
@ -51,12 +51,21 @@ module GitlabMarkdownHelper
|
|||
@markdown.render(text).html_safe
|
||||
end
|
||||
|
||||
def first_line_in_markdown(text)
|
||||
line = text.split("\n").detect do |i|
|
||||
# Return the first line of +text+, up to +max_chars+, after parsing the line
|
||||
# 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?
|
||||
end
|
||||
line += '...' unless line.nil?
|
||||
line
|
||||
|
||||
if line
|
||||
md = markdown(line)
|
||||
truncated = truncate_visible(md, max_chars || md.length)
|
||||
end
|
||||
|
||||
truncated
|
||||
end
|
||||
|
||||
def render_wiki_content(wiki_page)
|
||||
|
@ -204,4 +213,30 @@ module GitlabMarkdownHelper
|
|||
def correct_ref
|
||||
@ref ? @ref : "master"
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue