aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2018-04-29 10:13:51 +0200
committerache <ache@ache.one>2018-04-29 10:14:32 +0200
commit59c8cab6a8cc9258a0f6442299ea6368ce11b306 (patch)
tree605d686981f88726b16b4a9eafa70adcb69fdfdd
parentFix an URL (diff)
Fix the old bug with raw interpretation
-rw-r--r--__tests__/__snapshots__/index.js.md18
-rw-r--r--__tests__/__snapshots__/index.js.snapbin204 -> 167 bytes
-rw-r--r--__tests__/index.js39
-rw-r--r--app.js215
-rw-r--r--package.json5
5 files changed, 45 insertions, 232 deletions
diff --git a/__tests__/__snapshots__/index.js.md b/__tests__/__snapshots__/index.js.md
index 5bcf487..881259a 100644
--- a/__tests__/__snapshots__/index.js.md
+++ b/__tests__/__snapshots__/index.js.md
@@ -4,28 +4,16 @@ The actual snapshot is saved in `index.js.snap`.
Generated by [AVA](https://ava.li).
-## text raw
-
-> Snapshot 1
-
- `<p>This is a text : </p>␊
- <textarea></textarea>`
-
-## text simple
-
-> Snapshot 1
-
- '<p>This is a text : </p>
-
## snapshot raw
> Snapshot 1
`<p>This is a text : </p>␊
- <textarea></textarea>`
+ <TEXTAREA></TEXTAREA>`
## snapshot simple
> Snapshot 1
- '<p>This is a text : </p>' \ No newline at end of file
+ `<p>this is a text : </p>␊
+ <textarea></textarea>`
diff --git a/__tests__/__snapshots__/index.js.snap b/__tests__/__snapshots__/index.js.snap
index 8bfbb1f..5e0f63c 100644
--- a/__tests__/__snapshots__/index.js.snap
+++ b/__tests__/__snapshots__/index.js.snap
Binary files differ
diff --git a/__tests__/index.js b/__tests__/index.js
index e37b64f..38a3dbf 100644
--- a/__tests__/index.js
+++ b/__tests__/index.js
@@ -27,7 +27,7 @@ const renderRaw = text => unified()
test('snapshot simple', t => {
const {contents} = render(file(join(__dirname, 'text.md')));
- t.snapshot(contents);
+ t.snapshot(contents.toLowerCase());
});
test('snapshot raw', t => {
@@ -37,36 +37,36 @@ test('snapshot raw', t => {
test('emty', t => {
const {contents} = render('[____\n____]');
- t.is(contents, '<textarea></textarea>');
+ t.is(contents.toLowerCase(), '<textarea></textarea>');
});
test('emty-raw', t => {
const {contents} = renderRaw('[____\n____]');
- t.is(contents, '<textarea></textarea>');
+ t.is(contents.toLowerCase(), '<textarea></textarea>');
});
test('simple', t => {
const {contents} = render('[____\nHere some text\n____]');
- t.is(contents, '<textarea>Here some text</textarea>');
+ t.is(contents.toLowerCase(), '<textarea>here some text</textarea>');
});
test('simple-raw', t => {
const {contents} = renderRaw('[____\nHere some text\n____]');
- t.is(contents, '<textarea>Here some text</textarea>');
+ t.is(contents.toLowerCase(), '<textarea>here some text</textarea>');
});
test('long', t => {
const {contents} = render(`
[_______
-Here some awesome text !
-With severale lines, ...
+here some awesome text !
+with severale lines, ...
a good text area
_______]`);
- t.is(contents, `<textarea>
-Here some awesome text !
-With severale lines, ...
+ t.is(contents.toLowerCase(), `<textarea>
+here some awesome text !
+with severale lines, ...
a good text area</textarea>`);
});
@@ -75,14 +75,14 @@ test('long-raw', t => {
const {contents} = renderRaw(`
[_______
-Here some awesome text !
-With severale lines, ...
+here some awesome text !
+with severale lines, ...
a good text area
_______]`);
- t.is(contents, `<textarea>
-Here some awesome text !
-With severale lines, ...
+ t.is(contents.toLowerCase(), `<textarea>
+here some awesome text !
+with severale lines, ...
a good text area</textarea>`);
});
@@ -90,9 +90,14 @@ a good text area</textarea>`);
test('not a text-area', t => {
const {contents} = renderRaw(`
[_______some text
-Oups bad-formated text area
+oups bad-formated text area
_______]`);
- t.notRegex(contents, /textarea/);
+ t.notRegex(contents.toLowerCase(), /textarea/);
+});
+
+test('simple-raw2', t => {
+ const {contents} = renderRaw('[____\nHere some text\n____]\n\n<div>Yeah !</div>');
+ t.is(contents.toLowerCase(), '<textarea>here some text</textarea>');
});
test.todo('id text');
diff --git a/app.js b/app.js
index a904757..a37436e 100644
--- a/app.js
+++ b/app.js
@@ -3,150 +3,12 @@
const START = /^(\[_+)\n/g;
const END = /\n(_+])/g;
+const parseAttr = require('md-attr-parser');
+
function locator(value, fromIndex) {
const index = value.indexOf(START, fromIndex);
return index;
}
-function prop2HTML(prop) {
- let html = '';
-
- if ('id' in prop && prop.id) {
- html += ` id=${prop.id}`;
- }
- if ('class' in prop && prop.class) {
- html += ` class="${prop.class.join(' ')}"`;
- }
- if ('key' in prop && prop.key) {
- Object.entries(prop.key).forEach(
- ([key,
- value,
- ]) => {
- html += ' ';
- if (value) {
- html += `${key}="${value}"`;
- } else {
- html += key;
- }
- }
- );
- }
-
- return html;
-}
-
-function parseHTMLparam(value, indexNext) {
- let letsEat = '{';
- indexNext++;
-
- const eat = chars => {
- let eaten = '';
- while (chars.indexOf(value.charAt(indexNext)) >= 0) {
- letsEat += value.charAt(indexNext);
- eaten += value.charAt(indexNext);
- indexNext++;
- }
- return eaten;
- };
- const eatUntil = chars => {
- let eaten = '';
- while (chars.indexOf(value.charAt(indexNext)) < 0) {
- letsEat += value.charAt(indexNext);
- eaten += value.charAt(indexNext);
- indexNext++;
- }
- return eaten;
- };
-
- const prop =
- {key: undefined /* {} */,
- class: undefined /* [] */,
- id: undefined,
- };
- let type;
-
- while (value.charAt(indexNext) !== '}') {
- let labelFirst = '';
- let labelSecond;
-
- eat(' \t\n\r\v');
-
- if (value.charAt(indexNext) === '}') { // Fin l'accolade
- continue;
- } else if (value.charAt(indexNext) === '.') { // Classes
- type = 'class';
- indexNext++;
- letsEat += '.';
- } else if (value.charAt(indexNext) === '#') { // ID
- type = 'id';
- indexNext++;
- letsEat += '#';
- } else { // Key
- type = 'key';
- }
-
- // Extract name
- labelFirst = eatUntil('=\t\b\r\v  }');
-
- if (value.charAt(indexNext) === '=') { // Set labelSecond
- indexNext++;
- letsEat += '=';
-
- if (value.charAt(indexNext) === '"') {
- indexNext++;
- letsEat += '"';
- labelSecond = eatUntil('"}\n');
-
- if (value.charAt(indexNext) === '"') {
- indexNext++;
- letsEat += '"';
- } else {
- // Erreur
- }
- } else if (value.charAt(indexNext) === '\'') {
- indexNext++;
- letsEat += '\'';
- labelSecond = eatUntil('\'}\n');
-
- if (value.charAt(indexNext) === '\'') {
- indexNext++;
- letsEat += '\'';
- } else {
- // Erreur
- }
- } else {
- labelSecond = eatUntil(' \t\n\r\v=}');
- }
- }
- switch (type) {
- case 'id': // ID
- prop.id = labelFirst;
- break;
- case 'class':
- if (!prop.class) {
- prop.class = [];
- }
- prop.class.push(labelFirst);
- break;
- case 'key':
- if (!prop.key) {
- prop.key = {};
- }
- if (labelFirst !== 'id' && labelFirst !== 'class') {
- prop.key[labelFirst] = labelSecond || '';
- }
- break;
- default:
- // Default
- }
- }
- letsEat += '}';
-
- return {
- type,
- prop,
- eaten: letsEat,
- };
-}
function plugin() {
function blockTokenizer(eat, value) {
@@ -155,72 +17,30 @@ function plugin() {
}
let prop = {
- key: undefined /* {} */,
class: undefined /* [] */,
id: undefined,
};
let eaten = '';
- if (value.charAt(value.search(END) + value.match(END)[0].length) === '{') {
- const res = parseHTMLparam(value, value.search(END) + value.match(END)[0].length);
- eaten = res.eaten;
- prop = res.prop;
- }
-
if (value.search(END) > 0) {
- return eat(value.slice(0, value.search(END)) + value.match(END)[0] + eaten)({
- type: 'html',
- value: `<textarea${prop2HTML(prop)}>` +
- `${value.slice(value.match(START)[0].length, value.search(END))}`
- '</textarea>',
- /*
-
- Type: 'form',
- data: {
- hName:'form',
- hChildren : [ {
- type: 'element',
- tagName:'div',
- properties: {},
- children: [ {
- type:'element',
- tagName: 'textarea',
- properties: prop,
- children: [ { type: 'text',
- value: value.slice(value.match(START)[0].length+1, value.search(END)-1 )
- } ]
- }]
- }, { type:'element', tagName:'div', properties:{} } ]
- }
-
- type: 'form',
- children: [ {
- type: 'texterea',
- children: [ { type: 'text',
- value: value.slice(value.match(START)[0].length+1, value.search(END)-1 )
- } ],
- data: {
- hName: 'textarea'
- }
- }],
- */
- /*
+ if ((value.search(END) + value.match(END)[0].length) < value.length &&
+ value.charAt(value.search(END) + value.match(END)[0].length) === '{') {
+ const res = parseAttr(value, value.search(END) + value.match(END)[0].length);
+ eaten = res.eaten;
+ prop = res.prop;
+ }
+ const t = eat(value.slice(0, value.match(END)[0].lenght) + eaten)({
+ type: 'textarea',
data: {
- hName: 'form',
- hChildren : [ {
- type: 'texterea',
- data: {
- hName: 'textarea',
- hChildren : [ { type: 'text',
- value: value.slice(value.match(START)[0].length+1, value.search(END)-1 )
- } ]
- }
- }]
- }
- hName: 'form'
- } */
+ hName: 'TEXTAREA',
+ hProperties: prop,
+ hChildren: [{type: 'text',
+ value: value.slice(value.match(START)[0].length, value.search(END)),
+ }],
+ },
});
+ return t;
}
return true;
}
@@ -229,7 +49,6 @@ function plugin() {
const Parser = this.Parser;
- // Inject blockTokenizer
const blockTokenizers = Parser.prototype.blockTokenizers;
const blockMethods = Parser.prototype.blockMethods;
blockTokenizers.textinput = blockTokenizer;
diff --git a/package.json b/package.json
index 14c9ff1..0853f65 100644
--- a/package.json
+++ b/package.json
@@ -1,10 +1,11 @@
{
"name": "remark-text-input",
- "version": "0.1.0",
+ "version": "0.5.0",
"description": "A remark plugin that parse a syntax for textarea",
"main": "app.js",
"dependencies": {
- "unist-util-visit": "^1.1.3"
+ "unist-util-visit": "^1.1.3",
+ "md-attr-parser": "^1.1.0"
},
"scripts": {
"pretest": "xo",