summaryrefslogtreecommitdiff
path: root/lib/gitlab/front_matter.rb
blob: 5c5c74ca1a0bb23a97677d5c1fa0bf9cae838817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# frozen_string_literal: true

module Gitlab
  module FrontMatter
    DELIM_LANG = {
      '---' => 'yaml',
      '+++' => 'toml',
      ';;;' => 'json'
    }.freeze

    DELIM = Regexp.union(DELIM_LANG.keys)

    PATTERN = %r{
      \A(?:[^\r\n]*coding:[^\r\n]*\R)?        # optional encoding line
      \s*
      ^(?<delim>#{DELIM})[ \t]*(?<lang>\S*)\R # opening front matter marker (optional language specifier)
      (?<front_matter>.*?)                    # front matter block content (not greedy)
      ^(\k<delim> | \.{3})                    # closing front matter marker
      \s*
    }mx.freeze
  end
end