md-editor-lite 0.1.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/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./components";
2
+ export * from "./constants";
3
+ export * from "./hooks";
4
+ export * from "./utils";
@@ -0,0 +1,3 @@
1
+ export function cx(...classes: (string | false | undefined)[]) {
2
+ return classes.filter(Boolean).join(" ");
3
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./cx";
2
+ export * from "./markdown";
@@ -0,0 +1,89 @@
1
+ function parseBold(text: string): string {
2
+ return text.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
3
+ }
4
+
5
+ function parseItalic(text: string): string {
6
+ return text.replace(/\*(.+?)\*/g, "<em>$1</em>");
7
+ }
8
+
9
+ function parseInlineCode(text: string): string {
10
+ return text.replace(/`(.+?)`/g, "<code>$1</code>");
11
+ }
12
+
13
+ function parseBlockquote(text: string): string {
14
+ return text.replace(/^> (.+)$/gm, "<blockquote>$1</blockquote>");
15
+ }
16
+
17
+ function parseImages(text: string): string {
18
+ return text.replace(
19
+ /!\[(.*?)\]\(([^)]*?)\)/g,
20
+ (_, alt: string, url: string) => `<img src="${url}" alt="${alt || ""}" />`
21
+ );
22
+ }
23
+
24
+ function parseLinks(text: string): string {
25
+ return text.replace(/\[(.+?)\]\((.+?)\)/g, '<a href="$2">$1</a>');
26
+ }
27
+
28
+ function parseHeadings(text: string): string {
29
+ return text
30
+ .replace(/^# (.+)$/gm, "<h1>$1</h1>")
31
+ .replace(/^## (.+)$/gm, "<h2>$1</h2>")
32
+ .replace(/^### (.+)$/gm, "<h3>$1</h3>");
33
+ }
34
+
35
+ function parseUnorderedList(text: string): string {
36
+ return text.replace(
37
+ /(^|\r?\n)((?:\s*- .+(?:\r?\n|$))+)/g,
38
+ (_, prefix, block) => {
39
+ const items = block
40
+ .trim()
41
+ .split(/\r?\n/)
42
+ .map((line: string) => line.replace(/^\s*- /, "").trim())
43
+ .filter(Boolean)
44
+ .map((item: string) => `<li>${item}</li>`)
45
+ .join("");
46
+ return `${prefix}<ul>${items}</ul>`;
47
+ }
48
+ );
49
+ }
50
+
51
+ function parseOrderedList(text: string): string {
52
+ return text.replace(
53
+ /(^|\r?\n)((?:\d+\. .+(?:\r?\n|$))+)/g,
54
+ (_, prefix, block) => {
55
+ const items = block
56
+ .trim()
57
+ .split(/\r?\n/)
58
+ .map((line: string) => line.replace(/^\d+\. /, "").trim())
59
+ .filter(Boolean)
60
+ .map((item: string) => `<li>${item}</li>`)
61
+ .join("");
62
+ return `${prefix}<ol>${items}</ol>`;
63
+ }
64
+ );
65
+ }
66
+
67
+ function parseLineBreaks(text: string): string {
68
+ return text.replace(
69
+ /(?<!<\/ul>|<\/ol>|<\/li>|<\/h1>|<\/h2>|<\/h3>)\n/g,
70
+ "<br/>"
71
+ );
72
+ }
73
+
74
+ export function markdownToHtml(markdown: string): string {
75
+ let html = markdown;
76
+
77
+ html = parseBold(html);
78
+ html = parseItalic(html);
79
+ html = parseInlineCode(html);
80
+ html = parseBlockquote(html);
81
+ html = parseImages(html);
82
+ html = parseLinks(html);
83
+ html = parseHeadings(html);
84
+ html = parseUnorderedList(html);
85
+ html = parseOrderedList(html);
86
+ html = parseLineBreaks(html);
87
+
88
+ return html;
89
+ }
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+
7
+ "jsx": "react-jsx",
8
+
9
+ "declaration": true,
10
+ "emitDeclarationOnly": false,
11
+
12
+ "strict": true,
13
+ "skipLibCheck": true,
14
+
15
+ "baseUrl": "."
16
+ },
17
+ "include": ["src"]
18
+ }