aboutsummaryrefslogtreecommitdiff
path: root/src/index.js
blob: 063444131000f6523729c267b0dce02dcb77eed6 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const visit = require('unist-util-visit');

/**
 * Given the MDAST ast, look for all fenced Blockquote
 *
 * @param {object} ast
 * @param {vFile} vFile
 * @return {function}
 */
function visitBlockquote(ast) {
  return visit(ast, 'blockquote', (node, index, parent) => {
    const firstNode = node.children[0];

    if (firstNode.type === 'paragraph') {
      if (firstNode.children[0].type === 'text') {

        const firstChild = firstNode.children[0];
        if (firstChild.value.startsWith('!secret')) {
          node.type = 'div';
          firstChild.value = firstChild.value.substr(7);

          let sum = '';
          if (firstChild.value.indexOf('\n') >= 0) {
            sum = firstChild.value.substr(0,
              firstChild.value.indexOf('\n'));
            firstChild.value = firstChild.value.substr(
              firstChild.value.indexOf('\n'));
          } else {
            sum = firstChild.value;
            firstChild.value = '';
          }

          const secret = {
            type: 'special-box-secret',
            children: [
              node,
              {
                type: 'summary',
                data: {
                  hName: 'summary',
                  hChildren: [{
                    type: 'text',
                    value: sum || 'Spoiler',
                  }],
                },
              },
            ],
            data: {
              hName: 'details',
              hProperties: {
                className: 'special-box secret',
              },
            },
          };

          parent.children.splice(index, 1, secret);
        } else if (firstChild.value.startsWith('!information\n') ||
            firstChild.value.startsWith('!good\n') ||
            firstChild.value.startsWith('!bad\n') ||
            firstChild.value.startsWith('!comment\n') ||
            firstChild.value.startsWith('!attention\n') ||
            firstChild.value.startsWith('!question')) {
          node.type = 'div';
          node.data = {
            hName: 'div',
            hProperties: {
              className: 'special-box-content',
            },
          };

          let type = '';
          if (firstChild.value.indexOf('\n') > 0) {
            type = firstChild.value.substr(1, firstChild.value.indexOf('\n'));
            firstChild.value = firstChild.value.substr(firstChild.value.indexOf('\n') + 1);
          } else {
            type = firstChild.value.substr(1);
            firstChild.value = '';
          }

          const box = {
            type: 'special-box-div',
            children: [node],
            data: {
              hName: 'div',
              hProperties: {
                className: `special-box ${type}`,
              },
            },
          };

          parent.children.splice(index, 1, box);
        }
      }
    }
    return node;
  });
}

/**
 * Returns the transformer which acst on the MDAST tree and given VFile.
 *
 * @link https://github.com/unifiedjs/unified#function-transformernode-file-next
 * @link https://github.com/syntax-tree/mdast
 * @link https://github.com/vfile/vfile
 * @return {function}
 */
function box() {
  /**
   * @param {object} ast MDAST
   * @param {vFile} vFile
   * @param {function} next
   * @return {object}
   */
  return function (ast, vFile, next) { // Transformer
    visitBlockquote(ast);

    if (typeof next === 'function') {
      return next(null, ast, vFile);
    }

    return ast;
  };
}

module.exports = box;