aboutsummaryrefslogtreecommitdiff
path: root/app.js
blob: de1f2d701d610c0b1ca330c2c8181414778c86b3 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const visit = require('unist-util-visit');


const PLUGIN_NAME = 'remark-qcm';

var nb_qcm = 0;

function dealLabelChildren( listChild ) {
  var t = []
  if( listChild[0].type == 'paragraph') {
    t = listChild[0].children
  }
  if(listChild[listChild.length - 1].type == 'blockquote') {
      listChild[listChild.length - 1].type = 'div'
      listChild[listChild.length - 1].data = {
        hName:'blockquote',
        hProperties: {
          className: 'hiden_block_quote'
        }
      }
  }
  t = t.concat(listChild.slice(1))
  return t
}

function visitList(ast, vFile) {
  return visit(ast, 'list', (node, index, parent) => {
    const { position } = node;
    var isQcm = true;
    var nbQ = 0;
    var tab = []

    Array.from(node.children).forEach( ( nodeC ) => {
        if( nodeC.children && nodeC.children[0].type == 'paragraph' ) {
          if( nodeC.children[0].children && nodeC.children[0].children[0].value ) {
              if( "~!=".indexOf(nodeC.children[0].children[0].value[0]) < 0)
                isQcm = false;
          } else
            isQcm = false;
        } else
          isQcm = false;
    });
    if( isQcm ) {
      Array.from(node.children).forEach( ( nodeC ) => {
        if( nodeC.children[0].type == 'paragraph' ) {
          if( nodeC.children[0].children[0].value[0] == '~' ) {
            tab.push(0.5);
          }else if(nodeC.children[0].children[0].value[0] == '!') {
            tab.push(0);
          }else {
            tab.push(1);
          }
          nodeC.children[0].children[0].value = nodeC.children[0].children[0].value.slice(1) + '\r';
        }
      })
      node.type = 'qcm'
      node.data = {
          hName: 'fieldset',
          hProperties: {
            className: 'qcm check',
            id: 'qcm_' + nb_qcm
          }
      }
      node.children = [{
        type:'list-item-qcm',
        data: {
          hName: 'ul',
          hProperties:{
              'style': 'list-style-type: none'
          }
        },
        children : node.children.map( (x) => ({type:'input-list-item',
          data: {
            hName: 'li',
            hProperties:{
              'style': 'list-style-type: none'
            }
          },
          children: [
          {type:'input-list-input',
          data: {
            hName: 'input',
            hProperties: {
              checked: x.checked,
              type:'checkbox',
              id: 'qcm_'+nb_qcm+'_'+nbQ,
              className: tab[nbQ] == 0 ? '!' : (tab[nbQ] == 1 ? '=' : '~'),
            }
          }},
            {
            type: 'input-list-label',
            data: {
              hName: 'label',
              hProperties: {
                for: 'qcm_'+nb_qcm+'_'+(nbQ++)
              }
            },
            children: dealLabelChildren(x.children)
          }]
      }) )
      }, {type:'field-button',
        data: {
          hName: 'input',
          hProperties:{
            'onclick': 'check(\'qcm_'+nb_qcm+'\',[' + String(tab) + '])',
            value: 'Validate',
            type:'button'
          }
        }

      }
      ]
        
      nb_qcm++;
    }
    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 qcm() {
  /**
   * @param {object} ast MDAST
   * @param {vFile} vFile
   * @param {function} next
   * @return {object}
   */
  return function transformer(ast, vFile, next) {
    visitList(ast, vFile);

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

    return ast;
  };
}

module.exports = qcm;