@podlite/diagram 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 CHANGED
@@ -1,6 +1,16 @@
1
1
  # @podlite/diagram
2
2
 
3
3
  ## Upcoming
4
+ ## 0.0.8
5
+ - add :caption, :id support
6
+ - upgrade mermaid to 8.12.1
7
+ ## 0.0.7
8
+ - update deps
9
+ ## 0.0.6
10
+ - update deps
11
+ ## 0.0.5
12
+ - update mermaid to latest version
13
+ - fix error while diagrams renders
4
14
 
5
15
  ## 0.0.4
6
16
  - update deps
package/README.md CHANGED
@@ -1 +1,114 @@
1
- # @podlite/diagram
1
+ # =Diagram block
2
+ ### `=Diagram` - use diagrams
3
+
4
+ To render beautiful graphs, sequence and Gantt diagrams and flowcharts, one can use the `=Diagram` blocks.
5
+
6
+ ```
7
+ =begin Diagram
8
+ graph LR
9
+ A-->B
10
+ B-->C
11
+ C-->A
12
+ D-->C
13
+ =end Diagram
14
+ ```
15
+ ![sample1](./doc/diagram-sample1.png)
16
+
17
+ [Open this example in pod6.in](https://pod6.in/#p=%3Dbegin+Diagram+%0Agraph+LR%0A++++++++A--%3EB%0A++++++++B--%3EC%0A++++++++C--%3EA%0A++++++++D--%3EC%0A%3Dend+Diagram)
18
+
19
+ #### Flowchart
20
+
21
+ ```
22
+ =Diagram
23
+ graph LR
24
+ A[Square Rect] -- Link text --> B((Circle))
25
+ A --> C(Round Rect)
26
+ B --> D{Rhombus}
27
+ C --> D
28
+ ```
29
+ ![Flowchart](./doc/diagram-sample2.png)
30
+
31
+ [Open this example in pod6.in](https://pod6.in/#p=%3Dhead1+Graph%0A%0A%3DDiagram%0Agraph+LR%0A++A--%3EB%0A++B--%3EC%0A++C--%3EA%0A++D--%3EC%0A%0A%3DDiagram%0Agraph+LR%0A++++A%5BSquare+Rect%5D+--+Link+text+--%3E+B%28%28Circle%29%29%0A++++A+--%3E+C%28Round+Rect%29%0A++++B+--%3E+D%7BRhombus%7D%0A++++C+--%3E+D%0A%0A%0A+%0A%0A)
32
+
33
+ ```
34
+ =Diagram
35
+ graph TD
36
+ A(Coffee machine <br>not working) --> B{Machine has power?}
37
+ B -->|No| H(Plug in and turn on)
38
+ B -->|Yes| C{Out of beans or water?} -->|Yes| G(Refill beans and water)
39
+ C -->|No| D{Filter warning?} -->|Yes| I(Replace or clean filter)
40
+ D -->|No| F(Send for repair)
41
+ ```
42
+ ![Flowchart](./doc/diagram-sample6.png)
43
+
44
+ [Open this example in pod6.in](https://pod6.in/#p=%3Dhead1+Flowchart%0A%3DDiagram%0Agraph+TD%0A+++A%28Coffee+machine+%3Cbr%3Enot+working%29+--%3E+B%7BMachine+has+power%3F%7D%0A+++B+--%3E%7CNo%7C+H%28Plug+in+and+turn+on%29%0A+++B+--%3E%7CYes%7C+C%7BOut+of+beans+or+water%3F%7D+--%3E%7CYes%7C+G%28Refill+beans+and+water%29%0A+++C+--%3E%7CNo%7C+D%7BFilter+warning%3F%7D+--%3E%7CYes%7C+I%28Replace+or+clean+filter%29%0A+++D+--%3E%7CNo%7C+F%28Send+for+repair%29)
45
+ #### UML sequence diagram
46
+
47
+ ```
48
+ =Diagram
49
+ sequenceDiagram
50
+ autonumber
51
+ Student->>Admin: Can I enrol this semester?
52
+ loop enrolmentCheck
53
+ Admin->>Admin: Check previous results
54
+ end
55
+ Note right of Admin: Exam results may <br> be delayed
56
+ Admin-->>Student: Enrolment success
57
+ Admin->>Professor: Assign student to tutor
58
+ Professor-->>Admin: Student is assigned
59
+ ```
60
+ ![Flowchart](./doc/diagram-sample3.png)
61
+
62
+ [Open this example in pod6.in](https://pod6.in/#p=+%3Dhead2+UML+sequence+diagram%0A%0A%3DDiagram%0AsequenceDiagram%0A++++autonumber%0A++++Student-%3E%3EAdmin%3A+Can+I+enrol+this+semester%3F%0A++++loop+enrolmentCheck%0A++++++++Admin-%3E%3EAdmin%3A+Check+previous+results%0A++++end%0A++++Note+right+of+Admin%3A+Exam+results+may+%3Cbr%3E+be+delayed%0A++++Admin--%3E%3EStudent%3A+Enrolment+success%0A++++Admin-%3E%3EProfessor%3A+Assign+student+to+tutor%0A++++Professor--%3E%3EAdmin%3A+Student+is+assigned)
63
+
64
+
65
+ #### UML class diagram
66
+
67
+
68
+ ```
69
+ =Diagram
70
+ classDiagram
71
+ Person <|-- Student
72
+ Person <|-- Professor
73
+ Person : +String name
74
+ Person : +String phoneNumber
75
+ Person : +String emailAddress
76
+ Person: +purchaseParkingPass()
77
+ Address "1" <-- "0..1" Person:lives at
78
+ class Student{
79
+ +int studentNumber
80
+ +int averageMark
81
+ +isEligibleToEnrol()
82
+ +getSeminarsTaken()
83
+ }
84
+ class Professor{
85
+ +int salary
86
+ }
87
+ class Address{
88
+ +String street
89
+ +String city
90
+ +String state
91
+ +int postalCode
92
+ +String country
93
+ -validate()
94
+ +outputAsLabel()
95
+ }
96
+ ```
97
+ ![Flowchart](./doc/diagram-sample4.png)
98
+
99
+ [Open this example in pod6.in](https://pod6.in/#p=%3Dhead1+%0AUML+class+diagram%0A%0A%3DDiagram%0AclassDiagram%0A+++Person+%3C%7C--+Student%0A+++Person+%3C%7C--+Professor%0A+++Person+%3A+%2BString+name%0A+++Person+%3A+%2BString+phoneNumber%0A+++Person+%3A+%2BString+emailAddress%0A+++Person%3A+%2BpurchaseParkingPass%28%29%0A+++Address+%221%22+%3C--+%220..1%22+Person%3Alives+at%0A+++class+Student%7B%0A++++++%2Bint+studentNumber%0A++++++%2Bint+averageMark%0A++++++%2BisEligibleToEnrol%28%29%0A++++++%2BgetSeminarsTaken%28%29%0A++++%7D%0A++++class+Professor%7B%0A++++++%2Bint+salary%0A++++%7D%0A++++class+Address%7B%0A++++++%2BString+street%0A++++++%2BString+city%0A++++++%2BString+state%0A++++++%2Bint+postalCode%0A++++++%2BString+country%0A++++++-validate%28%29%0A++++++%2BoutputAsLabel%28%29++%0A++++%7D)
100
+
101
+
102
+ ## Contributing
103
+
104
+ This is an open source program. Feel free to fork and contribute.
105
+
106
+ In order to keep the match between this documentation and the last release, please contribute and pull requests on the dedicated develop branch.
107
+
108
+ ## AUTHOR
109
+
110
+ Copyright (c) 2021 Alexandr Zahatski
111
+
112
+ ## License
113
+
114
+ Released under a MIT License.
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/lib/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  /// <reference types="react" />
2
2
  import { Plugin } from '@podlite/schema';
3
- declare const Diagram: ({ chart, isError }: {
3
+ declare const Diagram: ({ chart, isError, caption, id }: {
4
4
  chart: string;
5
5
  isError: any;
6
+ caption?: string;
7
+ id?: string;
6
8
  }) => JSX.Element;
7
9
  export declare const plugin: Plugin;
8
10
  export default Diagram;
package/lib/index.js CHANGED
@@ -1,36 +1,20 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
4
  };
16
5
  Object.defineProperty(exports, "__esModule", { value: true });
17
6
  exports.plugin = void 0;
18
- var react_1 = __importDefault(require("react"));
19
- var react_2 = require("react");
20
- var mermaid_1 = __importDefault(require("mermaid"));
21
- var i = 0;
22
- var Diagram = function (_a) {
23
- var chart = _a.chart, isError = _a.isError;
24
- var inputEl = react_2.useRef(null);
25
- var config = {
7
+ const react_1 = __importDefault(require("react"));
8
+ const react_2 = require("react");
9
+ const mermaid_1 = __importDefault(require("mermaid"));
10
+ let i = 0;
11
+ const Diagram = ({ chart, isError, caption, id }) => {
12
+ const inputEl = (0, react_2.useRef)(null);
13
+ const config = {
26
14
  securityLevel: "loose",
27
15
  startOnLoad: false
28
16
  };
29
- if (inputEl) {
30
- mermaid_1.default.initialize(config);
31
- mermaid_1.default.init(inputEl);
32
- }
33
- react_2.useEffect(function () {
17
+ (0, react_2.useEffect)(() => {
34
18
  var insertSvg = function (svgCode) {
35
19
  inputEl.current.innerHTML = svgCode;
36
20
  };
@@ -39,28 +23,32 @@ var Diagram = function (_a) {
39
23
  return;
40
24
  }
41
25
  try {
26
+ mermaid_1.default.initialize(config);
27
+ mermaid_1.default.init(undefined, inputEl.current);
42
28
  mermaid_1.default.render('graph-div' + i++, chart, insertSvg);
43
29
  }
44
30
  catch (e) {
45
- console.log({ e: e });
31
+ console.log({ e });
46
32
  }
47
33
  }, [chart]);
48
- return react_1.default.createElement("div", { className: "mermaid" + (isError ? ' error' : ''), ref: inputEl });
34
+ return react_1.default.createElement("div", { className: "diagram", id: id },
35
+ react_1.default.createElement("div", { className: `mermaid${isError ? ' error' : ''}`, ref: inputEl }),
36
+ caption ? react_1.default.createElement("div", { className: "caption" }, caption) : null);
49
37
  };
50
38
  exports.plugin = {
51
- toAst: function (writer) { return function (node) {
39
+ toAst: (writer) => (node) => {
52
40
  if (typeof node !== "string" && 'type' in node && 'content' in node && node.type === 'block') {
53
- var content = node.content[0];
41
+ const content = node.content[0];
54
42
  if (typeof content !== "string" && 'location' in node && 'value' in content) {
55
43
  // get link and alt text
56
- var data = content.value;
44
+ const data = content.value;
57
45
  try {
58
46
  mermaid_1.default.parse(data);
59
47
  }
60
48
  catch (err) {
61
49
  // calculate line in relation to node
62
- var convert_line_to_absolute = function (line, location) {
63
- var lineoffset = line + location.start.line + 1;
50
+ const convert_line_to_absolute = (line, location) => {
51
+ const lineoffset = line + location.start.line + 1;
64
52
  return { "start": {
65
53
  "offset": 0,
66
54
  "line": lineoffset,
@@ -73,12 +61,12 @@ exports.plugin = {
73
61
  }
74
62
  };
75
63
  };
76
- node.custom = __assign(__assign({}, err), { location: convert_line_to_absolute(err['hash']['line'], node.location) });
64
+ node.custom = { ...err, location: convert_line_to_absolute(err['hash']['line'], node.location) };
77
65
  writer.emit("errors", node.location);
78
66
  }
79
67
  return node;
80
68
  }
81
69
  }
82
- }; },
70
+ },
83
71
  };
84
72
  exports.default = Diagram;
package/package.json CHANGED
@@ -1,33 +1,35 @@
1
1
  {
2
2
  "name": "@podlite/diagram",
3
- "version": "0.0.4",
3
+ "version": "0.0.8",
4
4
  "description": "Diagram",
5
- "main": "index.js",
5
+ "main": "./src/index.tsx",
6
6
  "types": "./lib/index.d.ts",
7
7
  "license": "MIT",
8
8
  "scripts": {
9
9
  "clean": "rm -rf dist lib tsconfig.tsbuildinfo",
10
10
  "build": "tsc",
11
- "test": "jest --passWithNoTests"
11
+ "test": "yarn g:jest --passWithNoTests"
12
12
  },
13
13
  "publishConfig": {
14
14
  "access": "public"
15
15
  },
16
16
  "dependencies": {
17
- "@types/node": "^14.11.2",
18
- "jest-watch-typeahead": "^0.6.1",
19
- "mermaid": "^8.9.2",
20
- "react-is": "^16.12.0",
21
- "ts-jest": "^26.4.1"
17
+ "@podlite/schema": "0.0.7",
18
+ "react-is": "^17.0.2"
22
19
  },
23
20
  "devDependencies": {
24
- "@types/react-dom": "^17.0.3",
25
- "react": "16.12.0",
26
- "react-dom": "^17.0.2",
27
- "typescript": "4.2.3"
21
+ "@podlite/to-jsx": "0.0.8",
22
+ "@types/node": "^16.11.9",
23
+ "@types/react": "^16.x",
24
+ "@types/react-dom": "^16.x",
25
+ "mermaid": "8.12.1",
26
+ "podlite": "0.0.12",
27
+ "react": "^16.12.0",
28
+ "react-dom": "^16.12.0"
28
29
  },
29
30
  "peerDependencies": {
30
- "react": "*"
31
- },
32
- "gitHead": "658ca02b7366520f9516d86249622bc47f60a620"
33
- }
31
+ "mermaid": "*",
32
+ "react": "*",
33
+ "react-is": "*"
34
+ }
35
+ }
package/src/index.tsx CHANGED
@@ -3,16 +3,12 @@ import {useEffect, useRef} from 'react'
3
3
  import {Plugin, Location} from '@podlite/schema'
4
4
  import mermaid from 'mermaid';
5
5
  let i = 0;
6
- const Diagram = ({ chart , isError }: {chart:string, isError:any})=>{
6
+ const Diagram = ({ chart , isError, caption, id }: {chart:string, isError:any, caption?:string, id?:string})=>{
7
7
  const inputEl = useRef(null);
8
8
  const config = {
9
9
  securityLevel: "loose",
10
10
  startOnLoad: false
11
11
  }
12
- if ( inputEl ) {
13
- mermaid.initialize(config)
14
- mermaid.init( inputEl)
15
- }
16
12
 
17
13
  useEffect(()=>{
18
14
  var insertSvg = function(svgCode:any){
@@ -23,13 +19,18 @@ const Diagram = ({ chart , isError }: {chart:string, isError:any})=>{
23
19
  return
24
20
  }
25
21
  try {
22
+ mermaid.initialize(config)
23
+ mermaid.init(undefined, inputEl.current)
26
24
  mermaid.render('graph-div' + i++, chart, insertSvg)
27
25
  } catch (e) {
28
26
  console.log({e});
29
27
  }
30
28
  }, [chart])
31
29
 
32
- return <div className={ `mermaid${ isError ? ' error' : ''}` }ref={inputEl}/>
30
+ return <div className="diagram" id={id}>
31
+ <div className={ `mermaid${ isError ? ' error' : ''}` } ref={inputEl}/>
32
+ { caption ? <div className="caption">{caption}</div> : null }
33
+ </div>
33
34
  }
34
35
 
35
36
  export const plugin:Plugin = {
package/t/plugin.test.tsx CHANGED
@@ -8,6 +8,7 @@ import Diagram, { plugin as DiagramPlugin } from "../src/index";
8
8
  import Podlite from "@podlite/to-jsx";
9
9
  import React from "react";
10
10
  import ReactDOM from "react-dom";
11
+ import makeAttrs from "pod6/built/helpers/config";
11
12
 
12
13
  const parse = (str: string): PodliteDocument => {
13
14
  let podlite = podlite_core({ importPlugins: false }).use({
@@ -31,6 +32,31 @@ const cleanHTML = (html: string) => {
31
32
  return html.replace(/\n+/g, "");
32
33
  };
33
34
 
35
+ const plugins = (makeComponent) => {
36
+ return {
37
+ Diagram: () => (node, ctx, interator) => {
38
+ const conf = makeAttrs(node, ctx);
39
+ const caption = conf.exists("caption")
40
+ ? conf.getFirstValue("caption")
41
+ : null;
42
+ return makeComponent(
43
+ ({ children, key }) => {
44
+ return (
45
+ <Diagram
46
+ isError={node.custom}
47
+ key={key}
48
+ caption={caption}
49
+ chart={node.content[0].value}
50
+ />
51
+ );
52
+ },
53
+ node,
54
+ interator(node.content, { ...ctx })
55
+ );
56
+ },
57
+ };
58
+ };
59
+
34
60
  const root = document.body.appendChild(document.createElement("div"));
35
61
 
36
62
  function render(jsx) {
@@ -73,25 +99,35 @@ B-->D(fa:fa-spinner aaaaa);
73
99
  it("=Diagrams: parse to html", () => {
74
100
  expect(parseToHtml(pod)).toMatchInlineSnapshot(`""`);
75
101
  });
102
+ it("=Diagrams: caption", () => {
103
+ const pod = `
104
+ =begin Diagram :caption('Caption test')
105
+ graph LR
106
+ A-->B
107
+ B-->C
108
+ C-->A
109
+ D-->C
110
+ =end Diagram
111
+ `;
112
+ render(<Podlite plugins={plugins}>{pod}</Podlite>);
113
+ expect(root.innerHTML).toMatchInlineSnapshot(`
114
+ <div class="diagram">
115
+ <div class="mermaid">
116
+ </div>
117
+ <div class="caption">
118
+ Caption test
119
+ </div>
120
+ </div>
121
+ `);
122
+ });
76
123
 
77
124
  it("accepts =Diagram", () => {
78
- const plugins = (makeComponent) => {
79
- return {
80
- Diagram: () => (node, ctx, interator) => {
81
- return makeComponent(
82
- ({ children, key }) => {
83
- return <Diagram isError= {node.custom} key={key} chart={node.content[0].value} />;
84
- },
85
- node,
86
- interator(node.content, { ...ctx })
87
- );
88
- },
89
- };
90
- };
91
125
  render(<Podlite plugins={plugins}>{pod}</Podlite>);
92
126
  expect(root.innerHTML).toMatchInlineSnapshot(`
93
127
  <div>
94
- <div class="mermaid">
128
+ <div class="diagram">
129
+ <div class="mermaid">
130
+ </div>
95
131
  </div>
96
132
  </div>
97
133
  `);
package/tsconfig.json CHANGED
@@ -2,9 +2,10 @@
2
2
  "extends": "../core/tsconfig.json",
3
3
  "include": ["src/**/*"],
4
4
  "compilerOptions": {
5
+ "jsx": "react",
5
6
  "outDir": "./lib",
6
7
  "rootDir": "./src",
7
- "jsx": "react",
8
+ "baseUrl": "."
8
9
  },
9
10
  "references": [
10
11
  {