├── .gitignore
├── README.md
├── html2cell.js
├── index.js
├── jsx2cell.js
├── node2cell.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | package-lock.json
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### convert structures to [cell](https://github.com/intercellular/cell)
2 |
3 | ```js
4 | > const to_cell = require('to_cell')
5 | undefined
6 | > to_cell.jsx('
')
7 | [ { '$type': 'h1', '$cell': true } ]
8 | > to_cell.html('')
9 | [ { '$type': 'h1', '$cell': true } ]
10 | > to_cell.jsx(' {}}>
')
11 | [ { '$type': 'h1', '$cell': true, onclick: [Function] } ]
12 | > to_cell.jsx(' {}}>hi
')
13 | [ { '$type': 'h1',
14 | '$cell': true,
15 | onclick: [Function],
16 | '$text': 'hi' } ]
17 | > to_cell.html('hi
')
18 | [ { '$type': 'h1', '$cell': true, '$text': 'hi' } ]
19 | ```
20 |
--------------------------------------------------------------------------------
/html2cell.js:
--------------------------------------------------------------------------------
1 | const parse5 = require('parse5');
2 | const node2cell = require('./node2cell');
3 |
4 | module.exports = function convert(str) {
5 | return parse5.parseFragment(str).childNodes.map(n => node2cell(n, true));
6 | };
7 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | html: require('./html2cell'),
3 | jsx: require('./jsx2cell'),
4 | };
5 |
--------------------------------------------------------------------------------
/jsx2cell.js:
--------------------------------------------------------------------------------
1 | const parse = require('jsx-parser/index.umd.js');
2 | const node2cell = require('./node2cell');
3 |
4 | function toStandard(node, parent = null) {
5 | const specNode = {
6 | nodeName: node.type,
7 | tagName: node.type,
8 | parentNode: parent,
9 | };
10 | if (node.props) {
11 | specNode.attrs = Object.entries(node.props).map(([name, prop]) => {
12 | if (typeof prop === 'string') return { name, type: '#text', value: prop };
13 | return {
14 | name,
15 | value: prop.nodeValue,
16 | type: prop.type,
17 | };
18 | });
19 | }
20 | if (node.type === '#text') specNode.value = node.nodeValue;
21 | if (node.children) specNode.childNodes = node.children.map(c => toStandard(c, specNode));
22 | return specNode;
23 | }
24 |
25 | module.exports = (str) => {
26 | return [node2cell(toStandard(parse(str)), true)];
27 | };
28 |
--------------------------------------------------------------------------------
/node2cell.js:
--------------------------------------------------------------------------------
1 | module.exports = function node2cell(node, top = false) {
2 | const cell = {
3 | $type: node.nodeName,
4 | };
5 | if (top) cell.$cell = true;
6 | if (node.attrs) for (const a of node.attrs) {
7 | if (a.type && a.type === '#jsx') a.value = eval(a.value);
8 | cell[a.name] = a.value;
9 | }
10 | const $components = [];
11 | for (const child of node.childNodes || []) {
12 | if (child.nodeName === '#text') {
13 | if (!child.value.trim()) continue;
14 | if (node.childNodes.length === 1) cell.$text = child.value;
15 | else $components.push({ $type: 'text', $text: child.value });
16 | } else {
17 | $components.push(node2cell(child));
18 | }
19 | }
20 | if ($components.length) cell.$components = $components;
21 | return cell;
22 | }
23 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "to_cell",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "author": "snek ",
10 | "license": "MIT",
11 | "dependencies": {
12 | "jsx-parser": "^1.0.8",
13 | "parse5": "^3.0.2"
14 | }
15 | }
16 |
--------------------------------------------------------------------------------