@mdream/js 2.0.0-beta.1 → 2.0.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.
Files changed (40) hide show
  1. package/README.md +402 -126
  2. package/dist/_chunks/clean.mjs +526 -0
  3. package/dist/_chunks/const.mjs +3 -7
  4. package/dist/_chunks/extraction.d.mts +14 -0
  5. package/dist/_chunks/html.mjs +260 -0
  6. package/dist/_chunks/markdown-processor.mjs +1077 -828
  7. package/dist/_chunks/minimal.mjs +29 -0
  8. package/dist/_chunks/output-runner.mjs +54 -0
  9. package/dist/_chunks/parse.mjs +134 -1501
  10. package/dist/_chunks/plugin.mjs +14 -0
  11. package/dist/_chunks/src.mjs +291 -0
  12. package/dist/_chunks/tag-overrides.mjs +138 -0
  13. package/dist/_chunks/{plugins.mjs → tailwind.mjs} +145 -292
  14. package/dist/_chunks/text.mjs +583 -0
  15. package/dist/_chunks/types.d.mts +136 -126
  16. package/dist/_chunks/url.mjs +82 -0
  17. package/dist/_chunks/utils.mjs +659 -0
  18. package/dist/clean.d.mts +10 -0
  19. package/dist/clean.mjs +2 -0
  20. package/dist/cli.mjs +47 -8
  21. package/dist/html.d.mts +6 -0
  22. package/dist/html.mjs +2 -0
  23. package/dist/index.d.mts +5 -4
  24. package/dist/index.mjs +4 -58
  25. package/dist/parse.d.mts +15 -10
  26. package/dist/plugins.d.mts +13 -28
  27. package/dist/plugins.mjs +52 -2
  28. package/dist/preset/minimal.d.mts +14 -2
  29. package/dist/preset/minimal.mjs +1 -32
  30. package/dist/splitter.d.mts +12 -4
  31. package/dist/splitter.mjs +386 -113
  32. package/dist/text.d.mts +6 -0
  33. package/dist/text.mjs +2 -0
  34. package/package.json +34 -8
  35. package/dist/_chunks/index.d.mts +0 -9
  36. package/dist/_chunks/minimal.d.mts +0 -6
  37. package/dist/_chunks/resolve-plugins.mjs +0 -44
  38. package/dist/_chunks/stream.mjs +0 -37
  39. package/dist/core.d.mts +0 -15
  40. package/dist/core.mjs +0 -28
