summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/extensions/task_list.js
blob: 01e5bddb97a27a82b2c034d70a534ad9e0d2a8c8 (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
44
45
46
47
48
49
import { mergeAttributes } from '@tiptap/core';
import { TaskList } from '@tiptap/extension-task-list';
import { PARSE_HTML_PRIORITY_HIGHEST } from '../constants';
import { getMarkdownSource } from '../services/markdown_sourcemap';

export default TaskList.extend({
  addOptions() {
    return {
      ...this.parent?.(),
      HTMLAttributes: { dir: 'auto' },
    };
  },

  addAttributes() {
    return {
      numeric: {
        default: false,
        parseHTML: (element) => element.tagName.toLowerCase() === 'ol',
      },
      start: {
        default: 1,
        parseHTML: (element) =>
          element.hasAttribute('start') ? parseInt(element.getAttribute('start') || '', 10) : 1,
      },

      parens: {
        default: false,
        parseHTML: (element) => /^[0-9]+\)/.test(getMarkdownSource(element)),
      },
    };
  },

  parseHTML() {
    return [
      {
        tag: '.task-list',
        priority: PARSE_HTML_PRIORITY_HIGHEST,
      },
    ];
  },

  renderHTML({ HTMLAttributes: { numeric, ...HTMLAttributes } }) {
    return [
      numeric ? 'ol' : 'ul',
      mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, { 'data-type': 'taskList' }),
      0,
    ];
  },
});