Module: MotionMarkdownItPlugins::EmojiPlugin::NormalizeOpts

Included in:
MotionMarkdownItPlugins::Emoji
Defined in:
lib/motion-markdown-it-plugins/emoji/plugin/normalize_opts.rb

Instance Method Summary collapse

Instance Method Details

#normalize_opts(options) ⇒ Object




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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/motion-markdown-it-plugins/emoji/plugin/normalize_opts.rb', line 10

def normalize_opts(options)
  emojies   = stringify_keys(options[:defs])
  shortcuts = stringify_keys(options[:shortcuts])

  # Filter emojies by whitelist, if needed
  if options[:enabled].length > 0
    emojies = emojies.keys.reduce({}) do |acc, key|
      if options[:enabled].include?(key)
        acc[key] = emojies[key]
      end

      acc
    end
  end

  # Flatten shortcuts to simple object: { alias: emoji_name }
  shortcuts = shortcuts.keys.reduce({}) do |acc, key|
    # Skip aliases for filtered emojies, to reduce regexp
    next acc if !emojies[key]

    if shortcuts[key].is_a?(Array)
      shortcuts[key].each do |alias_value|
        acc[alias_value] = key
      end
      next acc
    end

    acc[shortcuts[key]] = key
    acc
  end

  # Compile regexp
  names = emojies.keys
                .map { |name| ':' + name + ':' }
                .concat(shortcuts.keys)
                .sort
                .reverse
                .map { |name| quoteRE(name) }
                .join('|')

  scanRE    = Regexp.new(names)
  replaceRE = Regexp.new(names)

  return {
    defs:       emojies,
    shortcuts:  shortcuts,
    scanRE:     scanRE,
    replaceRE:  replaceRE
  }
end

#quoteRE(str) ⇒ Object




5
6
7
# File 'lib/motion-markdown-it-plugins/emoji/plugin/normalize_opts.rb', line 5

def quoteRE(str)
  str.gsub(/([.?*+^$\[\]\\(){}|-])/, '\\\\\1')
end