aboutsummaryrefslogtreecommitdiff
path: root/app.js
blob: fdf3d23657555956bd81d23858233435daec7ceb (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
'use strict';

const START = /^(\[_+\n)/g;
const END = /(_+])/g;

const parseAttr = require('md-attr-parser');

function locator(value, fromIndex) {
  const index = value.indexOf(START, fromIndex);
  return index;
}

function plugin() {
  function blockTokenizer(eat, value) {
    if (!this.options.gfm || value.search(START) !== 0) {
      return;
    }

    let prop = {
      class: undefined /* [] */,
      id: undefined,
    };

    let eaten = '';

    if (value.search(END) > 0) {
      if ((value.search(END) + value.match(END)[0].length) < value.length &&
            value.charAt(value.search(END) + value.match(END)[0].length) === '{') {
        const res = parseAttr(value, value.search(END) + value.match(END)[0].length);
        eaten = res.eaten;
        prop = res.prop;
      }
      let end = value.search(END);
      if (end !== value.match(START)[0].length) {
        end -= 1;
      }
      const t = eat(value.slice(0, value.match(END)[0].lenght) + eaten)({
        type: 'textarea',
        data: {
          hName: 'TEXTAREA',
          hProperties: prop,
          hChildren: [{type: 'text',
            value: value.slice(value.match(START)[0].length, end),
          }],
        },
      });
      return t;
    }
    return true;
  }

  blockTokenizer.locator = locator;

  const Parser = this.Parser;

  const blockTokenizers = Parser.prototype.blockTokenizers;
  const blockMethods = Parser.prototype.blockMethods;
  blockTokenizers.textinput = blockTokenizer;
  blockMethods.splice(blockMethods.indexOf('fencedCode') + 1, 0, 'textinput');

  const Compiler = this.Compiler;

  // Stringify
  if (Compiler) {
    const visitors = Compiler.prototype.visitors;
    visitors.textinput = function (node) {
      return `[__${this.all(node).join('')}__]`;
    };
  }
}

module.exports = plugin;