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

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

const START = '[__';
const END = '__]';

/* Function used to locate the start of a line input filed
 * Used by remark
 */
function locator(value, fromIndex) {
  const index = value.indexOf(START, fromIndex);
  return index;
}

/* Funtion which is exported */
function plugin() {
  /* Verifie if it's the syntax of a line input and return a line input node */
  function inlineTokenizer(eat, value, silent) {
    if (!this.options.gfm || !value.startsWith(START)) {
      return;
    }

    let subvalue = '';
    let index = START.length;
    const length = value.length;

    while (!value.startsWith(END, index) && index < length) {
      subvalue += value.charAt(index);
      if (value.charAt(index) === '\n') {
        return true;
      }
      index++;
    }

    let letsEat = '';
    let prop = { /* key: undefined {}  class: undefined [] id: undefined */};

    /* Parse the attributes if any */
    if (value.charAt(index + END.length) === '{') {
      const res = parseAttr(value, index + END.length);
      letsEat = res.eaten;
      prop = res.prop;
    }

    /* istanbul ignore if - never used (yet) */
    if (silent) {
      return true;
    }

    /* Allow some other kind of input */
    if (prop.type !== 'password') {
      prop.type = 'text';
    }

    prop.placeholder = subvalue.replace(/^_*/g, '').replace(/_*$/g, '') || undefined;

    if (index < length) {
      return eat(START + subvalue + END + letsEat)({
        type: 'line-input',
        children: [],
        data: {
          hName: 'input',
          hProperties: prop,
        },
      });
    }
    return true;
  }

  inlineTokenizer.locator = locator;

  const Parser = this.Parser;

  // Inject inlineTokenizer
  const inlineTokenizers = Parser.prototype.inlineTokenizers;
  const inlineMethods = Parser.prototype.inlineMethods;
  inlineTokenizers.input = inlineTokenizer;
  inlineMethods.splice(inlineMethods.indexOf('url'), 0, 'input');

  const Compiler = this.Compiler;

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

module.exports = plugin;