@podlite/to-jsx 0.0.6 → 0.0.10

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,16 @@
1
1
  # @podlite/to-jsx
2
2
 
3
3
  ## Upcoming
4
-
4
+ ## 0.0.10
5
+ - fix id for =table
6
+ ## 0.0.9
7
+ - fix npm
8
+ ## 0.0.8
9
+ - add support for =Image
10
+ - add caption for =Diagram
11
+ - add support for id html attribute
12
+ ## 0.0.7
13
+ - fix deps
5
14
  ## 0.0.6
6
15
  - fix process empty L<>
7
16
 
package/lib/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export declare type CreateElement = typeof React.createElement;
5
5
  export interface WrapElement {
6
6
  (node: PodNode, ...children: React.ReactChild[]): JSX.Element;
7
7
  }
8
- declare const Podlite: React.FC<{
8
+ export declare const Podlite: React.FC<{
9
9
  [key: string]: any;
10
10
  children?: string;
11
11
  file?: string;
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 `)
@@ -85,18 +90,10 @@ var helperMakeReact = function (_a) {
85
90
  return result;
86
91
  };
87
92
  };
88
- var Podlite = function (_a) {
93
+ export 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
  "]"));
@@ -392,10 +421,11 @@ var mapToReact = function (makeComponent) {
392
421
  console.warn('[jsx] no content in node');
393
422
  return '';
394
423
  }
424
+ var id = getNodeId(node, ctx);
395
425
  return makeComponent(function (_a) {
396
426
  var key = _a.key, children = _a.children;
397
- return React.createElement("table", { key: key },
398
- React.createElement("caption", null, attr.caption),
427
+ return React.createElement("table", { key: key, id: id },
428
+ React.createElement("caption", { className: "caption" }, attr.caption),
399
429
  React.createElement("tbody", null, children));
400
430
  }, node, interator(node.content, __assign({}, ctx)));
401
431
  };
@@ -423,8 +453,35 @@ var mapToReact = function (makeComponent) {
423
453
  if (!(node.content instanceof Array)) {
424
454
  console.error(node);
425
455
  }
426
- return makeComponent('li', node, interator(node.content, __assign({}, ctx)));
456
+ var id = getNodeId(node, ctx);
457
+ return makeComponent('li', node, interator(node.content, __assign({}, ctx)), { id: id });
427
458
  }; },
459
+ // table of content
460
+ ':toc': setFn(function (node, ctx) {
461
+ var tocTitle = node.title;
462
+ return mkComponent(function (_a) {
463
+ var children = _a.children, key = _a.key;
464
+ return React.createElement("div", { className: "toc", key: key },
465
+ tocTitle ? React.createElement("div", { className: "toctitle" }, tocTitle) : '',
466
+ children);
467
+ });
468
+ }),
469
+ ':toc-list': setFn(function (node, ctx) {
470
+ var level = node.level;
471
+ return mkComponent(function (_a) {
472
+ var children = _a.children, key = _a.key;
473
+ return React.createElement("ul", { className: "toc-list listlevel".concat(level), key: key }, children);
474
+ });
475
+ }),
476
+ ':toc-item': subUse({
477
+ // inside head don't wrap into <p>
478
+ ':para': nodeContent,
479
+ }, setFn(function (node, ctx) {
480
+ return mkComponent(function (_a) {
481
+ var children = _a.children, key = _a.key;
482
+ return React.createElement("li", { className: "toc-item", key: key }, children);
483
+ });
484
+ })),
428
485
  };
429
486
  };
430
487
  function podlite(children, _a) {
@@ -449,7 +506,7 @@ function podlite(children, _a) {
449
506
  var writer = new Writer(function (s) {
450
507
  });
451
508
  var res = toAny({ processor: parse })
452
- .use({ '*:*': function (writer, processor) { return function (node, ctx, interator) {
509
+ .use({ '*:*': function () { return function (node, ctx, interator) {
453
510
  // skip named blocks
454
511
  if (isNamedBlock(node.name)) {
455
512
  return null;
@@ -463,7 +520,7 @@ function podlite(children, _a) {
463
520
  }, node, interator(node.content, __assign({}, ctx)));
464
521
  }
465
522
  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));
523
+ return React.createElement('code', { key: ++i_key_i }, "not supported node:".concat(JSON.stringify(node, null, 2)));
467
524
  }; } })
468
525
  .use(rules)
469
526
  .run(ast, writer);
package/package.json CHANGED
@@ -1,37 +1,37 @@
1
1
  {
2
2
  "name": "@podlite/to-jsx",
3
- "version": "0.0.6",
3
+ "version": "0.0.10",
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
7
  "license": "MIT",
8
8
  "scripts": {
9
- "clean": "rm -rf dist lib tsconfig.tsbuildinfo",
10
9
  "build": "tsc",
11
- "test": "jest --verbose"
10
+ "test": "yarn g:jest --passWithNoTests"
12
11
  },
13
12
  "dependencies": {
14
- "@podlite/diagram": "^0.0.6",
15
- "pod6": "^0.0.39",
13
+ "@podlite/diagram": "^0.0.10",
14
+ "@podlite/schema": "0.0.7",
15
+ "pod6": "0.0.43",
16
+ "react": "^16.12.0",
17
+ "react-dom": "^16.12.0",
16
18
  "react-is": "^17.0.2"
17
19
  },
18
20
  "publishConfig": {
19
- "access": "public"
21
+ "access": "public",
22
+ "main": "index.js"
20
23
  },
21
24
  "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"
25
+ "@types/react": "^16.x",
26
+ "@types/react-dom": "^16.x",
27
+ "glob": "^7.2.0",
28
+ "podlite": "0.0.16"
30
29
  },
31
30
  "peerDependencies": {
32
31
  "@podlite/diagram": "*",
32
+ "@podlite/schema": "*",
33
33
  "pod6": "*",
34
- "react": "*"
35
- },
36
- "gitHead": "a4a607de65e342ea89d674628d2b450fa6b53de8"
37
- }
34
+ "react": "*",
35
+ "react-dom": "*"
36
+ }
37
+ }
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)) {
@@ -56,7 +55,7 @@ export interface WrapElement {
56
55
  ...children: React.ReactChild[]
57
56
  ) : JSX.Element
58
57
  }
59
- const Podlite: React.FC<{
58
+ export const Podlite: React.FC<{
60
59
  [key: string]: any
61
60
  children?: string
62
61
  file?:string,
@@ -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
 
@@ -320,9 +339,10 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
320
339
  console.warn('[jsx] no content in node')
321
340
  return ''
322
341
  }
342
+ const id = getNodeId(node, ctx)
323
343
  return makeComponent(({key, children})=>{
324
- return <table key={key}>
325
- <caption>{attr.caption}</caption>
344
+ return <table key={key} id={id}>
345
+ <caption className="caption">{attr.caption}</caption>
326
346
  <tbody>{children}</tbody></table>
327
347
  }, node, interator(node.content, { ...ctx}) )
328
348
  }
@@ -353,8 +373,25 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
353
373
  if (! (node.content instanceof Array)) {
354
374
  console.error(node)
355
375
  }
356
- return makeComponent('li', node, interator(node.content, { ...ctx}) )
376
+ const id = getNodeId(node,ctx)
377
+ return makeComponent('li', node, interator(node.content, { ...ctx}), {id} )
357
378
  },
379
+ // table of content
380
+ ':toc': setFn(( node:Toc, ctx ) => {
381
+ const tocTitle = node.title
382
+ return mkComponent(({children, key })=><div className="toc" key={key}>{ tocTitle ? <div className="toctitle">{tocTitle}</div> : '' }{children}</div>)
383
+ }),
384
+ ':toc-list': setFn(( node, ctx ) => {
385
+ const { level } = node
386
+ return mkComponent(({children, key })=><ul className={`toc-list listlevel${level}`} key={key}>{children}</ul>)
387
+ }),
388
+ ':toc-item': subUse({
389
+ // inside head don't wrap into <p>
390
+ ':para' : nodeContent,
391
+ },
392
+ setFn(( node, ctx ) => {
393
+ return mkComponent(({children, key })=><li className="toc-item" key={key}>{children}</li>)
394
+ })),
358
395
 
359
396
  }
360
397
  }
@@ -383,7 +420,7 @@ function podlite (children:string, { file,plugins=()=>{}, wrapElement, tree}:{f
383
420
  }) as WriterPostinterator
384
421
  const res =
385
422
  toAny({processor:parse})
386
- .use({'*:*':( writer, processor )=>( node, ctx, interator )=>{
423
+ .use({'*:*':() => ( node, ctx, interator ) => {
387
424
 
388
425
  // skip named blocks
389
426
  if (isNamedBlock(node.name)) {