Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorVinnie Okada <vokada@mrvinn.com>2014-10-12 06:17:02 +0400
committerVinnie Okada <vokada@mrvinn.com>2014-10-12 23:52:17 +0400
commit5b2a42a091b2300ae1962b158b1496ac160c9e0f (patch)
treebe2cc218ddeca791473ed1feed7f83d5c83981f3 /app
parent66dd61ef0f1f98f860bdef5aa2cdb1c6f2e1f83f (diff)
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.
Diffstat (limited to 'app')
-rw-r--r--app/helpers/events_helper.rb5
-rw-r--r--app/helpers/gitlab_markdown_helper.rb43
2 files changed, 41 insertions, 7 deletions
diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb
index 6aeab7bb8ce..100dde10273 100644
--- a/app/helpers/events_helper.rb
+++ b/app/helpers/events_helper.rb
@@ -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)
diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb
index 0365681a128..27d8aee830c 100644
--- a/app/helpers/gitlab_markdown_helper.rb
+++ b/app/helpers/gitlab_markdown_helper.rb
@@ -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