f9f9147290
This helper is used for extracting part of the issue / MR / whatever description for use in the description meta tag: 1. To do that, we look at the source of the Markdown description. 2. We then strip out all HTML tags. 3. And then take the first 30 words. Doing that can be really slow - especially as Markdown is supposed to be treated as plain text. There are many better ways to do this, but the immediate performance fix is to swap steps 2 and 3. This does mean that the description may be less than 30 words (or even empty), but it is much faster when the description is very long.
133 lines
3 KiB
Ruby
133 lines
3 KiB
Ruby
# coding: utf-8
|
|
# frozen_string_literal: true
|
|
|
|
module PageLayoutHelper
|
|
def page_title(*titles)
|
|
@page_title ||= []
|
|
|
|
@page_title.push(*titles.compact) if titles.any?
|
|
|
|
if titles.any? && !defined?(@breadcrumb_title)
|
|
@breadcrumb_title = @page_title.last
|
|
end
|
|
|
|
# Segments are separated by middot
|
|
@page_title.join(" · ")
|
|
end
|
|
|
|
# Define or get a description for the current page
|
|
#
|
|
# description - String (default: nil)
|
|
#
|
|
# If this helper is called multiple times with an argument, only the last
|
|
# description will be returned when called without an argument. Descriptions
|
|
# have newlines replaced with spaces and all HTML tags are sanitized.
|
|
#
|
|
# Examples:
|
|
#
|
|
# page_description # => "GitLab Community Edition"
|
|
# page_description("Foo")
|
|
# page_description # => "Foo"
|
|
#
|
|
# page_description("<b>Bar</b>\nBaz")
|
|
# page_description # => "Bar Baz"
|
|
#
|
|
# Returns an HTML-safe String.
|
|
def page_description(description = nil)
|
|
if description.present?
|
|
@page_description = description.squish
|
|
elsif @page_description.present?
|
|
sanitize(@page_description.truncate_words(30), tags: [])
|
|
end
|
|
end
|
|
|
|
def favicon
|
|
Gitlab::Favicon.main
|
|
end
|
|
|
|
def page_image
|
|
default = image_url('gitlab_logo.png')
|
|
|
|
subject = @project || @user || @group
|
|
|
|
image = subject.avatar_url if subject.present?
|
|
image || default
|
|
end
|
|
|
|
# Define or get attributes to be used as Twitter card metadata
|
|
#
|
|
# map - Hash of label => data pairs. Keys become labels, values become data
|
|
#
|
|
# Raises ArgumentError if given more than two attributes
|
|
def page_card_attributes(map = {})
|
|
raise ArgumentError, 'cannot provide more than two attributes' if map.length > 2
|
|
|
|
@page_card_attributes ||= {}
|
|
@page_card_attributes = map.reject { |_, v| v.blank? } if map.present?
|
|
@page_card_attributes
|
|
end
|
|
|
|
def page_card_meta_tags
|
|
tags = []
|
|
|
|
page_card_attributes.each_with_index do |pair, i|
|
|
tags << tag(:meta, property: "twitter:label#{i + 1}", content: pair[0])
|
|
tags << tag(:meta, property: "twitter:data#{i + 1}", content: pair[1])
|
|
end
|
|
|
|
tags.join.html_safe
|
|
end
|
|
|
|
def header_title(title = nil, title_url = nil)
|
|
if title
|
|
@header_title = title
|
|
@header_title_url = title_url
|
|
else
|
|
return @header_title unless @header_title_url
|
|
|
|
breadcrumb_list_item(link_to(@header_title, @header_title_url))
|
|
end
|
|
end
|
|
|
|
def sidebar(name = nil)
|
|
if name
|
|
@sidebar = name
|
|
else
|
|
@sidebar
|
|
end
|
|
end
|
|
|
|
def nav(name = nil)
|
|
if name
|
|
@nav = name
|
|
else
|
|
@nav
|
|
end
|
|
end
|
|
|
|
def fluid_layout
|
|
current_user && current_user.layout == "fluid"
|
|
end
|
|
|
|
def blank_container(enabled = false)
|
|
if @blank_container.nil?
|
|
@blank_container = enabled
|
|
else
|
|
@blank_container
|
|
end
|
|
end
|
|
|
|
def container_class
|
|
css_class = ["container-fluid"]
|
|
|
|
unless fluid_layout
|
|
css_class << "container-limited"
|
|
end
|
|
|
|
if blank_container
|
|
css_class << "container-blank"
|
|
end
|
|
|
|
css_class.join(' ')
|
|
end
|
|
end
|