@podlite/to-jsx 0.0.7 → 0.0.11

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,17 @@
1
1
  # @podlite/to-jsx
2
2
 
3
3
  ## Upcoming
4
-
4
+ ## 0.0.11
5
+ - use <video> tags for mov/mp4 media
6
+ - update deps
7
+ ## 0.0.10
8
+ - fix id for =table
9
+ ## 0.0.9
10
+ - fix npm
11
+ ## 0.0.8
12
+ - add support for =Image
13
+ - add caption for =Diagram
14
+ - add support for id html attribute
5
15
  ## 0.0.7
6
16
  - fix deps
7
17
  ## 0.0.6
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,37 @@ 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 = node.src.match(/(mp4|mov)$/)
141
+ ? React.createElement("video", { controls: true, key: key },
142
+ " ",
143
+ React.createElement("source", { src: node.src, type: "video/mp4" }),
144
+ " ")
145
+ : React.createElement("img", { key: key, src: node.src, alt: node.alt });
146
+ return linkTo ? React.createElement("a", { key: key, href: linkTo }, Img) : Img;
147
+ });
148
+ }),
149
+ 'caption': mkComponent(function (_a) {
150
+ var children = _a.children, key = _a.key;
151
+ return React.createElement("div", { className: "caption", key: key }, children);
152
+ })
153
+ }, setFn(function (node, ctx) {
154
+ var level = node.level;
155
+ var id = getNodeId(node, ctx);
156
+ var conf = makeAttrs(node, ctx);
157
+ if (conf.exists('link')) {
158
+ ctx.link = conf.getFirstValue('link');
159
+ }
160
+ return mkComponent(function (_a) {
161
+ var children = _a.children, key = _a.key;
162
+ return React.createElement("div", { className: "image_block", key: key, id: id }, children);
163
+ });
164
+ })),
137
165
  'image': nodeContent,
