aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorache <ache@ache.one>2018-12-30 08:08:44 +0100
committerache <ache@ache.one>2018-12-30 08:10:16 +0100
commit26c4c8998ee2cb84c8ce57a79042881cb1ada385 (patch)
tree520e14afe201a8e0e8497769ac52e123f57128d7 /src
parentClean the code and description (diff)
Distribution with babel
Diffstat (limited to 'src')
-rw-r--r--src/index.js89
1 files changed, 89 insertions, 0 deletions
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;