From 631e06d0c7e8e4a62a1291cb46f6914999a0a014 Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 26 Nov 2017 03:33:39 +0100 Subject: Init commit --- app.js | 142 ++++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 106 insertions(+), 36 deletions(-) diff --git a/app.js b/app.js index ed12659..de2f072 100644 --- a/app.js +++ b/app.js @@ -1,45 +1,115 @@ const visit = require('unist-util-visit'); -const PLUGIN_NAME = 'remark-mermaid-simple'; +const PLUGIN_NAME = 'remark-qcm'; -/** - * Given the MDAST ast, look for all fenced codeblocks that have a language of - * `mermaid` and pass that to mermaid.cli to render the image. Replaces the - * codeblocks with an image of the rendered graph. - * - * @param {object} ast - * @param {vFile} vFile - * @return {function} - */ -function visitCodeBlock(ast, vFile) { - return visit(ast, 'code', (node, index, parent) => { - const { lang, value, position } = node; - - // If this codeblock is not mermaid, bail. - if (lang !== 'mermaid') { - return node; - } +var nb_qcm = 0; - const image = { - type: 'mermaid', - value: value, - data: { - hName: 'div', +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: 'mermaid' - }, - hChildren: [ - { - type: 'text', - value: value - } - ] + className: 'hiden_block_quote' + } } - }; + } + t = t.concat(listChild.slice(1)) + return t +} - parent.children.splice(index, 1, image); +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[0].type == 'paragraph' ) { + if( "~!=".indexOf(nodeC.children[0].children[0].value[0]) < 0) { + 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; }); } @@ -52,7 +122,7 @@ function visitCodeBlock(ast, vFile) { * @link https://github.com/vfile/vfile * @return {function} */ -function mermaid() { +function qcm() { /** * @param {object} ast MDAST * @param {vFile} vFile @@ -60,7 +130,7 @@ function mermaid() { * @return {object} */ return function transformer(ast, vFile, next) { - visitCodeBlock(ast, vFile); + visitList(ast, vFile); if (typeof next === 'function') { return next(null, ast, vFile); @@ -70,4 +140,4 @@ function mermaid() { }; } -module.exports = mermaid; +module.exports = qcm; -- cgit v1.2.3