@@ -0,0 +1,260 @@
1
+ import { TagIdMap } from "./const.mjs";
2
+ import { escapeHtml, getLanguageFromClass, isSafeHtmlUrl, markRenderedChildContent, parseUnsignedInteger } from "./utils.mjs";
3
+ import { resolveUrl, safeAnchorOutput } from "./url.mjs";
4
+ import { assertEngineOptions, buildTagOverrideHandlers, checkClean } from "./tag-overrides.mjs";
5
+ import { parserTagHandlers } from "./parse.mjs";
6
+ import { resolvePlugins } from "./plugin.mjs";
7
+ import { processHtmlOutput, streamHtmlOutput } from "./output-runner.mjs";
8
+ import { slugify, stripHeadingFormatting } from "./clean.mjs";
9
+ function createHtmlOutputState() {
10
+ return {
11
+ frames: [],
12
+ hasOutput: false
13
+ };
14
+ }
15
+ function slugifyHeading(text) {
16
+ return slugify(stripHeadingFormatting(text));
17
+ }
18
+ function tagName(node, tagId) {
19
+ switch (tagId) {
20
+ case 14:
21
+ case 15: return "strong";
22
+ case 16:
23
+ case 17: return "em";
24
+ case 18:
25
+ case 110:
26
+ case 111: return "del";
27
+ }
28
+ if (!(tagId === 2 || tagId === 3 || tagId >= 14 && tagId <= 26 || tagId >= 28 && tagId <= 33 || tagId >= 35 && tagId <= 39 || tagId === 41 || tagId >= 46 && tagId <= 47 || tagId >= 50 && tagId <= 51 || tagId >= 79 && tagId <= 83 || tagId >= 88 && tagId <= 93 || tagId >= 95 && tagId <= 102 || tagId >= 104 && tagId <= 107)) return;
29
+ if (node.tagHandler?.aliasTagId === void 0) return node.name;
30
+ for (const name in TagIdMap) if (TagIdMap[name] === tagId) return name;
31
+ }
32
+ function resolveTagId(node) {
33
+ return node.tagHandler?.aliasTagId ?? node.tagId;
34
+ }
35
+ function resolveSafeUrl(url, options, image = false) {
36
+ if (!isSafeHtmlUrl(url, image)) return;
37
+ const resolved = resolveUrl(url, options.origin, options.clean);
38
+ return isSafeHtmlUrl(resolved, image) ? resolved : void 0;
39
+ }
40
+ function safeAttributes(node, tagId) {
41
+ const attributes = node.attributes;
42
+ if (tagId === 33) {
43
+ const start = parseUnsignedInteger(attributes.start);
44
+ return start === void 0 ? "" : ` start="${start}"`;
45
+ }
46
+ if (tagId === 23) {
47
+ const language = getLanguageFromClass(attributes.class);
48
+ return language ? ` class="language-${escapeHtml(language, true)}"` : "";
49
+ }
50
+ if (tagId === 31 || tagId === 32) {
51
+ let output = "";
52
+ const colspan = parseUnsignedInteger(attributes.colspan);
53
+ if (colspan !== void 0 && colspan > 0) output += ` colspan="${colspan}"`;
54
+ if (tagId === 31) {
55
+ const align = attributes.align?.toLowerCase();
56
+ if (align === "left" || align === "center" || align === "right") output += ` align="${align}"`;
57
+ }
58
+ return output;
59
+ }
60
+ return "";
61
+ }
62
+ function append(state, output, value) {
63
+ const frame = state.frames.at(-1);
64
+ if (frame) frame.output += value;
65
+ else {
66
+ output.push(value);
67
+ if (value) state.hasOutput = true;
68
+ }
69
+ }
70
+ function processText(node, state, output) {
71
+ if (node.excludedFromMarkdown || !node.value) return;
72
+ let value = node.value;
73
+ if (!state.frames.length && !state.hasOutput) {
74
+ state.hasOutput = output.some((fragment) => fragment.length > 0);
75
+ if (!state.hasOutput) {
76
+ let start = 0;
77
+ while (start < value.length) {
78
+ const code = value.charCodeAt(start);
79
+ if (code !== 32 && code !== 9 && code !== 10 && code !== 12 && code !== 13) break;
80
+ start++;
81
+ }
82
+ value = value.slice(start);
83
+ if (!value) return;
84
+ }
85
+ }
86
+ for (let index = state.frames.length - 1; index >= 0; index--) {
87
+ const frame = state.frames[index];
88
+ if (frame._tag === 0) {
89
+ frame.text += value;
90
+ break;
91
+ }
92
+ }
93
+ append(state, output, escapeHtml(value));
94
+ }
95
+ function closeFrame(state, output, frame) {
96
+ state.frames.pop();
97
+ if (frame._tag === 0) {
98
+ const slug = slugifyHeading(frame.text);
99
+ const id = slug ? ` id="${slug}"` : "";
100
+ append(state, output, `<h${frame.level}${id}>${frame.output}</h${frame.level}>`);
101
+ return;
102
+ }
103
+ append(state, output, `<pre tabindex="0"><code${frame.language ? ` class="language-${escapeHtml(frame.language, true)}"` : ""}>${frame.output}</code></pre>`);
104
+ }
105
+ function processHtmlOutputEvent(event, state, output, options) {
106
+ const { node, type } = event;
107
+ if (node.type === 2) {
108
+ if (type === 0) processText(node, state, output);
109
+ return;
110
+ }
111
+ if (node.type !== 1) return;
112
+ const element = node;
113
+ const tagId = resolveTagId(element);
114
+ const frame = state.frames.at(-1);
115
+ if (frame?._tag === 1) {
116
+ if (type === 0 && tagId === 23 && !frame.language) frame.language = getLanguageFromClass(element.attributes.class);
117
+ else if (type === 0 && tagId === 6) frame.output += "\n";
118
+ else if (type !== 0 && tagId === 34 && element.depth === frame.depth) closeFrame(state, output, frame);
119
+ return;
120
+ }
121
+ if (type === 0 && tagId !== void 0 && tagId >= 7 && tagId <= 12) {
122
+ state.frames.push({
123
+ _tag: 0,
124
+ level: tagId - 7 + 1,
125
+ output: "",
126
+ text: ""
127
+ });
128
+ return;
129
+ }
130
+ if (type !== 0 && frame?._tag === 0 && tagId !== void 0 && tagId >= 7 && tagId <= 12) {
131
+ closeFrame(state, output, frame);
132
+ return;
133
+ }
134
+ if (type === 0 && tagId === 34) {
135
+ state.frames.push({
136
+ _tag: 1,
137
+ depth: element.depth,
138
+ language: getLanguageFromClass(element.attributes.class),
139
+ output: ""
140
+ });
141
+ return;
142
+ }
143
+ if (tagId === 6) {
144
+ if (type === 0) append(state, output, "<br>");
145
+ return;
146
+ }
147
+ if (tagId === 13) {
148
+ if (type === 0) append(state, output, "<hr>");
149
+ return;
150
+ }
151
+ if (tagId === 27) {
152
+ if (type === 0) {
153
+ const src = element.attributes.src && resolveSafeUrl(element.attributes.src, options, true);
154
+ if (src) {
155
+ append(state, output, `<img src="${escapeHtml(src, true)}" alt="${escapeHtml(element.attributes.alt || "", true)}"${element.attributes.title === void 0 ? "" : ` title="${escapeHtml(element.attributes.title, true)}"`}>`);
156
+ markRenderedChildContent(element);
157
+ }
158
+ }
159
+ return;
160
+ }
161
+ if (tagId === 26) {
162
+ const rendered = safeAnchorOutput(element, options, type === 0);
163
+ if (rendered) append(state, output, rendered);
164
+ return;
165
+ }
166
+ if (tagId === void 0) return;
167
+ const name = tagName(element, tagId);
168
+ if (!name) return;
169
+ const attributes = safeAttributes(element, tagId);
170
+ if (attributes === void 0) return;
171
+ append(state, output, type === 0 ? `<${name}${attributes}>` : `</${name}>`);
172
+ }
173
+ const INLINE_TAG_IDS = [
174
+ 26,
175
+ 79,
176
+ 55,
177
+ 15,
178
+ 56,
179
+ 94,
180
+ 6,
181
+ 43,
182
+ 90,
183
+ 23,
184
+ 18,
185
+ 91,
186
+ 16,
187
+ 106,
188
+ 17,
189
+ 27,
190
+ 59,
191
+ 21,
192
+ 46,
193
+ 60,
194
+ 42,
195
+ 54,
196
+ 80,
197
+ 81,
198
+ 97,
199
+ 96,
200
+ 95,
201
+ 110,
202
+ 82,
203
+ 83,
204
+ 37,
205
+ 111,
206
+ 14,
207
+ 19,
208
+ 20,
209
+ 93,
210
+ 89,
211
+ 92,
212
+ 64
213
+ ];
214
+ const htmlTagHandlers = { ...parserTagHandlers };
215
+ for (const tagId of INLINE_TAG_IDS) htmlTagHandlers[tagId] = {
216
+ ...htmlTagHandlers[tagId],
217
+ isInline: true
218
+ };
219
+ function createProcessor(options) {
220
+ const outputState = createHtmlOutputState();
221
+ const state = {
222
+ options,
223
+ outputFormat: "html",
224
+ buffer: [],
225
+ depthMap: /* @__PURE__ */ new Uint16Array(113),
226
+ depth: 0,
227
+ plainText: true
228
+ };
229
+ return {
230
+ state,
231
+ processEvent(event) {
232
+ state.depth = event.node.depth;
233
+ if (event.node.type === 1 ? event.node.excludedFromMarkdown : event.node.parent?.excludedFromMarkdown) return;
234
+ processHtmlOutputEvent(event, outputState, state.buffer, options);
235
+ },
236
+ takeOutput() {
237
+ const output = state.buffer.join("");
238
+ if (output) outputState.hasOutput = true;
239
+ state.buffer.length = 0;
240
+ return output;
241
+ }
242
+ };
243
+ }
244
+ function resolveOutputOptions(options) {
245
+ assertEngineOptions(options);
246
+ checkClean(options);
247
+ return {
248
+ plugins: resolvePlugins(options.plugins),
249
+ tagHandlers: htmlTagHandlers,
250
+ tagOverrideHandlers: options.tagOverrides ? buildTagOverrideHandlers(options.tagOverrides, htmlTagHandlers) : void 0,
251
+ plainText: true
252
+ };
253
+ }
254
+ function htmlToSafeHtml(html, options = {}) {
255
+ return processHtmlOutput(html, createProcessor(options), resolveOutputOptions(options));
256
+ }
257
+ function streamHtmlToSafeHtml(htmlStream, options = {}) {
258
+ return streamHtmlOutput(htmlStream, createProcessor(options), resolveOutputOptions(options));
259
+ }
260
+ export { htmlToSafeHtml, streamHtmlToSafeHtml };