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.
Files changed (53) hide show
  1. package/README.md +72 -58
  2. package/__src__/index.js +2 -0
  3. package/__src__/test.js +21 -0
  4. package/__v2__/_test.js +77 -0
  5. package/__v2__/clean.js +67 -0
  6. package/__v2__/hast.js +70 -0
  7. package/__v2__/html2js.js +59 -0
  8. package/__v2__/index.js +2 -0
  9. package/__v2__/pre-parse/clean-md.js +93 -0
  10. package/__v2__/readme.md +17 -0
  11. package/__v2__/test.js +19 -0
  12. package/__v2__/transformers/vite/index.js +16 -0
  13. package/__v2__/transpiler/get-component-type.js +27 -0
  14. package/__v2__/transpiler/import-plugin.js +19 -0
  15. package/__v2__/transpiler/index.js +168 -0
  16. package/__v2__/transpiler/jsx2js.js +30 -0
  17. package/__v2__/transpiler/process-text.js +48 -0
  18. package/__v2__/utils/is-object.js +11 -0
  19. package/__v2__/utils/split-between.js +47 -0
  20. package/package.json +17 -7
  21. package/preview/Components/Dom.js +6 -0
  22. package/preview/Components/Hi.js +2 -0
  23. package/preview/Components/Scene.js +10 -0
  24. package/preview/index.html +10 -1
  25. package/preview/main.js +7 -10
  26. package/preview/package-lock.json +837 -7
  27. package/preview/package.json +7 -1
  28. package/preview/test.mdz +29 -13
  29. package/preview/test.mdz.__ +24 -0
  30. package/preview/vite.config.js +3 -2
  31. package/rollup.config.js +1 -1
  32. package/src/bundlers/vite.js +15 -0
  33. package/src/example.md +3 -0
  34. package/src/index.js +1 -2
  35. package/src/test.js +45 -17
  36. package/src/transpiler/index.js +3 -0
  37. package/src/transpiler/parser.js +14 -0
  38. package/src/transpiler/process.js +172 -0
  39. package/src/transpiler/transpile.js +22 -0
  40. package/src/utils/component-type.js +5 -0
  41. package/src/utils/hyperscript.js +4 -0
  42. package/src/utils/index.js +4 -0
  43. package/src/utils/parse-yml.js +19 -0
  44. package/src/utils/process-attributes.js +13 -0
  45. /package/{src → __src__}/converter/index.js +0 -0
  46. /package/{src → __src__}/parser/get-component-type.js +0 -0
  47. /package/{src → __src__}/parser/index.js +0 -0
  48. /package/{src → __src__}/parser/jsx2js.js +0 -0
  49. /package/{src → __src__}/parser/parser-markdown.js +0 -0
  50. /package/{src → __src__}/parser/smart-join.js +0 -0
  51. /package/{src → __src__}/vite/index.js +0 -0
  52. /package/preview/{InteractiveBlock.js → Components/InteractiveBlock.js} +0 -0
  53. /package/preview/{__main2.js → __main2.js.txt} +0 -0
