summaryrefslogtreecommitdiff
path: root/app/helpers/gitlab_markdown_helper.rb
diff options
context:
space:
mode:
authorVinnie Okada <vokada@mrvinn.com>2014-10-12 23:07:18 -0500
committerVinnie Okada <vokada@mrvinn.com>2014-10-12 23:56:56 -0500
commitb3c70d001d7371e8952cd7be879e727b5ee4155a (patch)
treeef3731d109df59f9786c1fe25aea70a1d08b89cb /app/helpers/gitlab_markdown_helper.rb
parent5b2a42a091b2300ae1962b158b1496ac160c9e0f (diff)
downloadgitlab-ce-b3c70d001d7371e8952cd7be879e727b5ee4155a.tar.gz
Improve dashboard note view and add tests
Update the `#first_line_in_markdown` method so that the first line of parsed text is displayed more reliably, and the continuation indicators ("...") are displayed in all cases where the note is truncated. Also add Rspec tests for `EventsHelper#event_note`.
Diffstat (limited to 'app/helpers/gitlab_markdown_helper.rb')
-rw-r--r--app/helpers/gitlab_markdown_helper.rb35
1 files changed, 25 insertions, 10 deletions
diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb
index 27d8aee830c..7d3cb749829 100644
--- a/app/helpers/gitlab_markdown_helper.rb
+++ b/app/helpers/gitlab_markdown_helper.rb
@@ -56,16 +56,9 @@ module GitlabMarkdownHelper
# +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
-
- if line
- md = markdown(line)
- truncated = truncate_visible(md, max_chars || md.length)
- end
+ md = markdown(text).strip
- truncated
+ truncate_visible(md, max_chars || md.length) if md.present?
end
def render_wiki_content(wiki_page)
@@ -221,22 +214,44 @@ module GitlabMarkdownHelper
def truncate_visible(text, max_chars)
doc = Nokogiri::HTML.fragment(text)
content_length = 0
+ truncated = false
doc.traverse do |node|
if node.text? || node.content.empty?
- if content_length >= max_chars
+ if truncated
node.remove
next
end
+ # Handle line breaks within a node
+ if node.content.strip.lines.length > 1
+ node.content = "#{node.content.lines.first.chomp}..."
+ truncated = true
+ end
+
num_remaining = max_chars - content_length
if node.content.length > num_remaining
node.content = node.content.truncate(num_remaining)
+ truncated = true
end
content_length += node.content.length
end
+
+ truncated = truncate_if_block(node, truncated)
end
doc.to_html
end
+
+ # Used by #truncate_visible. If +node+ is the first block element, and the
+ # text hasn't already been truncated, then append "..." to the node contents
+ # and return true. Otherwise return false.
+ def truncate_if_block(node, truncated)
+ if node.element? && node.description.block? && !truncated
+ node.content = "#{node.content}..." if node.next_sibling
+ true
+ else
+ truncated
+ end
+ end
end