onenote-cli 0.2.1-win32-arm64 → 0.2.1

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.
@@ -0,0 +1,134 @@
1
+ function escapeHtml(text: string): string {
2
+ return text
3
+ .replace(/&/g, "&")
4
+ .replace(/</g, "&lt;")
5
+ .replace(/>/g, "&gt;")
6
+ .replace(/"/g, "&quot;")
7
+ .replace(/'/g, "&#39;");
8
+ }
9
+
10
+ function formatInline(markdown: string): string {
11
+ const codeSpans: string[] = [];
12
+ let text = markdown.replace(/`([^`]+)`/g, (_match, code: string) => {
13
+ const token = `\u0000CODE${codeSpans.length}\u0000`;
14
+ codeSpans.push(`<code>${escapeHtml(code)}</code>`);
15
+ return token;
16
+ });
17
+
18
+ text = escapeHtml(text);
19
+ text = text
20
+ .replace(
21
+ /\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g,
22
+ '<a href="$2">$1</a>'
23
+ )
24
+ .replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
25
+ .replace(/__([^_]+)__/g, "<strong>$1</strong>")
26
+ .replace(/(^|[^\w])\*([^*\n]+)\*(?!\w)/g, "$1<em>$2</em>")
27
+ .replace(/(^|[^\w])_([^_\n]+)_(?!\w)/g, "$1<em>$2</em>");
28
+
29
+ return text.replace(/\u0000CODE(\d+)\u0000/g, (_match, index: string) => {
30
+ return codeSpans[Number(index)] ?? "";
31
+ });
32
+ }
33
+
34
+ function flushParagraph(lines: string[], out: string[]) {
35
+ if (lines.length === 0) return;
36
+ out.push(`<p>${lines.map((line) => formatInline(line)).join("<br />")}</p>`);
37
+ lines.length = 0;
38
+ }
39
+
40
+ function closeList(openList: "ul" | "ol" | null, out: string[]): null {
41
+ if (!openList) return null;
42
+ out.push(`</${openList}>`);
43
+ return null;
44
+ }
45
+
46
+ export function markdownToHtml(markdown: string): string {
47
+ const normalized = markdown.replace(/\r\n/g, "\n").trim();
48
+ if (!normalized) return "<p></p>";
49
+
50
+ const out: string[] = [];
51
+ const lines = normalized.split("\n");
52
+ const paragraph: string[] = [];
53
+ let openList: "ul" | "ol" | null = null;
54
+
55
+ for (let i = 0; i < lines.length; i += 1) {
56
+ const line = lines[i];
57
+ const trimmed = line.trim();
58
+
59
+ if (!trimmed) {
60
+ flushParagraph(paragraph, out);
61
+ openList = closeList(openList, out);
62
+ continue;
63
+ }
64
+
65
+ const fenceMatch = trimmed.match(/^```([\w-]+)?$/);
66
+ if (fenceMatch) {
67
+ flushParagraph(paragraph, out);
68
+ openList = closeList(openList, out);
69
+ const codeLines: string[] = [];
70
+ let j = i + 1;
71
+ while (j < lines.length && !lines[j].trim().startsWith("```")) {
72
+ codeLines.push(lines[j]);
73
+ j += 1;
74
+ }
75
+ const language = fenceMatch[1] ? ` class="language-${escapeHtml(fenceMatch[1])}"` : "";
76
+ out.push(`<pre><code${language}>${escapeHtml(codeLines.join("\n"))}</code></pre>`);
77
+ i = j;
78
+ continue;
79
+ }
80
+
81
+ const headingMatch = trimmed.match(/^(#{1,6})\s+(.+)$/);
82
+ if (headingMatch) {
83
+ flushParagraph(paragraph, out);
84
+ openList = closeList(openList, out);
85
+ const level = headingMatch[1].length;
86
+ out.push(`<h${level}>${formatInline(headingMatch[2].trim())}</h${level}>`);
87
+ continue;
88
+ }
89
+
90
+ if (/^([-*_])(?:\s*\1){2,}$/.test(trimmed)) {
91
+ flushParagraph(paragraph, out);
92
+ openList = closeList(openList, out);
93
+ out.push("<hr />");
94
+ continue;
95
+ }
96
+
97
+ const bulletMatch = trimmed.match(/^[-*+]\s+(.+)$/);
98
+ if (bulletMatch) {
99
+ flushParagraph(paragraph, out);
100
+ if (openList !== "ul") {
101
+ openList = closeList(openList, out);
102
+ out.push("<ul>");
103
+ openList = "ul";
104
+ }
105
+ out.push(`<li>${formatInline(bulletMatch[1])}</li>`);
106
+ continue;
107
+ }
108
+
109
+ const orderedMatch = trimmed.match(/^\d+\.\s+(.+)$/);
110
+ if (orderedMatch) {
111
+ flushParagraph(paragraph, out);
112
+ if (openList !== "ol") {
113
+ openList = closeList(openList, out);
114
+ out.push("<ol>");
115
+ openList = "ol";
116
+ }
117
+ out.push(`<li>${formatInline(orderedMatch[1])}</li>`);
118
+ continue;
119
+ }
120
+
121
+ if (trimmed.startsWith(">")) {
122
+ flushParagraph(paragraph, out);
123
+ openList = closeList(openList, out);
124
+ out.push(`<blockquote><p>${formatInline(trimmed.replace(/^>\s?/, ""))}</p></blockquote>`);
125
+ continue;
126
+ }
127
+
128
+ paragraph.push(trimmed);
129
+ }
130
+
131
+ flushParagraph(paragraph, out);
132
+ closeList(openList, out);
133
+ return out.join("\n");
134
+ }