summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/behaviors/markdown/marks/inline_html.js
blob: 4520598e0ab16766535fa592bf8fe3a90fdbef93 (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
import { escape } from 'lodash';

// Transforms generated HTML back to GFM for Banzai::Filter::MarkdownFilter
export default () => ({
  name: 'inline_html',
  schema: {
    excludes: '',
    attrs: {
      tag: {},
      title: { default: null },
    },
    parseDOM: [
      {
        tag: 'sup, sub, kbd, q, samp, var',
        getAttrs: (el) => ({ tag: el.nodeName.toLowerCase() }),
      },
      {
        tag: 'abbr',
        getAttrs: (el) => ({ tag: 'abbr', title: el.getAttribute('title') }),
      },
    ],
    toDOM: (node) => [node.attrs.tag, { title: node.attrs.title }, 0],
  },
  toMarkdown: {
    mixable: true,
    open(state, mark) {
      return `<${mark.attrs.tag}${
        mark.attrs.title ? ` title="${state.esc(escape(mark.attrs.title))}"` : ''
      }>`;
    },
    close(_, mark) {
      return `</${mark.attrs.tag}>`;
    },
  },
});