mdzjs 0.0.0 → 0.0.2
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/README.md +72 -58
- package/__src__/index.js +2 -0
- package/__src__/test.js +21 -0
- package/__v2__/_test.js +77 -0
- package/__v2__/clean.js +67 -0
- package/__v2__/hast.js +70 -0
- package/__v2__/html2js.js +59 -0
- package/__v2__/index.js +2 -0
- package/__v2__/pre-parse/clean-md.js +93 -0
- package/__v2__/readme.md +17 -0
- package/__v2__/test.js +19 -0
- package/__v2__/transformers/vite/index.js +16 -0
- package/__v2__/transpiler/get-component-type.js +27 -0
- package/__v2__/transpiler/import-plugin.js +19 -0
- package/__v2__/transpiler/index.js +168 -0
- package/__v2__/transpiler/jsx2js.js +30 -0
- package/__v2__/transpiler/process-text.js +48 -0
- package/__v2__/utils/is-object.js +11 -0
- package/__v2__/utils/split-between.js +47 -0
- package/package.json +17 -7
- package/preview/Components/Dom.js +6 -0
- package/preview/Components/Hi.js +2 -0
- package/preview/Components/Scene.js +10 -0
- package/preview/index.html +10 -1
- package/preview/main.js +7 -10
- package/preview/package-lock.json +837 -7
- package/preview/package.json +7 -1
- package/preview/test.mdz +29 -13
- package/preview/test.mdz.__ +24 -0
- package/preview/vite.config.js +3 -2
- package/rollup.config.js +1 -1
- package/src/bundlers/vite.js +15 -0
- package/src/example.md +3 -0
- package/src/index.js +1 -2
- package/src/test.js +45 -17
- package/src/transpiler/index.js +3 -0
- package/src/transpiler/parser.js +14 -0
- package/src/transpiler/process.js +172 -0
- package/src/transpiler/transpile.js +22 -0
- package/src/utils/component-type.js +5 -0
- package/src/utils/hyperscript.js +4 -0
- package/src/utils/index.js +4 -0
- package/src/utils/parse-yml.js +19 -0
- package/src/utils/process-attributes.js +13 -0
- /package/{src → __src__}/converter/index.js +0 -0
- /package/{src → __src__}/parser/get-component-type.js +0 -0
- /package/{src → __src__}/parser/index.js +0 -0
- /package/{src → __src__}/parser/jsx2js.js +0 -0
- /package/{src → __src__}/parser/parser-markdown.js +0 -0
- /package/{src → __src__}/parser/smart-join.js +0 -0
- /package/{src → __src__}/vite/index.js +0 -0
- /package/preview/{InteractiveBlock.js → Components/InteractiveBlock.js} +0 -0
- /package/preview/{__main2.js → __main2.js.txt} +0 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { unified } from 'unified';
|
|
2
|
+
import remarkParse from 'remark-parse';
|
|
3
|
+
import { getComponentType } from './get-component-type.js';
|
|
4
|
+
import { jsx2js } from './jsx2js.js';
|
|
5
|
+
import { processText } from "./process-text.js"
|
|
6
|
+
import { parse as parseYaml } from 'yaml';
|
|
7
|
+
import remarkFrontmatter from 'remark-frontmatter';
|
|
8
|
+
import remarkGFM from "remark-gfm"
|
|
9
|
+
import { cleanMD } from '../pre-parse/clean-md.js';
|
|
10
|
+
|
|
11
|
+
const parseMDZ = (markdown) => unified().use(remarkParse).use(remarkGFM).use(remarkFrontmatter, ['yaml']).parse(markdown);
|
|
12
|
+
|
|
13
|
+
const processMDZ = (markdown) => {
|
|
14
|
+
const importRegex = /^\s*import\s+[^;]+;$/gm
|
|
15
|
+
let imports = ['import {h, Flex, HTMLWrapper} from "ziko"'];
|
|
16
|
+
let frontmatter = {};
|
|
17
|
+
let cleanedMarkdown = markdown;
|
|
18
|
+
|
|
19
|
+
cleanedMarkdown = cleanedMarkdown.replace(importRegex, (match) => {
|
|
20
|
+
imports.push(match.trim());
|
|
21
|
+
return '';
|
|
22
|
+
});
|
|
23
|
+
return { imports : imports.join("\n"), frontmatter, cleanedMarkdown };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const transformMDZ = (markdownAST) => {
|
|
27
|
+
const transformNode = (node) => {
|
|
28
|
+
switch(node.type){
|
|
29
|
+
case 'text' : {
|
|
30
|
+
const text = node.value;
|
|
31
|
+
return processText(text)
|
|
32
|
+
}
|
|
33
|
+
case 'paragraph' : {
|
|
34
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
35
|
+
return `h('p', {}, ${childNodes})`
|
|
36
|
+
}
|
|
37
|
+
case 'heading' : {
|
|
38
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
39
|
+
return `h('h${node.depth}', {}, ${childNodes})`;
|
|
40
|
+
}
|
|
41
|
+
case 'strong': {
|
|
42
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
43
|
+
return `h('strong', {}, ${childNodes})`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
case 'emphasis': {
|
|
47
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
48
|
+
return `h('em', {}, ${childNodes})`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
case 'link': {
|
|
52
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
53
|
+
return `h('a', { href: "${node.url}" }, ${childNodes})`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
case 'image': {
|
|
57
|
+
return `h('img', { src: "${node.url}", alt: "${node.alt || ''}" })`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
case 'list': {
|
|
61
|
+
const listTag = node.ordered ? 'ol' : 'ul';
|
|
62
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
63
|
+
return `h('${listTag}', {}, ${childNodes})`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
case 'listItem': {
|
|
67
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
68
|
+
return `h('li', {}, ${childNodes})`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
case 'code': {
|
|
72
|
+
const language = node.lang ? `, { 'data-lang': '${node.lang}' }` : '';
|
|
73
|
+
return `h('pre', {}, h('code'${language}, {}, ${JSON.stringify(node.value)}))`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
case 'blockquote': {
|
|
77
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
78
|
+
return `h('blockquote', {}, ${childNodes})`;
|
|
79
|
+
}
|
|
80
|
+
case 'thematicBreak': {
|
|
81
|
+
return `h('hr', {})`;
|
|
82
|
+
}
|
|
83
|
+
case 'table': {
|
|
84
|
+
const headerRows = node.children[0].children.map(transformNode).join(', ');
|
|
85
|
+
const bodyRows = node.children.slice(1).map(transformNode).join(', ');
|
|
86
|
+
const thead = `h('thead', {}, h('tr', {}, ${headerRows})`;
|
|
87
|
+
const tbody = `h('tbody', {}, ${bodyRows})`
|
|
88
|
+
return `h('table', {}, ${thead}), ${tbody}).style({border : "1px solid darkblue", borderCollapse: "collapse"})`;
|
|
89
|
+
}
|
|
90
|
+
case 'tableRow': {
|
|
91
|
+
const cells = node.children.map(transformNode).join(', ');
|
|
92
|
+
return `h('tr', {}, ${cells}).style({border : "1px solid darkblue", borderCollapse: "collapse"})`;
|
|
93
|
+
}
|
|
94
|
+
case 'tableCell': {
|
|
95
|
+
const childNodes = node.children.map(transformNode).join(', ');
|
|
96
|
+
return `h('td', {}, ${childNodes}).style({border : "1px solid darkblue", borderCollapse: "collapse", padding : "5px"})`;
|
|
97
|
+
}
|
|
98
|
+
case 'html' : {
|
|
99
|
+
const component = node.value.trim();
|
|
100
|
+
const type = getComponentType(component);
|
|
101
|
+
switch (type) {
|
|
102
|
+
case 'jsx':
|
|
103
|
+
return ` ${jsx2js(component.replaceAll("\n",""))}`;
|
|
104
|
+
case 'html':{
|
|
105
|
+
return ` HTMLWrapper("${component}")`
|
|
106
|
+
|
|
107
|
+
// To be replaced by HTML Parser
|
|
108
|
+
}
|
|
109
|
+
case 'script' : return {
|
|
110
|
+
type : "script",
|
|
111
|
+
value : `${component.replace(/<script[^>]*>/g, '').replace(/<\/script>/g, '').trim()}`
|
|
112
|
+
};
|
|
113
|
+
default:
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
}
|
|
120
|
+
return 'null';
|
|
121
|
+
};
|
|
122
|
+
let fm = [];
|
|
123
|
+
let mdBody = [];
|
|
124
|
+
|
|
125
|
+
markdownAST.children.forEach((node) => {
|
|
126
|
+
if (node.type === 'yaml') {
|
|
127
|
+
fm.push(node);
|
|
128
|
+
} else {
|
|
129
|
+
mdBody.push(node);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
fm : fm[0] ? parseYaml(fm[0].value) : {},
|
|
134
|
+
body : mdBody.map(transformNode).map(n=>n instanceof Object? n.value: `__items__.push(${n})`).join("\n")
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const transpileMDZ= markdown =>{
|
|
139
|
+
const { imports, cleanedMarkdown } = processMDZ(markdown);
|
|
140
|
+
const ast = parseMDZ(cleanedMarkdown);
|
|
141
|
+
const {body,fm} = transformMDZ(ast);
|
|
142
|
+
const {__Props__, ...Attr} = fm
|
|
143
|
+
const defaultProps = __Props__
|
|
144
|
+
? Object.entries(__Props__)
|
|
145
|
+
.map(([key, value]) => `${key} = ${JSON.stringify(value)}`)
|
|
146
|
+
.join(', ')
|
|
147
|
+
: '';
|
|
148
|
+
|
|
149
|
+
console.log({defaultProps : fm})
|
|
150
|
+
let ui = `const __items__ = [];
|
|
151
|
+
${body}
|
|
152
|
+
// console.log({__items__})
|
|
153
|
+
const UI = (${defaultProps ? `{${defaultProps}}={}`: ""}) => Flex(...__items__).vertical(0, 0);
|
|
154
|
+
export default UI
|
|
155
|
+
`
|
|
156
|
+
const AttrFounded = Object.keys(Attr).length > 0
|
|
157
|
+
let attributs = AttrFounded ? `const {${Object.keys(Attr).join(",")}} = ${JSON.stringify(Attr,"",2)}` : null
|
|
158
|
+
let exports = AttrFounded ? `export {${Object.keys(Attr).join(", ")}}` : null
|
|
159
|
+
const Output = [
|
|
160
|
+
imports,
|
|
161
|
+
attributs,
|
|
162
|
+
ui,
|
|
163
|
+
exports,
|
|
164
|
+
].filter(Boolean).join('\n');
|
|
165
|
+
return Output
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
export {transpileMDZ}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
function jsx2js(component) {
|
|
2
|
+
const regex = /<(\w+)([^>]*)\/?>/;
|
|
3
|
+
const match = component.match(regex);
|
|
4
|
+
if (!match) {
|
|
5
|
+
throw new Error("Invalid component format");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const componentName = match[1];
|
|
9
|
+
const attributesString = match[2];
|
|
10
|
+
const attributes = {};
|
|
11
|
+
|
|
12
|
+
const attrRegex = /(\w+)\s*=\s*("([^"]*)"|{([^}]*)})/g;
|
|
13
|
+
let attrMatch;
|
|
14
|
+
|
|
15
|
+
while ((attrMatch = attrRegex.exec(attributesString)) !== null) {
|
|
16
|
+
const key = attrMatch[1];
|
|
17
|
+
const value = attrMatch[3] || attrMatch[4]; // Either the string value or the expression inside {}
|
|
18
|
+
attributes[key] = value === attrMatch[3]?JSON.stringify(value):new Function(`return ${value}`)();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const attributesArray = Object.entries(attributes).map(([key, value]) => {
|
|
22
|
+
return `${key}:${value}`;
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const attributesStringFormatted = attributesArray.join(',');
|
|
26
|
+
|
|
27
|
+
return `${componentName}({${attributesStringFormatted}})`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { jsx2js };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
function processText(str) {
|
|
2
|
+
const result = [];
|
|
3
|
+
let currentExpr = '';
|
|
4
|
+
let currentText = '';
|
|
5
|
+
let insideBraces = false;
|
|
6
|
+
let braceCount = 0;
|
|
7
|
+
|
|
8
|
+
for (let i = 0; i < str.length; i++) {
|
|
9
|
+
const char = str[i];
|
|
10
|
+
|
|
11
|
+
if (char === '{') {
|
|
12
|
+
if (braceCount === 0 && currentText) {
|
|
13
|
+
result.push(currentText);
|
|
14
|
+
currentText = '';
|
|
15
|
+
}
|
|
16
|
+
braceCount++;
|
|
17
|
+
currentExpr += char;
|
|
18
|
+
} else if (char === '}') {
|
|
19
|
+
braceCount--;
|
|
20
|
+
currentExpr += char;
|
|
21
|
+
if (braceCount === 0) {
|
|
22
|
+
result.push({type:"expression", value : currentExpr.slice(1,-1)});
|
|
23
|
+
// try {
|
|
24
|
+
// // console.log({currentExpr:currentExpr.slice(1,-1)})
|
|
25
|
+
// // const evaluatedExpr = new Function(`return ${currentExpr.trim().slice(1,-1)}`)()
|
|
26
|
+
// result.push({type:"expression", value : currentExpr.slice(1,-1)});
|
|
27
|
+
// } catch (e) {
|
|
28
|
+
// //result.push({type:"string", value : currentExpr.trim});
|
|
29
|
+
// result.push(currentExpr.trim());
|
|
30
|
+
// }
|
|
31
|
+
currentExpr = '';
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
if (braceCount === 0) {
|
|
35
|
+
currentText += char;
|
|
36
|
+
} else {
|
|
37
|
+
currentExpr += char;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (currentText) {
|
|
42
|
+
result.push(currentText);
|
|
43
|
+
}
|
|
44
|
+
return result.map(n=>typeof(n)==="string"?`"${n}"`:n.value);
|
|
45
|
+
}
|
|
46
|
+
export{
|
|
47
|
+
processText
|
|
48
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function isObject(input) {
|
|
2
|
+
try {
|
|
3
|
+
const parsed = new Function(`return (${input})`)();
|
|
4
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed);
|
|
5
|
+
} catch {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// console.log(isObject("{a:1}")); // true
|
|
11
|
+
// console.log(isObject("{1+1}")) // false
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export function splitBetween(input, startToken, endToken) {
|
|
2
|
+
const pattern = new RegExp(`${startToken}(.*?)${endToken}`, "g"); // Match the tokens and capture the content inside
|
|
3
|
+
const result = [];
|
|
4
|
+
let lastIndex = 0;
|
|
5
|
+
|
|
6
|
+
input.replace(pattern, (match, content, offset) => {
|
|
7
|
+
if (offset > lastIndex) {
|
|
8
|
+
result.push({
|
|
9
|
+
type: "string",
|
|
10
|
+
value: input.slice(lastIndex, offset),
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
result.push({
|
|
14
|
+
type: "expression",
|
|
15
|
+
value: content,
|
|
16
|
+
});
|
|
17
|
+
lastIndex = offset + match.length;
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
if (lastIndex < input.length) {
|
|
21
|
+
result.push({
|
|
22
|
+
type: "string",
|
|
23
|
+
value: input.slice(lastIndex),
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// const input = "Text before {token content} text between {another token} end text";
|
|
31
|
+
// console.log(splitBetween(input, "{", "}"));
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// export function splitBetween(input, startToken , endToken) {
|
|
35
|
+
// const pattern = new RegExp(`${startToken}|${endToken}`, "g")
|
|
36
|
+
// const arr=input.split(pattern);
|
|
37
|
+
// return arr
|
|
38
|
+
// }
|
|
39
|
+
|
|
40
|
+
// // const input = `
|
|
41
|
+
// // Hi "EXP_START{This is an expression}EXP_END" How are you , {}
|
|
42
|
+
// // `;
|
|
43
|
+
|
|
44
|
+
// // const startToken = "\"EXP_START";
|
|
45
|
+
// // const endToken = "EXP_END\"";
|
|
46
|
+
// // console.log(splitBetween(input, startToken, endToken));
|
|
47
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdzjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -14,19 +14,29 @@
|
|
|
14
14
|
"import": "./src/index.js"
|
|
15
15
|
},
|
|
16
16
|
"./vite": {
|
|
17
|
-
"import": "./src/vite
|
|
17
|
+
"import": "./src/bundlers/vite.js"
|
|
18
18
|
},
|
|
19
|
-
"author": "",
|
|
19
|
+
"author": "zakaria elalaoui",
|
|
20
20
|
"keywords": [
|
|
21
21
|
"zikojs",
|
|
22
|
-
"zikojs-addons"
|
|
22
|
+
"zikojs-addons",
|
|
23
|
+
"markdown",
|
|
24
|
+
"remark",
|
|
25
|
+
"mdx",
|
|
26
|
+
"preprocessor"
|
|
23
27
|
],
|
|
24
28
|
"license": "MIT",
|
|
25
29
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
30
|
+
"acorn": "^8.14.0",
|
|
31
|
+
"htmlparser2": "^9.1.0",
|
|
32
|
+
"remark-frontmatter": "^5.0.0",
|
|
33
|
+
"remark-gfm": "^4.0.0",
|
|
34
|
+
"remark-mdx": "^3.1.0",
|
|
35
|
+
"remark-parse": "^11.0.0",
|
|
28
36
|
"rollup": "^4.24.0",
|
|
29
|
-
"
|
|
37
|
+
"unified": "^11.0.5",
|
|
38
|
+
"yaml": "^2.6.1",
|
|
39
|
+
"ziko": "^0.0.27"
|
|
30
40
|
},
|
|
31
41
|
"devDependencies": {
|
|
32
42
|
"@rollup/plugin-commonjs": "^28.0.0",
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {SceneGl, cube3} from "ziko-gl"
|
|
2
|
+
import { LottiePlayer } from "ziko-lottie"
|
|
3
|
+
import {Flex} from "ziko"
|
|
4
|
+
export default ({data})=>{
|
|
5
|
+
return LottiePlayer().size("200px","200px").useControls()
|
|
6
|
+
let element = SceneGl("200px","200px").add(
|
|
7
|
+
cube3(1)
|
|
8
|
+
)
|
|
9
|
+
return Flex().append(element)
|
|
10
|
+
}
|
package/preview/index.html
CHANGED
|
@@ -10,7 +10,16 @@
|
|
|
10
10
|
rel="stylesheet"
|
|
11
11
|
/>
|
|
12
12
|
<link href="https://fonts.cdnfonts.com/css/cheeronsta" rel="stylesheet">
|
|
13
|
-
|
|
13
|
+
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css"> -->
|
|
14
|
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/monokai.css">
|
|
15
|
+
|
|
16
|
+
<style>
|
|
17
|
+
pre{
|
|
18
|
+
background : #282b2e;
|
|
19
|
+
padding : 10px;
|
|
20
|
+
color : white
|
|
21
|
+
}
|
|
22
|
+
</style>
|
|
14
23
|
<!-- <style>
|
|
15
24
|
body{
|
|
16
25
|
font-family: "Gloria Hallelujah", cursive;
|
package/preview/main.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { useTitle } from "ziko"
|
|
2
|
-
import UI,{
|
|
3
|
-
const {title, background} = attributes
|
|
2
|
+
import UI,{title} from "./test.mdz"
|
|
4
3
|
title && useTitle(title)
|
|
5
|
-
UI()
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
background : background ?? "orange"
|
|
12
|
-
}).vertical(-1, "space-around")
|
|
4
|
+
UI({name : "from MDZjs"})
|
|
5
|
+
.vertical(-1, 0)
|
|
6
|
+
.style({
|
|
7
|
+
border :"1px darkblue solid",
|
|
8
|
+
padding : "15px",
|
|
9
|
+
})
|