From 48ccaa3eca86b2a284a0a291e5741ff553a54038 Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 31 Dec 2018 16:02:36 +0100 Subject: Use babel --- .babelrc | 6 +++ app.js | 126 ----------------------------------------------------------- package.json | 30 ++++++++------ src/index.js | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 137 deletions(-) create mode 100644 .babelrc delete mode 100644 app.js create mode 100644 src/index.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..cf18521 --- /dev/null +++ b/.babelrc @@ -0,0 +1,6 @@ +{ + "presets": ["@babel/env"], + "plugins": [ + ["@babel/plugin-proposal-object-rest-spread"] + ] +} diff --git a/app.js b/app.js deleted file mode 100644 index 6f8b5d5..0000000 --- a/app.js +++ /dev/null @@ -1,126 +0,0 @@ -const visit = require('unist-util-visit'); - -/** - * Given the MDAST ast, look for all fenced Blockquote - * - * @param {object} ast - * @param {vFile} vFile - * @return {function} - */ -function visitBlockquote(ast) { - return visit(ast, 'blockquote', (node, index, parent) => { - const firstNode = node.children[0]; - - if (firstNode.type === 'paragraph') { - if (firstNode.children[0].type === 'text') { - const firstChild = firstNode.children[0]; - if (firstChild.value.startsWith('!secret')) { - node.type = 'div'; - firstChild.value = firstChild.value.substr(7); - let sum = ''; - if (firstChild.value.indexOf('\n') >= 0) { - sum = firstChild.value.substr(0, - firstChild.value.indexOf('\n')); - firstChild.value = firstChild.value.substr( - firstChild.value.indexOf('\n')); - } else { - sum = firstChild.value; - firstChild.value = ''; - } - - const secret = { - type: 'special-box-secret', - children: [ - node, - { - type: 'summary', - data: { - hName: 'summary', - hChildren: [{ - type: 'text', - value: sum || 'Spoiler', - }], - }, - }, - ], - data: { - hName: 'details', - hProperties: { - className: 'special-box secret', - }, - }, - }; - - parent.children.splice(index, 1, secret); - - return node; - } else if (firstChild.value.startsWith('!information') || - firstChild.value.startsWith('!good') || - firstChild.value.startsWith('!bad') || - firstChild.value.startsWith('!comment') || - firstChild.value.startsWith('!attention') || - firstChild.value.startsWith('!question')) { - node.type = 'div'; - node.data = { - hName: 'div', - hProperties: { - className: 'special-box-content', - }, - }; - let type = ''; - if (firstChild.value.indexOf('\n') > 0) { - type = firstChild.value.substr(1, firstChild.value.indexOf('\n')); - firstChild.value = firstChild.value.substr(firstChild.value.indexOf('\n')); - } else { - type = firstChild.value.substr(1); - firstChild.value = ''; - } - - const box = { - type: 'special-box-div', - children: [node], - data: { - hName: 'div', - hProperties: { - className: `special-box ${type}`, - }, - }, - }; - - parent.children.splice(index, 1, box); - - return node; - } - } - } - 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 box() { - /** - * @param {object} ast MDAST - * @param {vFile} vFile - * @param {function} next - * @return {object} - */ - return function (ast, vFile, next) { // Transformer - visitBlockquote(ast); - - if (typeof next === 'function') { - return next(null, ast, vFile); - } - - return ast; - }; -} - -module.exports = box; diff --git a/package.json b/package.json index 1b31927..0dd6b49 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "remark-special-box", "version": "0.1.0", - "description": "A remark plugin for Markdown that parse special blox", - "main": "app.js", + "description": "A remark plugin for markdown that parse special blox", + "main": "dist/index.js", "scripts": { "pretest": "xo", - "test": "ava", - "start": "node app.js" + "prepare": "del-cli dist && cross-env BABEL_ENV=production babel src --out-dir dist", + "test": "ava" }, "dependencies": { "unist-util-visit": "^1.1.3" @@ -18,13 +18,21 @@ "remark" ], "devDependencies": { - "ava": "^0.25.0", - "rehype-raw": "^2.0.0", - "rehype-stringify": "^3.0.0", - "remark-parse": "^5.0.0", - "remark-rehype": "^3.0.0", - "unified": "^6.1.6", - "xo": "^0.18.2" + "@babel/cli": "7.x", + "@babel/core": "7.x", + "@babel/plugin-proposal-object-rest-spread": "7.x", + "@babel/preset-env": "7.x", + "ava": "1.x", + "cross-env": "5.x", + "del-cli": "1.x", + "dom5": "3.x", + "parse5": "5.x", + "rehype-raw": "4.x", + "rehype-stringify": "5.x", + "remark-parse": "6.x", + "remark-rehype": "4.x", + "unified": "7.x", + "xo": "0.23.x" }, "xo": { "space": true, diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..6f8b5d5 --- /dev/null +++ b/src/index.js @@ -0,0 +1,126 @@ +const visit = require('unist-util-visit'); + +/** + * Given the MDAST ast, look for all fenced Blockquote + * + * @param {object} ast + * @param {vFile} vFile + * @return {function} + */ +function visitBlockquote(ast) { + return visit(ast, 'blockquote', (node, index, parent) => { + const firstNode = node.children[0]; + + if (firstNode.type === 'paragraph') { + if (firstNode.children[0].type === 'text') { + const firstChild = firstNode.children[0]; + if (firstChild.value.startsWith('!secret')) { + node.type = 'div'; + firstChild.value = firstChild.value.substr(7); + let sum = ''; + if (firstChild.value.indexOf('\n') >= 0) { + sum = firstChild.value.substr(0, + firstChild.value.indexOf('\n')); + firstChild.value = firstChild.value.substr( + firstChild.value.indexOf('\n')); + } else { + sum = firstChild.value; + firstChild.value = ''; + } + + const secret = { + type: 'special-box-secret', + children: [ + node, + { + type: 'summary', + data: { + hName: 'summary', + hChildren: [{ + type: 'text', + value: sum || 'Spoiler', + }], + }, + }, + ], + data: { + hName: 'details', + hProperties: { + className: 'special-box secret', + }, + }, + }; + + parent.children.splice(index, 1, secret); + + return node; + } else if (firstChild.value.startsWith('!information') || + firstChild.value.startsWith('!good') || + firstChild.value.startsWith('!bad') || + firstChild.value.startsWith('!comment') || + firstChild.value.startsWith('!attention') || + firstChild.value.startsWith('!question')) { + node.type = 'div'; + node.data = { + hName: 'div', + hProperties: { + className: 'special-box-content', + }, + }; + let type = ''; + if (firstChild.value.indexOf('\n') > 0) { + type = firstChild.value.substr(1, firstChild.value.indexOf('\n')); + firstChild.value = firstChild.value.substr(firstChild.value.indexOf('\n')); + } else { + type = firstChild.value.substr(1); + firstChild.value = ''; + } + + const box = { + type: 'special-box-div', + children: [node], + data: { + hName: 'div', + hProperties: { + className: `special-box ${type}`, + }, + }, + }; + + parent.children.splice(index, 1, box); + + return node; + } + } + } + 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 box() { + /** + * @param {object} ast MDAST + * @param {vFile} vFile + * @param {function} next + * @return {object} + */ + return function (ast, vFile, next) { // Transformer + visitBlockquote(ast); + + if (typeof next === 'function') { + return next(null, ast, vFile); + } + + return ast; + }; +} + +module.exports = box; -- cgit v1.3-2-g11bf