From 26c4c8998ee2cb84c8ce57a79042881cb1ada385 Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 30 Dec 2018 08:08:44 +0100 Subject: Distribution with babel --- .babelrc | 6 ++++ __tests__/index.js | 2 +- app.js | 89 ------------------------------------------------------ package.json | 16 +++++++--- src/index.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 95 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/__tests__/index.js b/__tests__/index.js index ce750fe..b0fb9a3 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -7,7 +7,7 @@ import stringify from 'rehype-stringify'; import remark2rehype from 'remark-rehype'; import parse5 from 'parse5'; -import plugin from '../app'; +import plugin from '..'; const dom5 = require('dom5'); diff --git a/app.js b/app.js deleted file mode 100644 index a267474..0000000 --- a/app.js +++ /dev/null @@ -1,89 +0,0 @@ -'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) { - if (!value.startsWith(START)) { - return; - } - - let subvalue = ''; - let index = START.length; - const {length} = value; - - /* Try to locale the end of the line input */ - 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 with md-attr-parser */ - if (value.charAt(index + END.length) === '{') { - const res = parseAttr(value, index + END.length); - letsEat = res.eaten; - ({prop} = res); - } - - /* Allow some other kind of input */ - if (prop.type !== 'password') { - prop.type = 'text'; - } - - /* Underscores in the placeholder become whitespaces */ - 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; - - // Inject inlineTokenizer - const {inlineTokenizers} = Parser.prototype; - const {inlineMethods} = Parser.prototype; - inlineTokenizers.input = inlineTokenizer; - inlineMethods.splice(inlineMethods.indexOf('url'), 0, 'input'); - - const {Compiler} = this; - - // Stringify - if (Compiler) { - const {visitors} = Compiler.prototype; - visitors.lineinput = function (node) { - return `[__${this.all(node).join('')}__]`; - }; - } -} - -module.exports = plugin; diff --git a/package.json b/package.json index 7785f2a..db64a85 100644 --- a/package.json +++ b/package.json @@ -1,25 +1,31 @@ { "name": "remark-line-input", - "version": "0.3.2", + "version": "0.4.0", "description": "A remark plugin to parse line input syntax", - "main": "app.js", + "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": { "md-attr-parser": "^1.0.0" }, "author": "ache", - "license": "MIT", + "license": "GPL-3.0-or-later", "keywords": [ "line", "input", "remark" ], "devDependencies": { + "@babel/cli": "^7.1.5", + "@babel/core": "^7.1.5", + "@babel/plugin-proposal-object-rest-spread": "^7.0.0", + "@babel/preset-env": "^7.1.5", "ava": "^0.25.0", + "cross-env": "^5.2.0", + "del-cli": "^1.1.0", "dom5": "^3.0.0", "parse5": "^5.0.0", "rehype-raw": "^3.0.0", diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..a267474 --- /dev/null +++ b/src/index.js @@ -0,0 +1,89 @@ +'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) { + if (!value.startsWith(START)) { + return; + } + + let subvalue = ''; + let index = START.length; + const {length} = value; + + /* Try to locale the end of the line input */ + 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 with md-attr-parser */ + if (value.charAt(index + END.length) === '{') { + const res = parseAttr(value, index + END.length); + letsEat = res.eaten; + ({prop} = res); + } + + /* Allow some other kind of input */ + if (prop.type !== 'password') { + prop.type = 'text'; + } + + /* Underscores in the placeholder become whitespaces */ + 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; + + // Inject inlineTokenizer + const {inlineTokenizers} = Parser.prototype; + const {inlineMethods} = Parser.prototype; + inlineTokenizers.input = inlineTokenizer; + inlineMethods.splice(inlineMethods.indexOf('url'), 0, 'input'); + + const {Compiler} = this; + + // Stringify + if (Compiler) { + const {visitors} = Compiler.prototype; + visitors.lineinput = function (node) { + return `[__${this.all(node).join('')}__]`; + }; + } +} + +module.exports = plugin; -- cgit v1.2.3