summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/behaviors/markdown/nodes/emoji.js
blob: 086c277bad4072dc729acd97f3b3b9fe1640ac40 (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
40
41
42
43
// Transforms generated HTML back to GFM for Banzai::Filter::EmojiFilter
export default () => ({
  name: 'emoji',
  schema: {
    inline: true,
    group: 'inline',
    attrs: {
      name: {},
      title: {},
      moji: {},
    },
    parseDOM: [
      {
        tag: 'gl-emoji',
        getAttrs: (el) => ({
          name: el.dataset.name,
          title: el.getAttribute('title'),
          moji: el.textContent,
        }),
      },
      {
        tag: 'img.emoji',
        getAttrs: (el) => {
          const name = el.getAttribute('title').replace(/^:|:$/g, '');

          return {
            name,
            title: name,
            moji: name,
          };
        },
      },
    ],
    toDOM: (node) => [
      'gl-emoji',
      { 'data-name': node.attrs.name, title: node.attrs.title },
      node.attrs.moji,
    ],
  },
  toMarkdown(state, node) {
    state.write(`:${node.attrs.name}:`);
  },
});