mdream 0.1.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,508 @@
1
+ import { ae as TEXT_NODE, aa as ELEMENT_NODE, a7 as TAG_MAIN, C as TAG_HEADER, B as TAG_FOOTER, I as TAG_H1, H as TAG_H2, G as TAG_H3, F as TAG_H4, E as TAG_H5, D as TAG_H6 } from './mdream.-hdaPj9a.mjs';
2
+ import { c as createPlugin } from './mdream.cpEmpxyh.mjs';
3
+
4
+ class TagSelector {
5
+ constructor(tagName) {
6
+ this.tagName = tagName;
7
+ }
8
+ matches(element) {
9
+ return element.name === this.tagName;
10
+ }
11
+ toString() {
12
+ return this.tagName;
13
+ }
14
+ }
15
+ class IdSelector {
16
+ id;
17
+ constructor(selector) {
18
+ this.id = selector.slice(1);
19
+ }
20
+ matches(element) {
21
+ return element.attributes?.id === this.id;
22
+ }
23
+ toString() {
24
+ return `#${this.id}`;
25
+ }
26
+ }
27
+ class ClassSelector {
28
+ className;
29
+ constructor(selector) {
30
+ this.className = selector.slice(1);
31
+ }
32
+ matches(element) {
33
+ if (!element.attributes?.class)
34
+ return false;
35
+ const classes = element.attributes.class.trim().split(" ").filter(Boolean);
36
+ return classes.includes(this.className);
37
+ }
38
+ toString() {
39
+ return `.${this.className}`;
40
+ }
41
+ }
42
+ class AttributeSelector {
43
+ attrName;
44
+ attrValue;
45
+ operator;
46
+ constructor(selector) {
47
+ const match = selector.match(/\[([^\]=~|^$*]+)(?:([=~|^$*]+)["']?([^"'\]]+)["']?)?\]/);
48
+ if (match) {
49
+ this.attrName = match[1];
50
+ this.operator = match[2];
51
+ this.attrValue = match[3];
52
+ } else {
53
+ this.attrName = selector.slice(1, -1);
54
+ }
55
+ }
56
+ matches(element) {
57
+ if (!(this.attrName in (element.attributes || {}))) {
58
+ return false;
59
+ }
60
+ if (!this.operator || !this.attrValue) {
61
+ return true;
62
+ }
63
+ const value = element.attributes[this.attrName];
64
+ switch (this.operator) {
65
+ case "=":
66
+ return value === this.attrValue;
67
+ case "^=":
68
+ return value.startsWith(this.attrValue);
69
+ case "$=":
70
+ return value.endsWith(this.attrValue);
71
+ case "*=":
72
+ return value.includes(this.attrValue);
73
+ case "~=":
74
+ return value.trim().split(" ").filter(Boolean).includes(this.attrValue);
75
+ case "|=":
76
+ return value === this.attrValue || value.startsWith(`${this.attrValue}-`);
77
+ default:
78
+ return false;
79
+ }
80
+ }
81
+ toString() {
82
+ if (!this.operator || !this.attrValue) {
83
+ return `[${this.attrName}]`;
84
+ }
85
+ return `[${this.attrName}${this.operator}${this.attrValue}]`;
86
+ }
87
+ }
88
+ class CompoundSelector {
89
+ constructor(selectors) {
90
+ this.selectors = selectors;
91
+ }
92
+ matches(element) {
93
+ return this.selectors.every((selector) => selector.matches(element));
94
+ }
95
+ toString() {
96
+ return this.selectors.map((s) => s.toString()).join("");
97
+ }
98
+ }
99
+ function parseSelector(selector) {
100
+ selector = selector.trim();
101
+ if (!selector) {
102
+ throw new Error("Empty selector");
103
+ }
104
+ const selectorParts = [];
105
+ let current = "";
106
+ let inAttribute = false;
107
+ for (let i = 0; i < selector.length; i++) {
108
+ const char = selector[i];
109
+ if ((char === "." || char === "#" || char === "[") && current) {
110
+ if (current[0] === ".") {
111
+ selectorParts.push(new ClassSelector(current));
112
+ } else if (current[0] === "#") {
113
+ selectorParts.push(new IdSelector(current));
114
+ } else if (current[0] === "[") {
115
+ selectorParts.push(new AttributeSelector(current));
116
+ } else {
117
+ selectorParts.push(new TagSelector(current));
118
+ }
119
+ current = char;
120
+ } else {
121
+ current += char;
122
+ }
123
+ if (char === "[")
124
+ inAttribute = true;
125
+ if (char === "]")
126
+ inAttribute = false;
127
+ if (inAttribute && char !== "[") {
128
+ continue;
129
+ }
130
+ }
131
+ if (current) {
132
+ if (current[0] === ".") {
133
+ selectorParts.push(new ClassSelector(current));
134
+ } else if (current[0] === "#") {
135
+ selectorParts.push(new IdSelector(current));
136
+ } else if (current[0] === "[") {
137
+ selectorParts.push(new AttributeSelector(current));
138
+ } else {
139
+ selectorParts.push(new TagSelector(current));
140
+ }
141
+ }
142
+ if (selectorParts.length === 1) {
143
+ return selectorParts[0];
144
+ }
145
+ return new CompoundSelector(selectorParts);
146
+ }
147
+ function filterPlugin(options = {}) {
148
+ const includeSelectors = options.include?.map((selector) => {
149
+ if (typeof selector === "string") {
150
+ return parseSelector(selector);
151
+ }
152
+ return { matches: (element) => element.tagId === selector };
153
+ }) || [];
154
+ const excludeSelectors = options.exclude?.map((selector) => {
155
+ if (typeof selector === "string") {
156
+ return parseSelector(selector);
157
+ }
158
+ return { matches: (element) => element.tagId === selector };
159
+ }) || [];
160
+ const processChildren = options.processChildren !== false;
161
+ return createPlugin({
162
+ // Handle include/exclude filtering for elements and text nodes
163
+ beforeNodeProcess(event) {
164
+ const { node } = event;
165
+ if (node.type === TEXT_NODE) {
166
+ const textNode = node;
167
+ let currentParent2 = textNode.parent;
168
+ while (currentParent2 && excludeSelectors.length) {
169
+ const parentShouldExclude = excludeSelectors.some((selector) => selector.matches(currentParent2));
170
+ if (parentShouldExclude) {
171
+ return { skip: true };
172
+ }
173
+ currentParent2 = currentParent2.parent;
174
+ }
175
+ return;
176
+ }
177
+ if (node.type !== ELEMENT_NODE) {
178
+ return;
179
+ }
180
+ const element = node;
181
+ if (excludeSelectors.length) {
182
+ if (element.attributes.style?.includes("absolute") || element.attributes.style?.includes("fixed")) {
183
+ return { skip: true };
184
+ }
185
+ const shouldExclude = excludeSelectors.some((selector) => selector.matches(element));
186
+ if (shouldExclude) {
187
+ return { skip: true };
188
+ }
189
+ }
190
+ let currentParent = element.parent;
191
+ while (currentParent) {
192
+ if (excludeSelectors.length) {
193
+ const parentShouldExclude = excludeSelectors.some((selector) => selector.matches(currentParent));
194
+ if (parentShouldExclude) {
195
+ return { skip: true };
196
+ }
197
+ }
198
+ currentParent = currentParent.parent;
199
+ }
200
+ if (includeSelectors.length) {
201
+ let currentElement = element;
202
+ while (currentElement) {
203
+ const shouldInclude = includeSelectors.some((selector) => selector.matches(currentElement));
204
+ if (shouldInclude) {
205
+ return;
206
+ }
207
+ if (!processChildren)
208
+ break;
209
+ currentElement = currentElement.parent;
210
+ }
211
+ return { skip: true };
212
+ }
213
+ }
214
+ });
215
+ }
216
+
217
+ function isolateMainPlugin() {
218
+ let mainElement = null;
219
+ let firstHeaderElement = null;
220
+ let afterFooter = false;
221
+ const headerTagIds = /* @__PURE__ */ new Set([TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6]);
222
+ return createPlugin({
223
+ beforeNodeProcess(event) {
224
+ const { node } = event;
225
+ if (node.type === ELEMENT_NODE) {
226
+ const element = node;
227
+ if (!mainElement && element.tagId === TAG_MAIN && element.depth <= 5) {
228
+ mainElement = element;
229
+ return;
230
+ }
231
+ if (mainElement) {
232
+ let current = element.parent;
233
+ let isInsideMain = element === mainElement;
234
+ while (current && !isInsideMain) {
235
+ if (current === mainElement) {
236
+ isInsideMain = true;
237
+ break;
238
+ }
239
+ current = current.parent;
240
+ }
241
+ if (!isInsideMain) {
242
+ return { skip: true };
243
+ }
244
+ return;
245
+ }
246
+ if (!firstHeaderElement && headerTagIds.has(element.tagId)) {
247
+ let current = element.parent;
248
+ let isInHeaderTag = false;
249
+ while (current) {
250
+ if (current.tagId === TAG_HEADER) {
251
+ isInHeaderTag = true;
252
+ break;
253
+ }
254
+ current = current.parent;
255
+ }
256
+ if (!isInHeaderTag) {
257
+ firstHeaderElement = element;
258
+ return;
259
+ }
260
+ }
261
+ if (firstHeaderElement && !afterFooter && element.tagId === TAG_FOOTER) {
262
+ const depthDifference = element.depth - firstHeaderElement.depth;
263
+ if (depthDifference <= 5) {
264
+ afterFooter = true;
265
+ return { skip: true };
266
+ }
267
+ }
268
+ if (!firstHeaderElement) {
269
+ return { skip: true };
270
+ }
271
+ if (afterFooter) {
272
+ return { skip: true };
273
+ }
274
+ }
275
+ if (node.type === TEXT_NODE) {
276
+ if (mainElement) {
277
+ let current = node.parent;
278
+ let isInsideMain = false;
279
+ while (current) {
280
+ if (current === mainElement) {
281
+ isInsideMain = true;
282
+ break;
283
+ }
284
+ current = current.parent;
285
+ }
286
+ if (!isInsideMain) {
287
+ return { skip: true };
288
+ }
289
+ return;
290
+ }
291
+ if (!firstHeaderElement || afterFooter) {
292
+ return { skip: true };
293
+ }
294
+ }
295
+ }
296
+ });
297
+ }
298
+
299
+ const TAILWIND_TO_MARKDOWN_MAP = {
300
+ // Typography
301
+ "font-bold": { prefix: "**", suffix: "**" },
302
+ "font-semibold": { prefix: "**", suffix: "**" },
303
+ "font-black": { prefix: "**", suffix: "**" },
304
+ "font-extrabold": { prefix: "**", suffix: "**" },
305
+ "font-medium": { prefix: "**", suffix: "**" },
306
+ "font-italic": { prefix: "*", suffix: "*" },
307
+ "italic": { prefix: "*", suffix: "*" },
308
+ "line-through": { prefix: "~~", suffix: "~~" },
309
+ // Display properties - hide elements
310
+ "hidden": { hidden: true },
311
+ "invisible": { hidden: true },
312
+ // Position properties - hide positioned elements
313
+ "absolute": { hidden: true },
314
+ "fixed": { hidden: true },
315
+ "sticky": { hidden: true }
316
+ };
317
+ function extractBaseClass(className) {
318
+ const breakpoints = ["sm:", "md:", "lg:", "xl:", "2xl:"];
319
+ for (const bp of breakpoints) {
320
+ if (className.startsWith(bp)) {
321
+ return {
322
+ baseClass: className.substring(bp.length),
323
+ breakpoint: bp
324
+ };
325
+ }
326
+ }
327
+ return {
328
+ baseClass: className,
329
+ breakpoint: ""
330
+ // Base/mobile class (no breakpoint)
331
+ };
332
+ }
333
+ function sortByBreakpoint(classes) {
334
+ const breakpointOrder = {
335
+ "": 0,
336
+ // Base/mobile (no breakpoint)
337
+ "sm:": 1,
338
+ "md:": 2,
339
+ "lg:": 3,
340
+ "xl:": 4,
341
+ "2xl:": 5
342
+ };
343
+ return [...classes].sort((a, b) => {
344
+ const aBreakpoint = extractBaseClass(a).breakpoint;
345
+ const bBreakpoint = extractBaseClass(b).breakpoint;
346
+ return breakpointOrder[aBreakpoint] - breakpointOrder[bBreakpoint];
347
+ });
348
+ }
349
+ function groupByFormattingType(classes) {
350
+ const sorted = sortByBreakpoint(classes);
351
+ const groups = {
352
+ emphasis: [],
353
+ // italic, etc.
354
+ weight: [],
355
+ // bold, etc.
356
+ decoration: [],
357
+ // strikethrough, etc.
358
+ display: [],
359
+ // hidden, etc.
360
+ position: [],
361
+ // absolute, fixed, etc.
362
+ other: []
363
+ };
364
+ for (const cls of sorted) {
365
+ const { baseClass } = extractBaseClass(cls);
366
+ if (baseClass.includes("italic")) {
367
+ groups.emphasis.push(cls);
368
+ } else if (baseClass.includes("font-") || baseClass === "bold") {
369
+ groups.weight.push(cls);
370
+ } else if (baseClass.includes("line-through") || baseClass.includes("underline")) {
371
+ groups.decoration.push(cls);
372
+ } else if (baseClass === "hidden" || baseClass.includes("invisible")) {
373
+ groups.display.push(cls);
374
+ } else if (["absolute", "fixed", "sticky"].includes(baseClass)) {
375
+ groups.position.push(cls);
376
+ } else {
377
+ groups.other.push(cls);
378
+ }
379
+ }
380
+ return groups;
381
+ }
382
+ function fixRedundantDelimiters(content) {
383
+ content = content.replaceAll("****", "**");
384
+ content = content.replaceAll("~~~~", "~~");
385
+ if (content.includes("***") && content.split("***").length > 3) {
386
+ const parts = content.split("***");
387
+ if (parts.length >= 4) {
388
+ content = `${parts[0]}***${parts[1]} ${parts[2]}***${parts.slice(3).join("***")}`;
389
+ }
390
+ }
391
+ return content;
392
+ }
393
+ function normalizeClasses(classes) {
394
+ const result = [];
395
+ const mobileClasses = classes.filter((cls) => !hasBreakpoint(cls));
396
+ const breakpointClasses = classes.filter((cls) => hasBreakpoint(cls));
397
+ result.push(...mobileClasses);
398
+ result.push(...breakpointClasses);
399
+ return result;
400
+ }
401
+ function hasBreakpoint(className) {
402
+ const { breakpoint } = extractBaseClass(className);
403
+ return breakpoint !== "";
404
+ }
405
+ function processTailwindClasses(classes) {
406
+ let prefix = "";
407
+ let suffix = "";
408
+ let hidden = false;
409
+ const normalizedClasses = normalizeClasses(classes);
410
+ const grouped = groupByFormattingType(normalizedClasses);
411
+ if (grouped.weight.length > 0) {
412
+ const { baseClass } = extractBaseClass(grouped.weight[0]);
413
+ const mapping = TAILWIND_TO_MARKDOWN_MAP[baseClass];
414
+ if (mapping) {
415
+ if (mapping.prefix)
416
+ prefix += mapping.prefix;
417
+ if (mapping.suffix)
418
+ suffix = mapping.suffix + suffix;
419
+ }
420
+ }
421
+ if (grouped.emphasis.length > 0) {
422
+ const { baseClass } = extractBaseClass(grouped.emphasis[0]);
423
+ const mapping = TAILWIND_TO_MARKDOWN_MAP[baseClass];
424
+ if (mapping) {
425
+ if (mapping.prefix)
426
+ prefix += mapping.prefix;
427
+ if (mapping.suffix)
428
+ suffix = mapping.suffix + suffix;
429
+ }
430
+ }
431
+ if (grouped.decoration.length > 0) {
432
+ const { baseClass } = extractBaseClass(grouped.decoration[0]);
433
+ const mapping = TAILWIND_TO_MARKDOWN_MAP[baseClass];
434
+ if (mapping) {
435
+ if (mapping.prefix)
436
+ prefix += mapping.prefix;
437
+ if (mapping.suffix)
438
+ suffix = mapping.suffix + suffix;
439
+ }
440
+ }
441
+ for (const cls of grouped.display) {
442
+ const { baseClass } = extractBaseClass(cls);
443
+ const mapping = TAILWIND_TO_MARKDOWN_MAP[baseClass];
444
+ if (mapping && mapping.hidden) {
445
+ hidden = true;
446
+ break;
447
+ }
448
+ }
449
+ for (const cls of grouped.position) {
450
+ const { baseClass } = extractBaseClass(cls);
451
+ const mapping = TAILWIND_TO_MARKDOWN_MAP[baseClass];
452
+ if (mapping && mapping.hidden) {
453
+ hidden = true;
454
+ break;
455
+ }
456
+ }
457
+ return { prefix, suffix, hidden };
458
+ }
459
+ function tailwindPlugin() {
460
+ return createPlugin({
461
+ // Process node attributes to extract Tailwind classes
462
+ processAttributes(node) {
463
+ const classAttr = node.attributes?.class;
464
+ if (!classAttr) {
465
+ return;
466
+ }
467
+ const classes = classAttr.trim().split(" ").filter(Boolean);
468
+ const { prefix, suffix, hidden } = processTailwindClasses(classes);
469
+ node.context = node.context || {};
470
+ node.context.tailwind = {
471
+ prefix,
472
+ suffix,
473
+ hidden
474
+ };
475
+ },
476
+ // Process text nodes to apply Tailwind formatting
477
+ processTextNode(node) {
478
+ const parentNode = node.parent;
479
+ if (!parentNode || parentNode.type !== ELEMENT_NODE) {
480
+ return void 0;
481
+ }
482
+ const tailwindData = parentNode.context?.tailwind;
483
+ if (tailwindData?.hidden) {
484
+ return { content: "", skip: true };
485
+ }
486
+ let content = node.value;
487
+ const prefix = tailwindData?.prefix || "";
488
+ const suffix = tailwindData?.suffix || "";
489
+ if (prefix || suffix) {
490
+ content = prefix + content + suffix;
491
+ content = fixRedundantDelimiters(content);
492
+ }
493
+ return { content, skip: false };
494
+ },
495
+ // Filter out hidden elements
496
+ beforeNodeProcess({ node }) {
497
+ if (node.type === ELEMENT_NODE) {
498
+ const elementNode = node;
499
+ const tailwindData = elementNode.context?.tailwind;
500
+ if (tailwindData?.hidden) {
501
+ return { skip: true };
502
+ }
503
+ }
504
+ }
505
+ });
506
+ }
507
+
508
+ export { filterPlugin as f, isolateMainPlugin as i, tailwindPlugin as t };