Skip to content

Commit 450b66a

Browse files
.
1 parent 0847117 commit 450b66a

File tree

10 files changed

+182
-193
lines changed

10 files changed

+182
-193
lines changed

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"remark-parse": "^11.0.0",
3030
"rollup": "^4.24.0",
3131
"unified": "^11.0.5",
32-
"unist-util-visit": "^5.0.0",
3332
"yaml": "^2.6.1",
3433
"ziko": "^0.0.20"
3534
},

v2/fm.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

v2/index-marked.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

v2/index.js

Lines changed: 0 additions & 111 deletions
This file was deleted.

v2/test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { transpileMDZ } from "./transpiler/index.js";
2+
const Markdown = `
3+
---
4+
title: "Example Page"
5+
author: "John Doe"
6+
count : 10
7+
---
8+
9+
import Button from './Button.js';
10+
11+
# Hello World <Button data = "100"/>
12+
13+
This is a paragraph with <Button data="hello"/>
14+
15+
<p> Hello </p>
16+
17+
[ll](#)
18+
19+
***uuu***
20+
![alt](#)
21+
22+
23+
<InteractiveBlog />
24+
`;
25+
26+
console.log(transpileMDZ(Markdown))
27+
28+
// const md2 = "```js \n console.log(1) \n ```"
29+
// console.log(transpileMDZ(md2))

v2/transformers/vite/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { transpileMDZ } from "../../transpiler/index.js";
2+
export function MDZ(){
3+
return {
4+
name: 'MDZ',
5+
transform(src, id) {
6+
if (id.endsWith('.mdz')) {
7+
console.log(src)
8+
return {
9+
code: transpileMDZ(src),
10+
map: null,
11+
};
12+
}
13+
},
14+
};
15+
}
16+
File renamed without changes.

v2/transpiler/index.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 { parse as parseYaml } from 'yaml';
6+
7+
const parseMDZ = (markdown) => unified().use(remarkParse).parse(markdown);
8+
9+
const processMDZ = (markdown) => {
10+
// Frontmatter regex (between '---' or '+++')
11+
const frontmatterRegex = /^---\n([\s\S]+?)\n---/m;
12+
const importRegex = /^import\s+.+from\s+.+;$/gm;
13+
14+
let imports = ['import {tag, Flex, HTMLWrapper} from "ziko"'];
15+
let frontmatter = {};
16+
let cleanedMarkdown = markdown;
17+
18+
const frontmatterMatch = markdown.match(frontmatterRegex);
19+
if (frontmatterMatch) {
20+
const frontmatterContent = frontmatterMatch[1];
21+
frontmatter = parseYaml(frontmatterContent);
22+
cleanedMarkdown = cleanedMarkdown.replace(frontmatterRegex, '');
23+
}
24+
25+
cleanedMarkdown = cleanedMarkdown.replace(importRegex, (match) => {
26+
imports.push(match.trim());
27+
return '';
28+
});
29+
return { imports, frontmatter, cleanedMarkdown };
30+
};
31+
32+
const transformMDZ = (markdownAST) => {
33+
const transformNode = (node) => {
34+
// console.log(node.type)
35+
switch(node.type){
36+
case 'text' : return JSON.stringify(node.value);
37+
case 'paragraph' : {
38+
const childNodes = node.children.map(transformNode).join(', ');
39+
return ` tag('p', {}, ${childNodes})`;
40+
}
41+
case 'heading' : {
42+
const childNodes = node.children.map(transformNode).join(', ');
43+
return ` tag('h${node.depth}', {}, ${childNodes})`;
44+
}
45+
case 'strong': {
46+
const childNodes = node.children.map(transformNode).join(', ');
47+
return ` tag('strong', {}, ${childNodes})`;
48+
}
49+
50+
case 'emphasis': {
51+
const childNodes = node.children.map(transformNode).join(', ');
52+
return ` tag('em', {}, ${childNodes})`;
53+
}
54+
55+
case 'link': {
56+
const childNodes = node.children.map(transformNode).join(', ');
57+
return ` tag('a', { href: "${node.url}" }, ${childNodes})`;
58+
}
59+
60+
case 'image': {
61+
return ` tag('img', { src: "${node.url}", alt: "${node.alt || ''}" })`;
62+
}
63+
64+
case 'list': {
65+
const listTag = node.ordered ? 'ol' : 'ul';
66+
const childNodes = node.children.map(transformNode).join(', ');
67+
return ` tag('${listTag}', {}, ${childNodes})`;
68+
}
69+
70+
case 'listItem': {
71+
const childNodes = node.children.map(transformNode).join(', ');
72+
return ` tag('li', {}, ${childNodes})`;
73+
}
74+
75+
case 'code': {
76+
const language = node.lang ? `, { 'data-lang': '${node.lang}' }` : '';
77+
return ` tag('pre', {}, tag('code'${language}, {}, ${JSON.stringify(node.value)}))`;
78+
}
79+
80+
case 'blockquote': {
81+
const childNodes = node.children.map(transformNode).join(', ');
82+
return ` tag('blockquote', {}, ${childNodes})`;
83+
}
84+
case 'thematicBreak': {
85+
return ` tag('hr', {}, '')`;
86+
}
87+
case 'html' : {
88+
const component = node.value.trim();
89+
const type = getComponentType(component);
90+
switch (type) {
91+
case 'jsx':
92+
return ` ${jsx2js(component)}`;
93+
case 'html':
94+
return ` HTMLWrapper("${component}")`;
95+
default:
96+
return null;
97+
}
98+
}
99+
100+
101+
}
102+
return 'null';
103+
};
104+
105+
const body = markdownAST.children.map(transformNode).join(',\n');
106+
return `()=>Flex(
107+
${body}
108+
).vertical(0,0)`
109+
};
110+
111+
const transpileMDZ= markdown =>{
112+
const { imports, frontmatter, cleanedMarkdown } = processMDZ(markdown);
113+
const ast = parseMDZ(cleanedMarkdown);
114+
const jsFunction = transformMDZ(ast);
115+
let exports = `
116+
const attributes = ${JSON.stringify(frontmatter)};
117+
export {${Object.keys(frontmatter)}} = attributes;
118+
`
119+
const Output = [
120+
...imports,
121+
jsFunction,
122+
exports,
123+
].join('\n');
124+
125+
return Output
126+
127+
}
128+
export {transpileMDZ}

0 commit comments

Comments
 (0)