From 5485600e31dc90a00b14e1274cbefc10fdcae626 Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 1 Feb 2018 14:40:53 +0100 Subject: Clean of the code with xo linter --- app.js | 157 +++++++++++++++++++++++++++++------------------------------ package.json | 5 +- 2 files changed, 82 insertions(+), 80 deletions(-) diff --git a/app.js b/app.js index 4ccc271..ddb548a 100644 --- a/app.js +++ b/app.js @@ -1,117 +1,116 @@ const visit = require('unist-util-visit'); +let nbMC = 0; -const PLUGIN_NAME = 'remark-multiple-choise'; - -var nb_mc = 0; - -function dealLabelChildren( listChild ) { - var t = [] - if( listChild[0].type == 'paragraph') { - t = listChild[0].children +function dealLabelChildren(listChild) { + let 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' - } + 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 + t = t.concat(listChild.slice(1)); + return t; } -function visitList(ast, vFile) { - return visit(ast, 'list', (node, index, parent) => { - const { position } = node; - var isMultipleChoise = true; - var nbQ = 0; - var tab = [] +function visitList(ast) { + return visit(ast, 'list', node => { + let isMultipleChoise = true; + let nbQ = 0; + const 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) - isMultipleChoise = false; - } else + 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) { isMultipleChoise = false; - } else + } + } else { isMultipleChoise = false; + } + } else { + isMultipleChoise = false; + } }); - if( isMultipleChoise ) { - Array.from(node.children).forEach( ( nodeC ) => { - if( nodeC.children[0].type == 'paragraph' ) { - if( nodeC.children[0].children[0].value[0] == '~' ) { + if (isMultipleChoise) { + 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] == '!') { + } else if (nodeC.children[0].children[0].value[0] === '!') { tab.push(0); - }else { + } else { tab.push(1); } nodeC.children[0].children[0].value = nodeC.children[0].children[0].value.slice(1) + '\r'; } - }) - node.type = 'mc' + }); + node.type = 'mc'; node.data = { - hName: 'fieldset', - hProperties: { - className: 'mc check', - id: 'mc_' + nb_mc - } - } + hName: 'fieldset', + hProperties: { + className: 'mc check', + id: 'mc_' + nbMC + } + }; node.children = [{ - type:'list-item-mc', + type: 'list-item-mc', data: { hName: 'ul', - hProperties:{ - 'style': 'list-style-type: none' + hProperties: { + style: 'list-style-type: none' } }, - children : node.children.map( (x) => ({type:'input-list-item', + children: node.children.map(x => ({type: 'input-list-item', data: { hName: 'li', - hProperties:{ - 'style': 'list-style-type: none' + hProperties: { + style: 'list-style-type: none' } }, children: [ - {type:'input-list-input', - data: { - hName: 'input', - hProperties: { - checked: x.checked, - type:'checkbox', - id: 'mc_'+nb_mc+'_'+nbQ, - className: tab[nbQ] == 0 ? '!' : (tab[nbQ] == 1 ? '=' : '~'), - } - }}, + {type: 'input-list-input', + data: { + hName: 'input', + hProperties: { + checked: x.checked, + type: 'checkbox', + id: 'mc_' + nbMC + '_' + nbQ, + className: tab[nbQ] === 0 ? '!' : (tab[nbQ] === 1 ? '=' : '~') + } + }}, { - type: 'input-list-label', - data: { - hName: 'label', - hProperties: { - for: 'mc_'+nb_mc+'_'+(nbQ++) - } - }, - children: dealLabelChildren(x.children) - }] - }) ) - }, {type:'field-button', + type: 'input-list-label', + data: { + hName: 'label', + hProperties: { + for: 'mc_' + nbMC + '_' + (nbQ++) + } + }, + children: dealLabelChildren(x.children) + }] + })) + }, {type: 'field-button', data: { hName: 'input', - hProperties:{ - 'onclick': 'check(\'mc_'+nb_mc+'\',[' + String(tab) + '])', + hProperties: { + onclick: 'check(\'mc_' + nbMC + '\',[' + String(tab) + '])', value: 'Validate', - type:'button' + type: 'button' } } } - ] - - nb_mc++; + ]; + + nbMC++; } return node; }); @@ -132,8 +131,8 @@ function multipleChoise() { * @param {function} next * @return {object} */ - return function transformer(ast, vFile, next) { - visitList(ast, vFile); + return function (ast, vFile, next) { // Transformer + visitList(ast); if (typeof next === 'function') { return next(null, ast, vFile); diff --git a/package.json b/package.json index 58284f1..3ddf323 100644 --- a/package.json +++ b/package.json @@ -17,5 +17,8 @@ "mermaid", "graphs", "remark" - ] + ], + "devDependencies": { + "xo": "^0.18.2" + } } -- cgit v1.2.3