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


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

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

function plugin() {
  function blockTokenizer(eat, value, silent) {

    if (!this.options.gfm || value.search(START) != 0) {
      return;
    }
    if( value.search(END) > 0 ) {
      return eat(value.slice(0,value.search(END))+value.match(END)[0])({
        type: 'form',
        data: {
          hName: 'form',
          hChildren : [ {
            type: 'texterea',
            data: {
              hName: 'textarea',
              hChildren : [ { type: 'text',
                value: value.slice(value.match(START)[0].length+1, value.search(END)-1 )
              } ]
            }
          }]
        }
      });
    } else
      return true;
  }

  blockTokenizer.locator = locator;

  var Parser = this.Parser;

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


  var Compiler = this.Compiler;

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

module.exports = plugin;