@podlite/to-jsx 0.0.4 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.js +87 -32
- package/package.json +20 -19
- package/src/index.tsx +67 -28
- package/t/index.component.spec.tsx +147 -9
- package/tsconfig.tsbuildinfo +1 -1937
- package/yarn-error.log +8944 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
# @podlite/to-jsx
|
|
2
2
|
|
|
3
3
|
## Upcoming
|
|
4
|
+
## 0.0.8
|
|
5
|
+
- add support for =Image
|
|
6
|
+
- add caption for =Diagram
|
|
7
|
+
- add support for id html attribute
|
|
8
|
+
## 0.0.7
|
|
9
|
+
- fix deps
|
|
10
|
+
## 0.0.6
|
|
11
|
+
- fix process empty L<>
|
|
4
12
|
|
|
13
|
+
## 0.0.5
|
|
14
|
+
- update deps
|
|
15
|
+
- declare tree arg
|
|
5
16
|
## 0.0.4
|
|
6
17
|
- update deps
|
|
7
18
|
## 0.0.3
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import { PodliteExport } from 'podlite';
|
|
2
3
|
import { PodNode } from '@podlite/schema';
|
|
3
4
|
export declare type CreateElement = typeof React.createElement;
|
|
4
5
|
export interface WrapElement {
|
|
@@ -6,9 +7,10 @@ export interface WrapElement {
|
|
|
6
7
|
}
|
|
7
8
|
declare const Podlite: React.FC<{
|
|
8
9
|
[key: string]: any;
|
|
9
|
-
children
|
|
10
|
+
children?: string;
|
|
10
11
|
file?: string;
|
|
11
12
|
plugins?: any;
|
|
12
13
|
wrapElement?: WrapElement;
|
|
14
|
+
tree?: PodliteExport;
|
|
13
15
|
}>;
|
|
14
16
|
export default Podlite;
|
package/lib/index.js
CHANGED
|
@@ -36,10 +36,14 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
36
36
|
}
|
|
37
37
|
return ar;
|
|
38
38
|
};
|
|
39
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
40
|
-
for (var i = 0,
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
40
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
41
|
+
if (ar || !(i in from)) {
|
|
42
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
43
|
+
ar[i] = from[i];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
43
47
|
};
|
|
44
48
|
import React from 'react';
|
|
45
49
|
import toAny from 'pod6/built/exportAny';
|
|
@@ -51,21 +55,22 @@ import { parse } from 'pod6';
|
|
|
51
55
|
import Writer from 'pod6/built/writer';
|
|
52
56
|
import { podlite as podlite_core } from 'podlite';
|
|
53
57
|
import Diagram from '@podlite/diagram';
|
|
54
|
-
|
|
58
|
+
import { getNodeId } from '@podlite/schema';
|
|
55
59
|
var helperMakeReact = function (_a) {
|
|
56
60
|
var wrapElement = _a.wrapElement;
|
|
57
61
|
var i_key_i = 0;
|
|
58
62
|
var mapByType = {};
|
|
59
63
|
var getIdForNode = function (_a) {
|
|
60
64
|
var _b = _a.type, type = _b === void 0 ? "notype" : _b, _c = _a.name, name = _c === void 0 ? "noname" : _c;
|
|
61
|
-
var type_idx = type
|
|
65
|
+
var type_idx = "".concat(type, "_").concat(name);
|
|
62
66
|
if (!mapByType[type_idx]) {
|
|
63
67
|
mapByType[type_idx] = 0;
|
|
64
68
|
}
|
|
65
69
|
++mapByType[type_idx];
|
|
66
|
-
return type_idx
|
|
70
|
+
return "".concat(type_idx, "_").concat(mapByType[type_idx]);
|
|
67
71
|
};
|
|
68
|
-
return function (src, node, children) {
|
|
72
|
+
return function (src, node, children, extraProps) {
|
|
73
|
+
if (extraProps === void 0) { extraProps = {}; }
|
|
69
74
|
// for string return it
|
|
70
75
|
if (typeof node == 'string') {
|
|
71
76
|
return node;
|
|
@@ -74,7 +79,7 @@ var helperMakeReact = function (_a) {
|
|
|
74
79
|
var key = 'type' in node && node.type ? getIdForNode(node) : ++i_key_i;
|
|
75
80
|
var result = typeof src === 'function'
|
|
76
81
|
? src(__assign(__assign({}, attr), { key: key, children: children }), children)
|
|
77
|
-
: React.createElement(src, { key: key }, children);
|
|
82
|
+
: React.createElement(src, __assign(__assign({}, extraProps), { key: key }), children);
|
|
78
83
|
// // create react element and pass key
|
|
79
84
|
// if (!isValidElementType(src)) {
|
|
80
85
|
// throw new Error(`Bad React element for ${ruleName} rule `)
|
|
@@ -87,16 +92,8 @@ var helperMakeReact = function (_a) {
|
|
|
87
92
|
};
|
|
88
93
|
var Podlite = function (_a) {
|
|
89
94
|
var children = _a.children, options = __rest(_a, ["children"]);
|
|
90
|
-
// console.log({podlite:podlite(children, options)})
|
|
91
|
-
// return React.cloneElement(
|
|
92
95
|
var result = podlite(children, options);
|
|
93
96
|
return result;
|
|
94
|
-
// props as React.Props<any>
|
|
95
|
-
// )
|
|
96
|
-
// return React.cloneElement(
|
|
97
|
-
// podlite(children, options),
|
|
98
|
-
// // props as React.Props<any>
|
|
99
|
-
// )
|
|
100
97
|
};
|
|
101
98
|
var mapToReact = function (makeComponent) {
|
|
102
99
|
var mkComponent = function (src) { return function (writer, processor) { return function (node, ctx, interator) {
|
|
@@ -114,7 +111,7 @@ var mapToReact = function (makeComponent) {
|
|
|
114
111
|
if (!nesting) {
|
|
115
112
|
return children;
|
|
116
113
|
}
|
|
117
|
-
var arr = __spreadArray([], __read(Array(nesting).keys()));
|
|
114
|
+
var arr = __spreadArray([], __read(Array(nesting).keys()), false);
|
|
118
115
|
return arr.reduce(function (acc) { return makeComponent('blockquote', node, acc); }, children);
|
|
119
116
|
};
|
|
120
117
|
};
|
|
@@ -134,6 +131,32 @@ var mapToReact = function (makeComponent) {
|
|
|
134
131
|
return React.createElement("code", { key: key },
|
|
135
132
|
React.createElement("pre", null, children));
|
|
136
133
|
}),
|
|
134
|
+
'Image': subUse({
|
|
135
|
+
// inside head don't wrap into <p>
|
|
136
|
+
':image': setFn(function (node, ctx) {
|
|
137
|
+
var linkTo = ctx.link;
|
|
138
|
+
return mkComponent(function (_a) {
|
|
139
|
+
var children = _a.children, key = _a.key;
|
|
140
|
+
var Img = React.createElement("img", { key: key, src: node.src, alt: node.alt });
|
|
141
|
+
return linkTo ? React.createElement("a", { key: key, href: linkTo }, Img) : Img;
|
|
142
|
+
});
|
|
143
|
+
}),
|
|
144
|
+
'caption': mkComponent(function (_a) {
|
|
145
|
+
var children = _a.children, key = _a.key;
|
|
146
|
+
return React.createElement("div", { className: "caption", key: key }, children);
|
|
147
|
+
})
|
|
148
|
+
}, setFn(function (node, ctx) {
|
|
149
|
+
var level = node.level;
|
|
150
|
+
var id = getNodeId(node, ctx);
|
|
151
|
+
var conf = makeAttrs(node, ctx);
|
|
152
|
+
if (conf.exists('link')) {
|
|
153
|
+
ctx.link = conf.getFirstValue('link');
|
|
154
|
+
}
|
|
155
|
+
return mkComponent(function (_a) {
|
|
156
|
+
var children = _a.children, key = _a.key;
|
|
157
|
+
return React.createElement("div", { className: "image_block", key: key, id: id }, children);
|
|
158
|
+
});
|
|
159
|
+
})),
|
|
137
160
|
'image': nodeContent,
|
|
138
161
|
':image': setFn(function (node, ctx) {
|
|
139
162
|
return mkComponent(function (_a) {
|
|
@@ -142,9 +165,14 @@ var mapToReact = function (makeComponent) {
|
|
|
142
165
|
});
|
|
143
166
|
}),
|
|
144
167
|
'Diagram': function () { return function (node, ctx, interator) {
|
|
168
|
+
var conf = makeAttrs(node, ctx);
|
|
169
|
+
var caption = conf.exists("caption")
|
|
170
|
+
? conf.getFirstValue("caption")
|
|
171
|
+
: null;
|
|
172
|
+
var id = getNodeId(node, ctx);
|
|
145
173
|
return makeComponent(function (_a) {
|
|
146
174
|
var children = _a.children, key = _a.key;
|
|
147
|
-
return React.createElement(Diagram, { isError: node.custom, chart: node.content[0].value });
|
|
175
|
+
return React.createElement(Diagram, { id: id, isError: node.custom, caption: caption, chart: node.content[0].value });
|
|
148
176
|
}, node, interator(node.content, __assign({}, ctx)));
|
|
149
177
|
}; },
|
|
150
178
|
':text': function (writer, processor) { return function (node, ctx, interator) {
|
|
@@ -158,9 +186,10 @@ var mapToReact = function (makeComponent) {
|
|
|
158
186
|
':para': nodeContent,
|
|
159
187
|
}, setFn(function (node, ctx) {
|
|
160
188
|
var level = node.level;
|
|
189
|
+
var id = getNodeId(node, ctx);
|
|
161
190
|
return mkComponent(function (_a) {
|
|
162
191
|
var level = _a.level, children = _a.children, key = _a.key;
|
|
163
|
-
return React.createElement("h"
|
|
192
|
+
return React.createElement("h".concat(level), { key: key, id: id }, children);
|
|
164
193
|
});
|
|
165
194
|
})),
|
|
166
195
|
':blankline': emptyContent(),
|
|
@@ -266,10 +295,10 @@ var mapToReact = function (makeComponent) {
|
|
|
266
295
|
}
|
|
267
296
|
var FootNotes = makeComponent(function (_a) {
|
|
268
297
|
var children = _a.children, key = _a.key;
|
|
269
|
-
return React.createElement("div", { key: key
|
|
298
|
+
return React.createElement("div", { key: "".concat(key, "_FOOTNOTES"), className: "footnotes" }, footnotes.map(function (footnote, id) {
|
|
270
299
|
return React.createElement("p", { key: id },
|
|
271
300
|
React.createElement("sup", { id: footnote.fnId, className: "footnote" },
|
|
272
|
-
React.createElement("a", { href: "#"
|
|
301
|
+
React.createElement("a", { href: "#".concat(footnote.fnRefId) },
|
|
273
302
|
"[",
|
|
274
303
|
footnote.gid,
|
|
275
304
|
"]")),
|
|
@@ -288,8 +317,8 @@ var mapToReact = function (makeComponent) {
|
|
|
288
317
|
}
|
|
289
318
|
// get foot note id
|
|
290
319
|
var gid = writer.gid++;
|
|
291
|
-
var fnRefId = "fnref:"
|
|
292
|
-
var fnId = "fn:"
|
|
320
|
+
var fnRefId = "fnref:".concat(gid);
|
|
321
|
+
var fnId = "fn:".concat(gid);
|
|
293
322
|
if (!writer.hasOwnProperty('FOOTNOTES')) {
|
|
294
323
|
writer.FOOTNOTES = [];
|
|
295
324
|
}
|
|
@@ -303,7 +332,7 @@ var mapToReact = function (makeComponent) {
|
|
|
303
332
|
return makeComponent(function (_a) {
|
|
304
333
|
var children = _a.children, key = _a.key;
|
|
305
334
|
return React.createElement("sup", { key: key, id: fnRefId, className: "footnote" },
|
|
306
|
-
React.createElement("a", { href: "#"
|
|
335
|
+
React.createElement("a", { href: "#".concat(fnId) },
|
|
307
336
|
"[",
|
|
308
337
|
gid,
|
|
309
338
|
"]"));
|
|
@@ -333,7 +362,7 @@ var mapToReact = function (makeComponent) {
|
|
|
333
362
|
if (Array.isArray(meta)) {
|
|
334
363
|
meta = meta[0];
|
|
335
364
|
}
|
|
336
|
-
if (typeof meta !== 'string' && 'value' in meta) {
|
|
365
|
+
if (meta && typeof meta !== 'string' && 'value' in meta) {
|
|
337
366
|
meta = meta.value;
|
|
338
367
|
}
|
|
339
368
|
return mkComponent(function (_a) {
|
|
@@ -377,7 +406,7 @@ var mapToReact = function (makeComponent) {
|
|
|
377
406
|
return interator(node.content, ctx);
|
|
378
407
|
}; },
|
|
379
408
|
// table section
|
|
380
|
-
'table
|
|
409
|
+
'table': function (writer, processor) {
|
|
381
410
|
return function (node, ctx, interator) {
|
|
382
411
|
var conf = makeAttrs(node, ctx);
|
|
383
412
|
var attr = { caption: '' };
|
|
@@ -395,7 +424,7 @@ var mapToReact = function (makeComponent) {
|
|
|
395
424
|
return makeComponent(function (_a) {
|
|
396
425
|
var key = _a.key, children = _a.children;
|
|
397
426
|
return React.createElement("table", { key: key },
|
|
398
|
-
React.createElement("caption",
|
|
427
|
+
React.createElement("caption", { className: "caption" }, attr.caption),
|
|
399
428
|
React.createElement("tbody", null, children));
|
|
400
429
|
}, node, interator(node.content, __assign({}, ctx)));
|
|
401
430
|
};
|
|
@@ -423,8 +452,35 @@ var mapToReact = function (makeComponent) {
|
|
|
423
452
|
if (!(node.content instanceof Array)) {
|
|
424
453
|
console.error(node);
|
|
425
454
|
}
|
|
426
|
-
|
|
455
|
+
var id = getNodeId(node, ctx);
|
|
456
|
+
return makeComponent('li', node, interator(node.content, __assign({}, ctx)), { id: id });
|
|
427
457
|
}; },
|
|
458
|
+
// table of content
|
|
459
|
+
':toc': setFn(function (node, ctx) {
|
|
460
|
+
var tocTitle = node.title;
|
|
461
|
+
return mkComponent(function (_a) {
|
|
462
|
+
var children = _a.children, key = _a.key;
|
|
463
|
+
return React.createElement("div", { className: "toc", key: key },
|
|
464
|
+
tocTitle ? React.createElement("div", { className: "toctitle" }, tocTitle) : '',
|
|
465
|
+
children);
|
|
466
|
+
});
|
|
467
|
+
}),
|
|
468
|
+
':toc-list': setFn(function (node, ctx) {
|
|
469
|
+
var level = node.level;
|
|
470
|
+
return mkComponent(function (_a) {
|
|
471
|
+
var children = _a.children, key = _a.key;
|
|
472
|
+
return React.createElement("ul", { className: "toc-list listlevel".concat(level), key: key }, children);
|
|
473
|
+
});
|
|
474
|
+
}),
|
|
475
|
+
':toc-item': subUse({
|
|
476
|
+
// inside head don't wrap into <p>
|
|
477
|
+
':para': nodeContent,
|
|
478
|
+
}, setFn(function (node, ctx) {
|
|
479
|
+
return mkComponent(function (_a) {
|
|
480
|
+
var children = _a.children, key = _a.key;
|
|
481
|
+
return React.createElement("li", { className: "toc-item", key: key }, children);
|
|
482
|
+
});
|
|
483
|
+
})),
|
|
428
484
|
};
|
|
429
485
|
};
|
|
430
486
|
function podlite(children, _a) {
|
|
@@ -449,7 +505,7 @@ function podlite(children, _a) {
|
|
|
449
505
|
var writer = new Writer(function (s) {
|
|
450
506
|
});
|
|
451
507
|
var res = toAny({ processor: parse })
|
|
452
|
-
.use({ '*:*': function (
|
|
508
|
+
.use({ '*:*': function () { return function (node, ctx, interator) {
|
|
453
509
|
// skip named blocks
|
|
454
510
|
if (isNamedBlock(node.name)) {
|
|
455
511
|
return null;
|
|
@@ -463,12 +519,11 @@ function podlite(children, _a) {
|
|
|
463
519
|
}, node, interator(node.content, __assign({}, ctx)));
|
|
464
520
|
}
|
|
465
521
|
console.warn('[podlite] Not supported: ' + JSON.stringify(node, null, 2));
|
|
466
|
-
return React.createElement('code', { key: ++i_key_i }, "not supported node:"
|
|
522
|
+
return React.createElement('code', { key: ++i_key_i }, "not supported node:".concat(JSON.stringify(node, null, 2)));
|
|
467
523
|
}; } })
|
|
468
524
|
.use(rules)
|
|
469
525
|
.run(ast, writer);
|
|
470
526
|
// union main react elements and post processed via onEnd event
|
|
471
|
-
//@ts-ignore
|
|
472
527
|
return new Array().concat(res.interator, writer.postInterator);
|
|
473
528
|
}
|
|
474
529
|
export default Podlite;
|
package/package.json
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@podlite/to-jsx",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "The lightweight, customizable React podlite component.",
|
|
5
|
-
"main": "index.js",
|
|
6
5
|
"types": "lib/index.d.ts",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"license": "MIT",
|
|
8
9
|
"scripts": {
|
|
9
|
-
"clean": "rm -rf dist lib tsconfig.tsbuildinfo",
|
|
10
10
|
"build": "tsc",
|
|
11
|
-
"test": "jest --
|
|
11
|
+
"test": "yarn g:jest --passWithNoTests"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@podlite/diagram": "^0.0.
|
|
15
|
-
"
|
|
14
|
+
"@podlite/diagram": "^0.0.8",
|
|
15
|
+
"@podlite/schema": "0.0.7",
|
|
16
|
+
"pod6": "0.0.43",
|
|
17
|
+
"react": "^16.12.0",
|
|
18
|
+
"react-dom": "^16.12.0",
|
|
16
19
|
"react-is": "^17.0.2"
|
|
17
20
|
},
|
|
18
21
|
"publishConfig": {
|
|
19
|
-
"access": "public"
|
|
22
|
+
"access": "public",
|
|
23
|
+
"main": "index.js"
|
|
20
24
|
},
|
|
21
25
|
"devDependencies": {
|
|
22
|
-
"@types/
|
|
23
|
-
"@types/react": "^
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"react": "16.12.0",
|
|
27
|
-
"react-dom": "^17.0.2",
|
|
28
|
-
"ts-jest": "^26.5.4",
|
|
29
|
-
"typescript": "4.2.3"
|
|
26
|
+
"@types/react": "^16.x",
|
|
27
|
+
"@types/react-dom": "^16.x",
|
|
28
|
+
"glob": "^7.2.0",
|
|
29
|
+
"podlite": "0.0.12"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"@podlite/diagram": "*",
|
|
33
|
+
"@podlite/schema": "*",
|
|
33
34
|
"pod6": "*",
|
|
34
|
-
"react": "*"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
35
|
+
"react": "*",
|
|
36
|
+
"react-dom": "*"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -9,12 +9,12 @@ import {parse} from 'pod6'
|
|
|
9
9
|
import Writer from 'pod6/built/writer'
|
|
10
10
|
import { podlite as podlite_core, PodliteExport } from 'podlite'
|
|
11
11
|
import Diagram, { plugin as DiagramPlugin } from '@podlite/diagram';
|
|
12
|
-
import {Verbatim, PodNode, Text, Rules} from '@podlite/schema'
|
|
12
|
+
import {Verbatim, PodNode, Text, Rules, RulesStrict, getNodeId, BlockImage, BlockNamed, getFromTree, Image} from '@podlite/schema'
|
|
13
|
+
import { Toc } from '@podlite/schema'
|
|
13
14
|
|
|
14
15
|
// interface SetFn { <T>(<T>node, ctx:any) => () => () =>void
|
|
15
16
|
// }
|
|
16
17
|
export type CreateElement = typeof React.createElement
|
|
17
|
-
// const createElement = React.createElement
|
|
18
18
|
const helperMakeReact = ({wrapElement})=>{
|
|
19
19
|
let i_key_i = 0;
|
|
20
20
|
let mapByType= {};
|
|
@@ -24,7 +24,7 @@ const helperMakeReact = ({wrapElement})=>{
|
|
|
24
24
|
++mapByType[type_idx]
|
|
25
25
|
return `${type_idx}_${mapByType[type_idx]}`
|
|
26
26
|
}
|
|
27
|
-
return function(src:string|Function, node:PodNode, children ) {
|
|
27
|
+
return function(src:string|Function, node:PodNode, children, extraProps = {} ) {
|
|
28
28
|
// for string return it
|
|
29
29
|
if (typeof node == 'string') {
|
|
30
30
|
return node
|
|
@@ -32,11 +32,10 @@ const helperMakeReact = ({wrapElement})=>{
|
|
|
32
32
|
const { ...attr} = node
|
|
33
33
|
|
|
34
34
|
const key = 'type' in node && node.type ? getIdForNode(node) : ++i_key_i
|
|
35
|
-
|
|
36
35
|
const result =
|
|
37
36
|
typeof src === 'function'
|
|
38
37
|
? src({ ...attr, key , children}, children)
|
|
39
|
-
: React.createElement(src,{ key }, children )
|
|
38
|
+
: React.createElement(src,{ ...extraProps, key }, children )
|
|
40
39
|
|
|
41
40
|
// // create react element and pass key
|
|
42
41
|
// if (!isValidElementType(src)) {
|
|
@@ -58,27 +57,20 @@ export interface WrapElement {
|
|
|
58
57
|
}
|
|
59
58
|
const Podlite: React.FC<{
|
|
60
59
|
[key: string]: any
|
|
61
|
-
children
|
|
60
|
+
children?: string
|
|
62
61
|
file?:string,
|
|
63
62
|
plugins?:any,
|
|
64
|
-
wrapElement?:WrapElement
|
|
63
|
+
wrapElement?:WrapElement,
|
|
64
|
+
tree?:PodliteExport
|
|
65
65
|
}> = ({ children, ...options }) => {
|
|
66
|
-
// console.log({podlite:podlite(children, options)})
|
|
67
|
-
// return React.cloneElement(
|
|
68
66
|
const result:any = podlite(children, options)
|
|
69
67
|
return result
|
|
70
|
-
// props as React.Props<any>
|
|
71
|
-
// )
|
|
72
|
-
// return React.cloneElement(
|
|
73
|
-
// podlite(children, options),
|
|
74
|
-
// // props as React.Props<any>
|
|
75
|
-
// )
|
|
76
68
|
}
|
|
77
|
-
const mapToReact = (makeComponent):
|
|
69
|
+
const mapToReact = (makeComponent):Partial<RulesStrict> => {
|
|
78
70
|
|
|
79
71
|
const mkComponent = (src) => ( writer, processor )=>( node, ctx, interator )=>{
|
|
80
72
|
// check if node.content defined
|
|
81
|
-
return makeComponent(src, node, 'content' in node ? interator(node.content, { ...ctx}) : []
|
|
73
|
+
return makeComponent(src, node, 'content' in node ? interator(node.content, { ...ctx}) : [])
|
|
82
74
|
}
|
|
83
75
|
// Handle nested block and :nested block attribute
|
|
84
76
|
const handleNested = ( defaultHandler, implicitLevel?:number ) => {
|
|
@@ -105,14 +97,41 @@ const mapToReact = (makeComponent):Rules => {
|
|
|
105
97
|
':ambient': emptyContent(),
|
|
106
98
|
':code': mkComponent(({ children, key })=><code key={key}><pre>{children}</pre></code>),
|
|
107
99
|
'code': mkComponent(({ children, key })=><code key={key}><pre>{children}</pre></code>),
|
|
108
|
-
'
|
|
100
|
+
'Image': subUse({
|
|
101
|
+
// inside head don't wrap into <p>
|
|
102
|
+
':image' : setFn(( node:Image, ctx ) => {
|
|
103
|
+
const linkTo = ctx.link
|
|
104
|
+
return mkComponent(({children, key })=>{
|
|
105
|
+
const Img = <img key={key} src={node.src} alt={node.alt}/>
|
|
106
|
+
return linkTo ?<a key={key} href={linkTo}>{Img}</a> : Img
|
|
107
|
+
})
|
|
108
|
+
}),
|
|
109
|
+
'caption': mkComponent(({ children, key })=><div className="caption" key={key}>{children}</div>)
|
|
110
|
+
},
|
|
111
|
+
setFn(( node, ctx ) => {
|
|
112
|
+
const {level} = node
|
|
113
|
+
const id = getNodeId(node, ctx)
|
|
114
|
+
const conf = makeAttrs(node, ctx)
|
|
115
|
+
if ( conf.exists('link') ) {
|
|
116
|
+
ctx.link = conf.getFirstValue('link')
|
|
117
|
+
}
|
|
118
|
+
return mkComponent(({children, key })=>
|
|
119
|
+
<div className="image_block" key={key} id={id}>{children}</div>)
|
|
120
|
+
})
|
|
121
|
+
),
|
|
122
|
+
'image':nodeContent,
|
|
109
123
|
':image': setFn(( node, ctx ) => {
|
|
110
124
|
return mkComponent(({ children, key })=><img key={key} src={node.src} alt={node.alt}/>)
|
|
111
125
|
}),
|
|
112
126
|
|
|
113
127
|
'Diagram': () => (node, ctx, interator) => {
|
|
128
|
+
const conf = makeAttrs(node, ctx);
|
|
129
|
+
const caption = conf.exists("caption")
|
|
130
|
+
? conf.getFirstValue("caption")
|
|
131
|
+
: null;
|
|
132
|
+
const id = getNodeId(node, ctx)
|
|
114
133
|
return makeComponent(({children, key} )=>{
|
|
115
|
-
return <Diagram
|
|
134
|
+
return <Diagram id={id} isError={node.custom} caption={caption} chart={node.content[0].value}/>
|
|
116
135
|
},node,interator(node.content, { ...ctx}) )
|
|
117
136
|
},
|
|
118
137
|
':text':( writer, processor )=>( node:Text, ctx, interator )=>{
|
|
@@ -126,8 +145,9 @@ const mapToReact = (makeComponent):Rules => {
|
|
|
126
145
|
':para' : nodeContent,
|
|
127
146
|
},
|
|
128
147
|
setFn(( node, ctx ) => {
|
|
129
|
-
const {level} = node
|
|
130
|
-
|
|
148
|
+
const {level } = node
|
|
149
|
+
const id = getNodeId(node, ctx)
|
|
150
|
+
return mkComponent(({level,children, key })=>React.createElement(`h${level}`,{key, id}, children))
|
|
131
151
|
})
|
|
132
152
|
),
|
|
133
153
|
|
|
@@ -268,7 +288,7 @@ const mapToReact = (makeComponent):Rules => {
|
|
|
268
288
|
if ( Array.isArray(meta)) {
|
|
269
289
|
meta = meta[0]
|
|
270
290
|
}
|
|
271
|
-
if (typeof meta !== 'string' && 'value' in meta ) {
|
|
291
|
+
if ( meta && typeof meta !== 'string' && 'value' in meta ) {
|
|
272
292
|
meta = meta.value
|
|
273
293
|
}
|
|
274
294
|
return mkComponent(({children, key })=><a href={meta} key={key}>{children}</a>)
|
|
@@ -302,7 +322,7 @@ const mapToReact = (makeComponent):Rules => {
|
|
|
302
322
|
return interator(node.content, ctx)
|
|
303
323
|
},
|
|
304
324
|
// table section
|
|
305
|
-
'table
|
|
325
|
+
'table' :
|
|
306
326
|
( writer, processor ) =>
|
|
307
327
|
( node, ctx, interator ) =>
|
|
308
328
|
{
|
|
@@ -321,7 +341,7 @@ const mapToReact = (makeComponent):Rules => {
|
|
|
321
341
|
}
|
|
322
342
|
return makeComponent(({key, children})=>{
|
|
323
343
|
return <table key={key}>
|
|
324
|
-
<caption>{attr.caption}</caption>
|
|
344
|
+
<caption className="caption">{attr.caption}</caption>
|
|
325
345
|
<tbody>{children}</tbody></table>
|
|
326
346
|
}, node, interator(node.content, { ...ctx}) )
|
|
327
347
|
}
|
|
@@ -352,8 +372,25 @@ const mapToReact = (makeComponent):Rules => {
|
|
|
352
372
|
if (! (node.content instanceof Array)) {
|
|
353
373
|
console.error(node)
|
|
354
374
|
}
|
|
355
|
-
|
|
375
|
+
const id = getNodeId(node,ctx)
|
|
376
|
+
return makeComponent('li', node, interator(node.content, { ...ctx}), {id} )
|
|
356
377
|
},
|
|
378
|
+
// table of content
|
|
379
|
+
':toc': setFn(( node:Toc, ctx ) => {
|
|
380
|
+
const tocTitle = node.title
|
|
381
|
+
return mkComponent(({children, key })=><div className="toc" key={key}>{ tocTitle ? <div className="toctitle">{tocTitle}</div> : '' }{children}</div>)
|
|
382
|
+
}),
|
|
383
|
+
':toc-list': setFn(( node, ctx ) => {
|
|
384
|
+
const { level } = node
|
|
385
|
+
return mkComponent(({children, key })=><ul className={`toc-list listlevel${level}`} key={key}>{children}</ul>)
|
|
386
|
+
}),
|
|
387
|
+
':toc-item': subUse({
|
|
388
|
+
// inside head don't wrap into <p>
|
|
389
|
+
':para' : nodeContent,
|
|
390
|
+
},
|
|
391
|
+
setFn(( node, ctx ) => {
|
|
392
|
+
return mkComponent(({children, key })=><li className="toc-item" key={key}>{children}</li>)
|
|
393
|
+
})),
|
|
357
394
|
|
|
358
395
|
}
|
|
359
396
|
}
|
|
@@ -375,11 +412,14 @@ function podlite (children:string, { file,plugins=()=>{}, wrapElement, tree}:{f
|
|
|
375
412
|
...mapToReact(makeComponent),
|
|
376
413
|
...plugins(makeComponent)
|
|
377
414
|
}
|
|
415
|
+
interface WriterPostinterator extends Writer {
|
|
416
|
+
postInterator? : any
|
|
417
|
+
}
|
|
378
418
|
const writer = new Writer((s)=>{
|
|
379
|
-
})
|
|
419
|
+
}) as WriterPostinterator
|
|
380
420
|
const res =
|
|
381
421
|
toAny({processor:parse})
|
|
382
|
-
.use({'*:*':(
|
|
422
|
+
.use({'*:*':() => ( node, ctx, interator ) => {
|
|
383
423
|
|
|
384
424
|
// skip named blocks
|
|
385
425
|
if (isNamedBlock(node.name)) {
|
|
@@ -398,7 +438,6 @@ function podlite (children:string, { file,plugins=()=>{}, wrapElement, tree}:{f
|
|
|
398
438
|
.use(rules)
|
|
399
439
|
.run(ast, writer)
|
|
400
440
|
// union main react elements and post processed via onEnd event
|
|
401
|
-
//@ts-ignore
|
|
402
441
|
return new Array().concat( res.interator, writer.postInterator )
|
|
403
442
|
}
|
|
404
443
|
export default Podlite
|