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

icons_helper.rb « helpers « lib - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4f22edd6fd9d0fdcd2194a8e2d990ed7db0b8d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

require 'json'

module Nanoc::Helpers
  module IconsHelper
    extend self

    ICONS_SVG = '/assets/images/icons.svg'.freeze

    def icon(icon_name, size = nil, css_class = nil)
      unless known_sprites.include?(icon_name)
        exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg")
        raise exception
      end

      svg_class = [
        'gl-icon',
        'ml-1',
        'mr-1',
        "s#{size || 16}",
        *css_class
      ].join(' ')

      %(<svg class="#{svg_class}"><use href="#{ICONS_SVG}##{icon_name}" /></svg>)
    end

    private

    def read_file(path)
      root = File.expand_path('../../', __dir__)
      File.read("#{root}/#{path}")
    end

    def known_sprites
      @known_sprites ||= JSON.parse(read_file('node_modules/@gitlab/svgs/dist/icons.json'))['icons']
    end
  end
end