package/README.md CHANGED
@@ -1,16 +1,19 @@
1
- # Markdown for [Zikojs](https://github.com/zakarialaoui10/ziko.js)
1
+ > [!NOTE]
2
+ > This project is part of the [ZikoJS](https://github.com/zakarialaoui10/ziko.js) ecosystem.
3
+
4
+ # MDZjs
5
+ A Markdown preprocessor for Zikojs. Markdown in Zikojs.
6
+ It combines the simplicity of Markdown syntax with the power and flexibility of ***Javascript***
2
7
 
3
8
  ## Install
4
9
  ```bash
5
10
  npm i mdzjs
6
11
  ```
7
-
8
- ## Usage
9
-
10
- ### Config
12
+
13
+ ## Config
11
14
  ```js
12
15
  import {defineConfig} from "vite"
13
- import {MDZ} from "mdzjs"
16
+ import MDZ from "mdzjs/vite"
14
17
  export default defineConfig({
15
18
  plugins : [
16
19
  MDZ()
@@ -18,67 +21,78 @@ export default defineConfig({
18
21
  })
19
22
  ```
20
23
 
21
- ### Example
24
+ ## Usage
25
+
26
+ *Article.mdz :*
22
27
 
23
- ```md
28
+ ```js
24
29
  ---
25
- modules :
26
- - import InteractiveBlock from "./InteractiveBlock.js"
27
- title : MDZ
28
- background : tomato
30
+ title : MDZ
31
+ name : world
32
+ __props__ :
33
+ background : tomato
34
+ data : []
29
35
  ---
30
- **MDZ** (Markdown for ***zikojs***) is a format that allows you to append Zikojs Elements directly within Markdown.
31
-
32
- It combines the simplicity of Markdown syntax with the power and flexibility of ***Javascript***
36
+ import data from "./data.js";
37
+ import InteractiveComponent from "./InteractiveComponent.js";
33
38
 
34
- Here’s an example of an interactive block rendered within this MDZ file:
39
+ # Hello {name}
35
40
 
36
- <InteractiveBlock data="Hello from MDZ" color="darkblue"/>
41
+ <InteractiveComponent data={data} background={tomato}/>
37
42
 
38
- ### Features of MDZ:
39
- 1- **Simple Integration :** Write Markdown as usual, and inject ZikoJS elements wherever needed.
40
- 2- **Frontmatter Support :** In this example, the title of the document is set dynamically through the frontmatter.
41
-
42
- 3- **Extensible :** Create custom components like InteractiveBlock and use them in any Markdown file.
43
43
  ```
44
44
 
45
45
  ```js
46
- // InteractiveBlock.js
47
- import {Flex, input, text} from "ziko"
48
- export default ({data, color})=>{
49
- let txt = text(data).style({color})
50
- let inp = input(data).style({
51
- padding : "5px",
52
- background : "transparent",
53
- outline :"none",
54
- boxShadow :"1px 1px 1px white",
55
- fontSize : "inherit"
56
- })
57
- inp.onInput(e=>txt.setValue(e.value))
58
- return Flex(
59
- inp,
60
- txt
61
- ).vertical(0, "space-around").size("60%").style({
62
- border : "2px darkblue solid",
63
- padding : "10px",
64
- minHeight : "100px",
65
- margin : "auto"
66
- })
67
- }
46
+ // main.js
47
+ import Article,{title} from "./Article.mdz"
68
48
  ```
69
49
 
50
+ ## Features
51
+ - **Simple Integration :** Write Markdown as usual, and inject Zikojs elements wherever needed.
52
+ - **Extensible :** Create custom interactive components using Zikojs and use them in any Markdown file.
53
+ - **Reusable :** MDZ exports a default functional component, allowing you to call it multiple times with different data, enabling dynamic and versatile use.
54
+ - **Frontmatter Support :** Use `YAML` syntax in to include metadata like titles, descriptions, or configurations in your Markdown files, and define props to pass data dynamically to Zikojs components.
55
+
56
+ - **Markdown :** Use standard Markdown syntax for writing content.
57
+ - **JSX Syntax :** Write components in JSX within Markdown, enabling easy integration of Zikojs elements with JavaScript and HTML..
58
+ - **Props :** Pass data to components through props, enabling dynamic rendering and customization of content within your Markdown files.
59
+ - **ESM :** Supports ECMAScript Modules (ESM), allowing you to import and export modules
60
+ - **Expressions :** MDZjs lets you use JS expressions inside curly braces, like Hello {name}.
61
+ These expressions can be full JS programs, as long as they evaluate to something renderable. For example, you can use an IIFE like this:
70
62
  ```js
71
- // main.js
72
- import { useTitle } from "ziko"
73
- import UI,{attributes} from "./test.mdz"
74
- const {title, background} = attributes
75
- title && useTitle(title)
76
- UI().style({
77
- border : "2px darkblue solid",
78
- width : "70%",
79
- margin : "auto",
80
- padding : "10px",
81
- fontFamily : "Cheeronsta",
82
- background : background ?? "orange"
83
- }).vertical(-1, "space-around")
84
- ```
63
+ Hello {(()=>{
64
+ const names = ["world", "everyone"];
65
+ const {length} = names
66
+ return names[Math.floor(Math.random()*length)]
67
+ })()}
68
+ ```
69
+ - **Internal scripts :** Include JS logic that runs alongside MDZjs components but isn't rendered in the output.
70
+ They can initialize variables or perform side effects...
71
+
72
+ ```html
73
+ <script>
74
+ console.log("Hello from MDZjs")
75
+ let addons = [
76
+ "ziko-gl",
77
+ "ziko-lottie",
78
+ "ziko-chart"
79
+ ]
80
+ // The addons variable can be accessed and used in MDZjs expressions
81
+ </script>
82
+ ```
83
+ - **Interleaving :** You can use inline markdown elements inside HTML or Zikojs Components
84
+
85
+ ```html
86
+ <Header background={background}>
87
+ ***Hello {name}***
88
+ </Header>
89
+ ```
90
+ # ⭐️ Show your support
91
+
92
+ If you appreciate the project, kindly demonstrate your support by giving it a star!<br>
93
+
94
+ [![Star](https://img.shields.io/github/stars/zakarialaoui10/mdzjs?style=social)](https://github.com/zakarialaoui10/mdzjs)
95
+ <!--## Financial support-->
96
+ # License
97
+ This projet is licensed under the terms of MIT License
98
+ <img src="https://img.shields.io/github/license/zakarialaoui10/mdzjs?color=rgb%2820%2C21%2C169%29" width="100" align="right">
@@ -0,0 +1,2 @@
1
+ export {parseMDZ} from "./parser/index.js"
2
+ export {MDZ} from "./vite/index.js"
@@ -0,0 +1,21 @@
1
+ import { getZikoScript } from "./converter/index.js";
2
+ const markdownContent = `
3
+ ---
4
+ modules:
5
+ - import Component from "./Component"
6
+ - import OtherComponent from "./OtherComponent"
7
+ - import OtherComonenetWithProps from "./OtherComonenetWithProps"
8
+ title: MDZ
9
+ ---
10
+ Para
11
+ [Link](#hi)
12
+
13
+ <Component />
14
+
15
+ line2
16
+
17
+ <OtherComponent />
18
+ <OtherComonenetWithProps data="hello" />
19
+ `;
20
+ const script = getZikoScript(markdownContent);
21
+ console.log(script)
@@ -0,0 +1,77 @@
1
+ import { transpileMDZ } from "./transpiler/index.js";
2
+
3
+ let cos = Math.cos
4
+ const Markdown = `---
5
+ title: "Example Page"
6
+ author: "John Doe"
7
+ count : 10
8
+ __Props__ : {
9
+ color : "red",
10
+ id : 10
11
+ }
12
+ ---
13
+
14
+ import Button from './Button.js';
15
+
16
+ import {cos} from "ziko";
17
+
18
+ # Hello World
19
+ <Button
20
+ data="100"
21
+ p="kkk"
22
+ />
23
+
24
+ This is a paragraph with <Button data="hello"/>
25
+
26
+ <p> Hello {1+1} </p>
27
+
28
+ <Button
29
+ data="100"
30
+ p="kkk"
31
+ m={1+1}
32
+ />
33
+
34
+
35
+ Hello {(()=>{
36
+ let a = Math.random()
37
+ return 1/a
38
+ })()} , How are you ?
39
+
40
+ <script>
41
+ let a=10
42
+ </script>
43
+
44
+ {(async () => {
45
+ const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
46
+ const data = await response.json()
47
+ return data
48
+ })()}
49
+
50
+ {const sayHi=()=>{
51
+
52
+ return "Hi"
53
+ }
54
+ }
55
+
56
+ Test {sayHi()}
57
+
58
+ <script>
59
+ let a=1
60
+ </script>
61
+ `;
62
+
63
+ let res = transpileMDZ(Markdown)
64
+ console.log(res)
65
+
66
+ // const md2 = "```js \n console.log(1) \n ```"
67
+ // const md2 = `
68
+ // ---
69
+ // title : "MDZ"
70
+ // background : "red"
71
+ // ---
72
+
73
+ // Hello World
74
+ // `
75
+ // console.log(md2)
76
+ // console.log(transpileMDZ(md2))
77
+
@@ -0,0 +1,67 @@
1
+ export function cleanMD(str, openSign, closeSign) {
2
+ const result = [];
3
+ let currentExpr = '';
4
+ let currentText = '';
5
+ let braceCount = 0;
6
+ let isEscaped = false;
7
+
8
+ for (let i = 0; i < str.length; i++) {
9
+ const char = str[i];
10
+
11
+ if (char === '\\' && !isEscaped) {
12
+ // Set escape flag and skip adding the backslash for now
13
+ isEscaped = true;
14
+ continue;
15
+ }
16
+
17
+ if (isEscaped) {
18
+ // If escaped, append the literal character and reset escape flag
19
+ currentText += char;
20
+ isEscaped = false;
21
+ continue;
22
+ }
23
+
24
+ if (char === '{') {
25
+ if (braceCount === 0 && currentText) {
26
+ result.push(currentText.trim());
27
+ currentText = '';
28
+ }
29
+ braceCount++;
30
+ currentExpr += char;
31
+ } else if (char === '}') {
32
+ braceCount--;
33
+ currentExpr += char;
34
+ if (braceCount === 0) {
35
+ result.push(cleanExpression(currentExpr, openSign, closeSign));
36
+ currentExpr = '';
37
+ }
38
+ } else {
39
+ if (braceCount === 0) {
40
+ currentText += char;
41
+ } else {
42
+ currentExpr += char;
43
+ }
44
+ }
45
+ }
46
+
47
+ if (currentText) {
48
+ result.push(currentText.trim());
49
+ }
50
+
51
+ return result.join('');
52
+ }
53
+
54
+ function cleanExpression(block, openSign = "", closeSign = "") {
55
+ const content = block.slice(1, -1);
56
+ const cleanedContent = content
57
+ .split('\n')
58
+ .map(line => line.trim())
59
+ .filter(line => line)
60
+ .join('\n');
61
+ return `${openSign}{${cleanedContent}}${closeSign}`;
62
+ // return `${openSign}{\n${cleanedContent}\n}${closeSign}`;
63
+ }
64
+
65
+ // const input = "Normal text {expression here} and \\{escaped brace\\}";
66
+ // console.log(cleanMD(input, "[", "]"));
67
+
package/__v2__/hast.js ADDED
@@ -0,0 +1,70 @@
1
+ import { parseDocument } from 'htmlparser2';
2
+ import { cleanMD } from './clean.js';
3
+ import { splitBetween } from './utils/split-between.js';
4
+ import { isObject } from './utils/is-object.js';
5
+ const OPEN_SIGN = "\"EXP_START"
6
+ const CLOSE_SIGN = "EXP_END\""
7
+ function transformNode(node) {
8
+ switch (node.type) {
9
+ case 'text': {
10
+ const text = node.data;
11
+ const cleanedText = cleanMD(text, OPEN_SIGN, CLOSE_SIGN)
12
+ const args = splitBetween(cleanedText, OPEN_SIGN, CLOSE_SIGN).map(({type, value})=>{
13
+ if(type === "string") return `"${value}"`
14
+ return value.slice(1,-1)
15
+ })
16
+ return args.length!==0 ? args : null;
17
+ }
18
+ case 'tag': {
19
+ const tagName = node.rawTagName || node.name;
20
+ const isJSXComponent = /^[A-Z]/.test(tagName);
21
+
22
+ const attributes = node.attribs
23
+ ? Object.entries(node.attribs)
24
+ .map(([key, value]) => `${key}: ${
25
+ isObject(value)
26
+ ?value
27
+ :(value.at(0)==="{")?value.slice(1,-1):'"'+value+'"'
28
+ }`)
29
+ .join(', ')
30
+ : '';
31
+ const childNodes = node.children
32
+ .map(transformNode)
33
+ .filter(Boolean)
34
+ .join(', ');
35
+
36
+ if (isJSXComponent) {
37
+ if(node.children.length > 0) return `${tagName}({${attributes}}, ${childNodes})`
38
+ return `${tagName}({${attributes}})`;
39
+ }
40
+ else {
41
+ if(node.children.length > 0) return `tag('${tagName}', {${attributes}}, ${childNodes})`;
42
+ return `tag('${tagName}', {${attributes}})`
43
+ }
44
+ }
45
+ default:
46
+ return '';
47
+ }
48
+ }
49
+
50
+
51
+
52
+ const html = `
53
+ <p ref="ll" id={m} ob={a:1}>Hi {exp} \\{</p>
54
+ <p ref="ll">{exp} \\{</p>
55
+ <p ref="ll" id={m} ob={a:1}>Hi {exp} \\{</p>
56
+ <p ref="ll">{exp} \\{</p>
57
+ <ZikoComp test=1>
58
+ <p> Hi </p>
59
+ </ZikoComp>
60
+ <Void />
61
+ `.trim();
62
+
63
+ const ast = parseDocument(html, { lowerCaseTags: false });
64
+
65
+ const transformedCode = ast.children
66
+ .map(transformNode)
67
+ .filter(Boolean)
68
+ // .join(', ');
69
+
70
+ console.log(transformedCode);
@@ -0,0 +1,59 @@
1
+ import { parseDocument } from 'htmlparser2';
2
+
3
+ function transformNode(node) {
4
+ switch (node.type) {
5
+ case 'text': {
6
+ const text = node.data;
7
+ return processText(text);
8
+ }
9
+ case 'tag': {
10
+ const tagName = node.rawTagName || node.name;
11
+ const isJSXComponent = /^[A-Z]/.test(tagName);
12
+
13
+ const attributes = node.attribs
14
+ ? Object.entries(node.attribs)
15
+ .map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
16
+ .join(', ')
17
+ : '';
18
+
19
+ const childNodes = node.children
20
+ .map(transformNode)
21
+ .filter(Boolean) // Remove empty strings from children
22
+ .join(', ');
23
+
24
+ if (isJSXComponent) {
25
+ return `${tagName}({${attributes}}, ${childNodes})`;
26
+ } else {
27
+ return `tag('${tagName}', {${attributes}}, ${childNodes})`;
28
+ }
29
+ }
30
+ default:
31
+ return '';
32
+ }
33
+ }
34
+
35
+ function processText(text) {
36
+ return JSON.stringify(text.trim());
37
+ }
38
+
39
+ const html = `<p>
40
+ Hello world
41
+ <span>
42
+ hi
43
+ </span>
44
+ <JsxComponent data={zikojs} />
45
+
46
+ <JsxComponent>
47
+ <Js />
48
+ <Js />
49
+ </JsxComonent>
50
+ </p>`;
51
+
52
+ const ast = parseDocument(html, { lowerCaseTags: false }); // Disable automatic lowercase conversion
53
+
54
+ const transformedCode = ast.children
55
+ .map(transformNode)
56
+ .filter(Boolean) // Remove empty strings from top-level nodes
57
+ .join(', ');
58
+
59
+ console.log(transformedCode);
@@ -0,0 +1,2 @@
1
+ export { transpileMDZ } from "./transpiler/index.js"
2
+ export {ViteMDZ} from "./transformers/vite/index.js"
@@ -0,0 +1,93 @@
1
+ function cleanMD(str, openSign, closeSign) {
2
+ const result = [];
3
+ let currentExpr = '';
4
+ let currentText = '';
5
+ let braceCount = 0;
6
+ for (let i = 0; i < str.length; i++) {
7
+ const char = str[i];
8
+ if (char === '{') {
9
+ if (braceCount === 0 && currentText) {
10
+ result.push(currentText.trim());
11
+ currentText = '';
12
+ }
13
+ braceCount++;
14
+ currentExpr += char;
15
+ } else if (char === '}') {
16
+ braceCount--;
17
+ currentExpr += char;
18
+ if (braceCount === 0) {
19
+ result.push(cleanExpression(currentExpr, openSign, closeSign));
20
+ currentExpr = '';
21
+ }
22
+ } else {
23
+ if (braceCount === 0) {
24
+ currentText += char;
25
+ } else {
26
+ currentExpr += char;
27
+ }
28
+ }
29
+ }
30
+ if (currentText) {
31
+ result.push(currentText.trim());
32
+ }
33
+
34
+ return result.join('');
35
+ }
36
+
37
+ function cleanExpression(block, openSign="", closeSign ="") {
38
+ const content = block.slice(1, -1);
39
+ const cleanedContent = content
40
+ .split('\n')
41
+ .map(line => line.trim())
42
+ .filter(line => line)
43
+ .join('\n');
44
+ return `${openSign}{\n${cleanedContent}\n}${closeSign}`;
45
+ }
46
+
47
+ // const text = `
48
+ // const obj = {
49
+
50
+ // key1: 'value1',
51
+
52
+
53
+ // key2: 'value2',
54
+
55
+
56
+ // key3: {
57
+ // nestedKey1: 'nestedValue1',
58
+
59
+
60
+ // nestedKey2: 'nestedValue2',
61
+
62
+
63
+ // nestedKey3: {
64
+
65
+ // deepKey: 'deepValue'
66
+
67
+ // }
68
+
69
+ // }
70
+
71
+
72
+ // };
73
+
74
+ // export default {
75
+
76
+
77
+ // keyA: 'valueA',
78
+
79
+
80
+ // keyB: {
81
+
82
+ // nestedKey: 'nestedValue'
83
+
84
+ // }
85
+
86
+ // };
87
+ // `;
88
+
89
+ // console.log(cleanMD(text));
90
+
91
+ export{
92
+ cleanMD
93
+ }
@@ -0,0 +1,17 @@
1
+ ## features :
2
+
3
+ ✅ Frontmatter
4
+ ✅ Props
5
+ ✅ Expression value in props
6
+ ✅ Markdown
7
+ ✅ Html
8
+ ✅ Zikojs Components
9
+
10
+
11
+ ## To Do
12
+
13
+ Async Directive
14
+ Expressions
15
+ Smart Links
16
+ Latex
17
+ Math
package/__v2__/test.js ADDED
@@ -0,0 +1,19 @@
1
+ import { transpileMDZ } from "./transpiler/index.js";
2
+
3
+ const Markdown = `
4
+ ---
5
+ title : hi
6
+ __Props__ :
7
+ m : 1
8
+
9
+ ---
10
+
11
+ # Hi
12
+
13
+ {
14
+ 1+1
15
+ }
16
+ <p> Hi </p>
17
+ `
18
+ let res = transpileMDZ(Markdown)
19
+ console.log(res)
@@ -0,0 +1,16 @@
1
+ import { transpileMDZ } from "../../transpiler/index.js";
2
+ export function ViteMDZ(){
3
+ return {
4
+ name: 'MDZ',
5
+ transform(src, id) {
6
+ // console.log({tr : transpileMDZ(src)})
7
+ if (id.endsWith('.mdz')) {
8
+ return {
9
+ code: transpileMDZ(src),
10
+ map: null,
11
+ };
12
+ }
13
+ },
14
+ };
15
+ }
16
+
@@ -0,0 +1,27 @@
1
+ function getComponentType(component) {
2
+ const trimmedComponent = component.trim();
3
+
4
+ const tagRegex = /^<([a-zA-Z][a-z0-9]*)/; // Match opening HTML/JSX tag
5
+ const match = trimmedComponent.match(tagRegex);
6
+
7
+ if (match) {
8
+ const tagName = match[1];
9
+
10
+ // Check if the first letter is uppercase - PascalCase component name
11
+ if (tagName[0] === tagName[0].toUpperCase()) {
12
+ return "jsx";
13
+ }
14
+
15
+ // Return "script" if it's a script tag
16
+ if (tagName.toLowerCase() === 'script') {
17
+ return "script";
18
+ }
19
+
20
+ // Otherwise, it's just regular HTML
21
+ return "html";
22
+ }
23
+
24
+ return "unknown";
25
+ }
26
+
27
+ export { getComponentType };
@@ -0,0 +1,19 @@
1
+ import { visit } from 'unist-util-visit';
2
+
3
+ const remarkImports = () => {
4
+ return (tree, file) => {
5
+ const imports = [];
6
+
7
+ visit(tree, 'html', (node, index, parent) => {
8
+ if (node.value.trim().startsWith('import ')) {
9
+ imports.push(node.value.trim());
10
+ parent.children.splice(index, 1);
11
+ }
12
+ });
13
+ file.data.imports = imports;
14
+ };
15
+ };
16
+
17
+ export {
18
+ remarkImports
19
+ };