@podlite/to-jsx 0.0.7 → 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 CHANGED
@@ -1,7 +1,10 @@
1
1
  # @podlite/to-jsx
2
2
 
3
3
  ## Upcoming
4
-
4
+ ## 0.0.8
5
+ - add support for =Image
6
+ - add caption for =Diagram
7
+ - add support for id html attribute
5
8
  ## 0.0.7
6
9
  - fix deps
7
10
  ## 0.0.6
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, il = from.length, j = to.length; i < il; i++, j++)
41
- to[j] = from[i];
42
- return to;
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
- // const createElement = React.createElement
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 + "_" + name;
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 + "_" + mapByType[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" + level, { key: key }, children);
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 + "_FOOTNOTES", className: "footnotes" }, footnotes.map(function (footnote, id) {
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: "#" + footnote.fnRefId },
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:" + gid;
292
- var fnId = "fn:" + gid;
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: "#" + fnId },
335
+ React.createElement("a", { href: "#".concat(fnId) },
307
336
  "[",
308
337
  gid,
309
338
  "]"));
@@ -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", null, attr.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
- return makeComponent('li', node, interator(node.content, __assign({}, ctx)));
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 (writer, processor) { return function (node, ctx, interator) {
508
+ .use({ '*:*': function () { return function (node, ctx, interator) {
453
509
  // skip named blocks
454
510
  if (isNamedBlock(node.name)) {
455
511
  return null;
@@ -463,7 +519,7 @@ 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:" + JSON.stringify(node, null, 2));
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);
package/package.json CHANGED
@@ -1,37 +1,38 @@
1
1
  {
2
2
  "name": "@podlite/to-jsx",
3
- "version": "0.0.7",
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 --verbose"
11
+ "test": "yarn g:jest --passWithNoTests"
12
12
  },
13
13
  "dependencies": {
14
- "@podlite/diagram": "^0.0.7",
15
- "pod6": "0.0.41",
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/jest": "^26.0.22",
23
- "@types/react": "^17.0.3",
24
- "jest-serializer-html": "^7.0.0",
25
- "prettier": "^2.2.1",
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
- "gitHead": "eaae7ef7bee8588d8dbe3abbff3a3a03e43b849e"
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, RulesStrict} 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)) {
@@ -64,22 +63,14 @@ const Podlite: React.FC<{
64
63
  wrapElement?:WrapElement,
65
64
  tree?:PodliteExport
66
65
  }> = ({ children, ...options }) => {
67
- // console.log({podlite:podlite(children, options)})
68
- // return React.cloneElement(
69
66
  const result:any = podlite(children, options)
70
67
  return result
71
- // props as React.Props<any>
72
- // )
73
- // return React.cloneElement(
74
- // podlite(children, options),
75
- // // props as React.Props<any>
76
- // )
77
68
  }
78
69
  const mapToReact = (makeComponent):Partial<RulesStrict> => {
79
70
 
80
71
  const mkComponent = (src) => ( writer, processor )=>( node, ctx, interator )=>{
81
72
  // check if node.content defined
82
- return makeComponent(src, node, 'content' in node ? interator(node.content, { ...ctx}) : [] )
73
+ return makeComponent(src, node, 'content' in node ? interator(node.content, { ...ctx}) : [])
83
74
  }
84
75
  // Handle nested block and :nested block attribute
85
76
  const handleNested = ( defaultHandler, implicitLevel?:number ) => {
@@ -106,14 +97,41 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
106
97
  ':ambient': emptyContent(),
107
98
  ':code': mkComponent(({ children, key })=><code key={key}><pre>{children}</pre></code>),
108
99
  'code': mkComponent(({ children, key })=><code key={key}><pre>{children}</pre></code>),
109
- 'image': nodeContent,
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,
110
123
  ':image': setFn(( node, ctx ) => {
111
124
  return mkComponent(({ children, key })=><img key={key} src={node.src} alt={node.alt}/>)
112
125
  }),
113
126
 
114
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)
115
133
  return makeComponent(({children, key} )=>{
116
- return <Diagram isError={node.custom} chart={node.content[0].value}/>
134
+ return <Diagram id={id} isError={node.custom} caption={caption} chart={node.content[0].value}/>
117
135
  },node,interator(node.content, { ...ctx}) )
118
136
  },
119
137
  ':text':( writer, processor )=>( node:Text, ctx, interator )=>{
@@ -127,8 +145,9 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
127
145
  ':para' : nodeContent,
128
146
  },
129
147
  setFn(( node, ctx ) => {
130
- const {level} = node
131
- return mkComponent(({level,children, key })=>React.createElement(`h${level}`,{key}, children))
148
+ const {level } = node
149
+ const id = getNodeId(node, ctx)
150
+ return mkComponent(({level,children, key })=>React.createElement(`h${level}`,{key, id}, children))
132
151
  })
133
152
  ),
134
153
 
@@ -322,7 +341,7 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
322
341
  }
323
342
  return makeComponent(({key, children})=>{
324
343
  return <table key={key}>
325
- <caption>{attr.caption}</caption>
344
+ <caption className="caption">{attr.caption}</caption>
326
345
  <tbody>{children}</tbody></table>
327
346
  }, node, interator(node.content, { ...ctx}) )
328
347
  }
@@ -353,8 +372,25 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
353
372
  if (! (node.content instanceof Array)) {
354
373
  console.error(node)
355
374
  }
356
- return makeComponent('li', node, interator(node.content, { ...ctx}) )
375
+ const id = getNodeId(node,ctx)
376
+ return makeComponent('li', node, interator(node.content, { ...ctx}), {id} )
357
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
+ })),
358
394
 
359
395
  }
360
396
  }
@@ -383,7 +419,7 @@ function podlite (children:string, { file,plugins=()=>{}, wrapElement, tree}:{f
383
419
  }) as WriterPostinterator
384
420
  const res =
385
421
  toAny({processor:parse})
386
- .use({'*:*':( writer, processor )=>( node, ctx, interator )=>{
422
+ .use({'*:*':() => ( node, ctx, interator ) => {
387
423
 
388
424
  // skip named blocks
389
425
  if (isNamedBlock(node.name)) {