138
166
  ':image': setFn(function (node, ctx) {
139
167
  return mkComponent(function (_a) {
@@ -142,9 +170,14 @@ var mapToReact = function (makeComponent) {
142
170
  });
143
171
  }),
144
172
  'Diagram': function () { return function (node, ctx, interator) {
173
+ var conf = makeAttrs(node, ctx);
174
+ var caption = conf.exists("caption")
175
+ ? conf.getFirstValue("caption")
176
+ : null;
177
+ var id = getNodeId(node, ctx);
145
178
  return makeComponent(function (_a) {
146
179
  var children = _a.children, key = _a.key;
147
- return React.createElement(Diagram, { isError: node.custom, chart: node.content[0].value });
180
+ return React.createElement(Diagram, { id: id, isError: node.custom, caption: caption, chart: node.content[0].value });
148
181
  }, node, interator(node.content, __assign({}, ctx)));
149
182
  }; },
150
183
  ':text': function (writer, processor) { return function (node, ctx, interator) {
@@ -158,9 +191,10 @@ var mapToReact = function (makeComponent) {
158
191
  ':para': nodeContent,
159
192
  }, setFn(function (node, ctx) {
160
193
  var level = node.level;
194
+ var id = getNodeId(node, ctx);
161
195
  return mkComponent(function (_a) {
162
196
  var level = _a.level, children = _a.children, key = _a.key;
163
- return React.createElement("h" + level, { key: key }, children);
197
+ return React.createElement("h".concat(level), { key: key, id: id }, children);
164
198
  });
165
199
  })),
166
200
  ':blankline': emptyContent(),
@@ -266,10 +300,10 @@ var mapToReact = function (makeComponent) {
266
300
  }
267
301
  var FootNotes = makeComponent(function (_a) {
268
302
  var children = _a.children, key = _a.key;
269
- return React.createElement("div", { key: key + "_FOOTNOTES", className: "footnotes" }, footnotes.map(function (footnote, id) {
303
+ return React.createElement("div", { key: "".concat(key, "_FOOTNOTES"), className: "footnotes" }, footnotes.map(function (footnote, id) {
270
304
  return React.createElement("p", { key: id },
271
305
  React.createElement("sup", { id: footnote.fnId, className: "footnote" },
272
- React.createElement("a", { href: "#" + footnote.fnRefId },
306
+ React.createElement("a", { href: "#".concat(footnote.fnRefId) },
273
307
  "[",
274
308
  footnote.gid,
275
309
  "]")),
@@ -288,8 +322,8 @@ var mapToReact = function (makeComponent) {
288
322
  }
289
323
  // get foot note id
290
324
  var gid = writer.gid++;
291
- var fnRefId = "fnref:" + gid;
292
- var fnId = "fn:" + gid;
325
+ var fnRefId = "fnref:".concat(gid);
326
+ var fnId = "fn:".concat(gid);
293
327
  if (!writer.hasOwnProperty('FOOTNOTES')) {
294
328
  writer.FOOTNOTES = [];
295
329
  }
@@ -303,7 +337,7 @@ var mapToReact = function (makeComponent) {
303
337
  return makeComponent(function (_a) {
304
338
  var children = _a.children, key = _a.key;
305
339
  return React.createElement("sup", { key: key, id: fnRefId, className: "footnote" },
306
- React.createElement("a", { href: "#" + fnId },
340
+ React.createElement("a", { href: "#".concat(fnId) },
307
341
  "[",
308
342
  gid,
309
343
  "]"));
@@ -392,10 +426,11 @@ var mapToReact = function (makeComponent) {
392
426
  console.warn('[jsx] no content in node');
393
427
  return '';
394
428
  }
429
+ var id = getNodeId(node, ctx);
395
430
  return makeComponent(function (_a) {
396
431
  var key = _a.key, children = _a.children;
397
- return React.createElement("table", { key: key },
398
- React.createElement("caption", null, attr.caption),
432
+ return React.createElement("table", { key: key, id: id },
433
+ React.createElement("caption", { className: "caption" }, attr.caption),
399
434
  React.createElement("tbody", null, children));
400
435
  }, node, interator(node.content, __assign({}, ctx)));
401
436
  };
@@ -423,8 +458,35 @@ var mapToReact = function (makeComponent) {
423
458
  if (!(node.content instanceof Array)) {
424
459
  console.error(node);
425
460
  }
426
- return makeComponent('li', node, interator(node.content, __assign({}, ctx)));
461
+ var id = getNodeId(node, ctx);
462
+ return makeComponent('li', node, interator(node.content, __assign({}, ctx)), { id: id });
427
463
  }; },
464
+ // table of content
465
+ ':toc': setFn(function (node, ctx) {
466
+ var tocTitle = node.title;
467
+ return mkComponent(function (_a) {
468
+ var children = _a.children, key = _a.key;
469
+ return React.createElement("div", { className: "toc", key: key },
470
+ tocTitle ? React.createElement("div", { className: "toctitle" }, tocTitle) : '',
471
+ children);
472
+ });
473
+ }),
474
+ ':toc-list': setFn(function (node, ctx) {
475
+ var level = node.level;
476
+ return mkComponent(function (_a) {
477
+ var children = _a.children, key = _a.key;
478
+ return React.createElement("ul", { className: "toc-list listlevel".concat(level), key: key }, children);
479
+ });
480
+ }),
481
+ ':toc-item': subUse({
482
+ // inside head don't wrap into <p>
483
+ ':para': nodeContent,
484
+ }, setFn(function (node, ctx) {
485
+ return mkComponent(function (_a) {
486
+ var children = _a.children, key = _a.key;
487
+ return React.createElement("li", { className: "toc-item", key: key }, children);
488
+ });
489
+ })),
428
490
  };
429
491
  };
430
492
  function podlite(children, _a) {
@@ -449,7 +511,7 @@ function podlite(children, _a) {
449
511
  var writer = new Writer(function (s) {
450
512
  });
451
513
  var res = toAny({ processor: parse })
452
- .use({ '*:*': function (writer, processor) { return function (node, ctx, interator) {
514
+ .use({ '*:*': function () { return function (node, ctx, interator) {
453
515
  // skip named blocks
454
516
  if (isNamedBlock(node.name)) {
455
517
  return null;
@@ -463,7 +525,7 @@ function podlite(children, _a) {
463
525
  }, node, interator(node.content, __assign({}, ctx)));
464
526
  }
465
527
  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));
528
+ return React.createElement('code', { key: ++i_key_i }, "not supported node:".concat(JSON.stringify(node, null, 2)));
467
529
  }; } })
468
530
  .use(rules)
469
531
  .run(ast, writer);
package/package.json CHANGED
@@ -1,37 +1,29 @@
1
1
  {
2
2
  "name": "@podlite/to-jsx",
3
- "version": "0.0.7",
3
+ "version": "0.0.11",
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.7",
15
- "pod6": "0.0.41",
13
+ "@podlite/diagram": "^0.0.11",
14
+ "@podlite/schema": "0.0.8",
15
+ "pod6": "0.0.43",
16
16
  "react-is": "^17.0.2"
17
17
  },
18
18
  "publishConfig": {
19
- "access": "public"
20
- },
21
- "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"
19
+ "access": "public",
20
+ "main": "index.js"
30
21
  },
31
22
  "peerDependencies": {
32
23
  "@podlite/diagram": "*",
24
+ "@podlite/schema": "*",
33
25
  "pod6": "*",
34
- "react": "*"
35
- },
36
- "gitHead": "eaae7ef7bee8588d8dbe3abbff3a3a03e43b849e"
37
- }
26
+ "react": "^16.0.0 || ^17.0.0",
27
+ "react-dom": "^16.0.0 || ^17.0.0"
28
+ }
29
+ }
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,44 @@ 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 = node.src.match(/(mp4|mov)$/)
106
+ ? <video controls key={key}> <source src={node.src} type="video/mp4" /> </video>
107
+ : <img key={key} src={node.src} alt={node.alt}/>
108
+ return linkTo ?<a key={key} href={linkTo}>{Img}</a> : Img
109
+ })
110
+ }),
111
+ 'caption': mkComponent(({ children, key })=><div className="caption" key={key}>{children}</div>)
112
+ },
113
+ setFn(( node, ctx ) => {
114
+ const {level} = node
115
+ const id = getNodeId(node, ctx)
116
+ const conf = makeAttrs(node, ctx)
117
+ if ( conf.exists('link') ) {
118
+ ctx.link = conf.getFirstValue('link')
119
+ }
120
+ return mkComponent(({children, key })=>
121
+ <div className="image_block" key={key} id={id}>{children}</div>)
122
+
123
+ })
124
+ ),
125
+ 'image':nodeContent,
110
126
  ':image': setFn(( node, ctx ) => {
111
127
  return mkComponent(({ children, key })=><img key={key} src={node.src} alt={node.alt}/>)
112
128
  }),
113
129
 
114
130
  'Diagram': () => (node, ctx, interator) => {
131
+ const conf = makeAttrs(node, ctx);
132
+ const caption = conf.exists("caption")
133
+ ? conf.getFirstValue("caption")
134
+ : null;
135
+ const id = getNodeId(node, ctx)
115
136
  return makeComponent(({children, key} )=>{
116
- return <Diagram isError={node.custom} chart={node.content[0].value}/>
137
+ return <Diagram id={id} isError={node.custom} caption={caption} chart={node.content[0].value}/>
117
138
  },node,interator(node.content, { ...ctx}) )
118
139
  },
119
140
  ':text':( writer, processor )=>( node:Text, ctx, interator )=>{
@@ -127,8 +148,9 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
127
148
  ':para' : nodeContent,
128
149
  },
129
150
  setFn(( node, ctx ) => {
130
- const {level} = node
131
- return mkComponent(({level,children, key })=>React.createElement(`h${level}`,{key}, children))
151
+ const {level } = node
152
+ const id = getNodeId(node, ctx)
153
+ return mkComponent(({level,children, key })=>React.createElement(`h${level}`,{key, id}, children))
132
154
  })
133
155
  ),
134
156
 
@@ -320,9 +342,10 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
320
342
  console.warn('[jsx] no content in node')
321
343
  return ''
322
344
  }
345
+ const id = getNodeId(node, ctx)
323
346
  return makeComponent(({key, children})=>{
324
- return <table key={key}>
325
- <caption>{attr.caption}</caption>
347
+ return <table key={key} id={id}>
348
+ <caption className="caption">{attr.caption}</caption>
326
349
  <tbody>{children}</tbody></table>
327
350
  }, node, interator(node.content, { ...ctx}) )
328
351
  }
@@ -353,8 +376,25 @@ const mapToReact = (makeComponent):Partial<RulesStrict> => {
353
376
  if (! (node.content instanceof Array)) {
354
377
  console.error(node)
355
378
  }
356
- return makeComponent('li', node, interator(node.content, { ...ctx}) )
379
+ const id = getNodeId(node,ctx)
380
+ return makeComponent('li', node, interator(node.content, { ...ctx}), {id} )
357
381
  },
382
+ // table of content
383
+ ':toc': setFn(( node:Toc, ctx ) => {
384
+ const tocTitle = node.title
385
+ return mkComponent(({children, key })=><div className="toc" key={key}>{ tocTitle ? <div className="toctitle">{tocTitle}</div> : '' }{children}</div>)
386
+ }),
387
+ ':toc-list': setFn(( node, ctx ) => {
388
+ const { level } = node
389
+ return mkComponent(({children, key })=><ul className={`toc-list listlevel${level}`} key={key}>{children}</ul>)
390
+ }),
391
+ ':toc-item': subUse({
392
+ // inside head don't wrap into <p>
393
+ ':para' : nodeContent,
394
+ },
395
+ setFn(( node, ctx ) => {
396
+ return mkComponent(({children, key })=><li className="toc-item" key={key}>{children}</li>)
397
+ })),
358
398
 
359
399
  }
360
400
  }
@@ -383,7 +423,7 @@ function podlite (children:string, { file,plugins=()=>{}, wrapElement, tree}:{f
383
423
  }) as WriterPostinterator
384
424
  const res =
385
425
  toAny({processor:parse})
386
- .use({'*:*':( writer, processor )=>( node, ctx, interator )=>{
426
+ .use({'*:*':() => ( node, ctx, interator ) => {
387
427
 
388
428
  // skip named blocks
389
429
  if (isNamedBlock(node.name)) {