remark-mind-elixir 0.2.0 → 0.3.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remark-mind-elixir",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "remark",
@@ -24,15 +24,17 @@
24
24
  "types": "./src/index.d.ts"
25
25
  },
26
26
  "./client": {
27
- "import": "./src/index.js",
28
- "types": "./src/index.d.ts"
27
+ "import": "./src/client.js"
28
+ },
29
+ "./style": {
30
+ "import": "./src/style.css"
29
31
  }
30
32
  },
31
33
  "license": "MIT",
32
34
  "author": "zakaria elalaoui",
33
35
  "type": "module",
34
36
  "dependencies": {
35
- "@zikojs/mind-elixir": "^0.6.1",
37
+ "@zikojs/mind-elixir": "^0.6.2",
36
38
  "hast-util-from-html-isomorphic": "^2.0.0",
37
39
  "mind-elixir": "6.0.0-next.4",
38
40
  "unist-util-visit": "^5.1.0",
package/src/client.js ADDED
@@ -0,0 +1,17 @@
1
+ import { MindMap } from '@zikojs/mind-elixir/no-css'
2
+
3
+ document.addEventListener('DOMContentLoaded', () => {
4
+ document.querySelectorAll('[data-elixir-mind]').forEach((element) => {
5
+ const raw = element.dataset.xmindBody
6
+ // const config = element.dataset?.xmindConfig
7
+
8
+ if (!raw) return
9
+
10
+ const nodeData = JSON.parse(raw)
11
+ // const nodeConfig = JSON.parse(config)
12
+
13
+ const map = MindMap({}, nodeData)
14
+
15
+ map.mount(element)
16
+ })
17
+ })
package/src/index.js CHANGED
@@ -1,36 +1,46 @@
1
1
  import { visit } from 'unist-util-visit'
2
- import { parse } from 'yaml'
2
+ import { parse, parseAllDocuments } from "yaml";
3
3
 
4
4
  import { yaml2MindElixirData } from '@zikojs/mind-elixir/utils'
5
5
 
6
6
  const remarkElixirMind = ({
7
7
  useCdn = true,
8
- } = {}) => {
8
+ } = {}) => () => {
9
9
  return function transformer(tree) {
10
10
  let hasElixirMind = false
11
11
 
12
12
  visit(tree, 'code', (node, index, parent) => {
13
13
  if (node.lang !== 'elixir-mind') return
14
14
 
15
- hasElixirMind = true
15
+ hasElixirMind = true;
16
+ let config = {}, body = null;
16
17
 
17
- const body = yaml2MindElixirData(node.value.trim())
18
+ const documents = splitDocuments(node.value.trim());
19
+ if(documents.length === 1) body = yaml2MindElixirData(documents[0])
20
+ else {
21
+ config = parse(documents[0])
22
+ body = yaml2MindElixirData(documents[1])
23
+ }
18
24
 
19
25
  // parent creates circular references, so serialize a clean copy
20
- const serialized = JSON.stringify(body, (key, value) => {
26
+ const serialized_body = JSON.stringify(body, (key, value) => {
21
27
  if (key === 'parent') return undefined
22
28
  return value
23
29
  })
24
30
 
31
+ const serialized_config = JSON.stringify(config)
32
+
25
33
  // Escape the JSON for use inside an HTML attribute
26
- const encoded = escapeHtmlAttribute(serialized)
34
+ const encoded_body = escapeHtmlAttribute(serialized_body)
35
+ const encoded_config = escapeHtmlAttribute(serialized_config)
27
36
 
28
37
  parent.children[index] = {
29
38
  type: 'html',
30
39
  value: `
31
40
  <div
32
41
  data-elixir-mind
33
- data-xmind-body="${encoded}"
42
+ data-xmind-body="${encoded_body}"
43
+ data-xmind-config="${encoded_config}"
34
44
  ></div>
35
45
  `
36
46
  }
@@ -45,23 +55,20 @@ const remarkElixirMind = ({
45
55
  @import url('https://esm.sh/mind-elixir/style')
46
56
  </style>
47
57
 
48
- <script type="module">
58
+ <script type="module" data-engine="zikojs, remark, mind-elixir">
49
59
  import { MindMap } from 'https://esm.sh/@zikojs/mind-elixir@latest/src/mind/main.js'
50
-
51
- document.querySelectorAll('[data-elixir-mind]').forEach((element) => {
52
- const raw = element.dataset.xmindBody
53
-
54
- if (!raw) return
55
-
56
- const nodeData = JSON.parse(raw)
57
-
58
- const map = MindMap({
59
- height: '400px',
60
- direction: 2
61
- }, nodeData)
62
-
63
- map.mount(element)
60
+ document.addEventListener('DOMContentLoaded', () => {
61
+ document.querySelectorAll('[data-elixir-mind]').forEach((element) => {
62
+ const raw = element.dataset.xmindBody
63
+ // const config = element.dataset?.xmindConfig
64
+ if (!raw) return
65
+ const nodeData = JSON.parse(raw)
66
+ // const nodeConfig = JSON.parse(config)
67
+ const map = MindMap({}, nodeData)
68
+ map.mount(element)
69
+ })
64
70
  })
71
+
65
72
  </script>
66
73
  `
67
74
  })
@@ -76,4 +83,61 @@ function escapeHtmlAttribute(value) {
76
83
  .replace(/>/g, '&gt;')
77
84
  }
78
85
 
79
- export default remarkElixirMind
86
+ export default remarkElixirMind
87
+
88
+ const splitDocuments = (text) => {
89
+ const documents = []
90
+ let current = ''
91
+
92
+ let quote = null
93
+ let escaped = false
94
+
95
+ const lines = text.trim().split('\n')
96
+
97
+ for (const line of lines) {
98
+ const isSeparator = line.match(/^---[ \t]*$/)
99
+
100
+ if (isSeparator && !quote) {
101
+ if (current.trim()) {
102
+ documents.push(current.trim())
103
+ }
104
+ current = ''
105
+ continue
106
+ }
107
+
108
+ current += (current ? '\n' : '') + line
109
+
110
+ // Track JS string state
111
+ for (let i = 0; i < line.length; i++) {
112
+ const char = line[i]
113
+
114
+ if (escaped) {
115
+ escaped = false
116
+ continue
117
+ }
118
+
119
+ if (char === '\\') {
120
+ escaped = true
121
+ continue
122
+ }
123
+
124
+ if (quote) {
125
+ if (char === quote) {
126
+ quote = null
127
+ }
128
+ } else if (
129
+ char === "'" ||
130
+ char === '"' ||
131
+ char === '`'
132
+ ) {
133
+ quote = char
134
+ }
135
+ }
136
+ }
137
+
138
+ if (current.trim()) {
139
+ documents.push(current.trim())
140
+ }
141
+
142
+ return documents
143
+ }
package/src/style.css ADDED
@@ -0,0 +1 @@
1
+ @import 'mind-elixir/style.css';