@lithent/lithent-mdx 0.2.2 → 0.2.4
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/README.md +2 -2
- package/dist/index.mjs +262 -84
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +12 -8
- package/dist/index.umd.js.map +1 -1
- package/package.json +3 -3
- package/src/mdx.example.mdx +4 -0
- package/src/tests/transform.test.ts +21 -0
- package/src/transform.ts +311 -11
package/README.md
CHANGED
|
@@ -15,9 +15,9 @@ pnpm add -D @lithent/lithent-mdx @mdx-js/rollup
|
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
Required peer dependencies:
|
|
18
|
-
- `lithent` `^1.20.
|
|
18
|
+
- `lithent` `^1.20.2`
|
|
19
19
|
- `vite` `^5.0.0`
|
|
20
|
-
- `@lithent/lithent-vite` `^0.2.
|
|
20
|
+
- `@lithent/lithent-vite` `^0.2.3` for end-to-end HMR support
|
|
21
21
|
|
|
22
22
|
## Usage
|
|
23
23
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
import
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
if (
|
|
5
|
-
return { code:
|
|
6
|
-
const t =
|
|
1
|
+
import x from "@mdx-js/rollup";
|
|
2
|
+
const l = "/* __lithentMdxWrapped */", h = (n) => {
|
|
3
|
+
const r = g(n), e = r !== n;
|
|
4
|
+
if (r.includes(l))
|
|
5
|
+
return { code: r, modified: e };
|
|
6
|
+
const t = b(r);
|
|
7
7
|
if (t < 0)
|
|
8
|
-
return { code:
|
|
9
|
-
const
|
|
10
|
-
let
|
|
11
|
-
|
|
8
|
+
return { code: r, modified: e };
|
|
9
|
+
const o = "MDXContent", i = t, c = i + 34;
|
|
10
|
+
let s = `${r.slice(0, i)}function ${o}${r.slice(c)}`;
|
|
11
|
+
w(s, "lithent", "mount") || (s = $(s, `import { mount } from 'lithent';
|
|
12
12
|
`));
|
|
13
|
-
const
|
|
14
|
-
${
|
|
15
|
-
const ${
|
|
16
|
-
return () => ${
|
|
13
|
+
const f = `__LithentMdxComponent_${o}`, m = `
|
|
14
|
+
${l}
|
|
15
|
+
const ${f} = mount((_renew, props) => {
|
|
16
|
+
return () => ${o}(props ?? {});
|
|
17
17
|
});
|
|
18
|
-
export default ${
|
|
18
|
+
export default ${f};
|
|
19
19
|
`;
|
|
20
|
-
return
|
|
21
|
-
},
|
|
22
|
-
const
|
|
23
|
-
let t = 0,
|
|
24
|
-
for (; t <
|
|
25
|
-
const
|
|
26
|
-
switch (
|
|
20
|
+
return s = `${s}${m}`, { code: s, modified: !0 };
|
|
21
|
+
}, b = (n) => {
|
|
22
|
+
const r = "export default function MDXContent", e = n.length;
|
|
23
|
+
let t = 0, o = "code", i = 0;
|
|
24
|
+
for (; t < e; ) {
|
|
25
|
+
const c = n[t], s = n[t + 1];
|
|
26
|
+
switch (o) {
|
|
27
27
|
case "code":
|
|
28
|
-
if (
|
|
29
|
-
if (
|
|
30
|
-
|
|
28
|
+
if (c === "/") {
|
|
29
|
+
if (s === "/") {
|
|
30
|
+
o = "lineComment", t += 2;
|
|
31
31
|
continue;
|
|
32
32
|
}
|
|
33
|
-
if (
|
|
34
|
-
|
|
33
|
+
if (s === "*") {
|
|
34
|
+
o = "blockComment", t += 2;
|
|
35
35
|
continue;
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
if (
|
|
39
|
-
|
|
38
|
+
if (c === "'") {
|
|
39
|
+
o = "single", t++;
|
|
40
40
|
continue;
|
|
41
41
|
}
|
|
42
|
-
if (
|
|
43
|
-
|
|
42
|
+
if (c === '"') {
|
|
43
|
+
o = "double", t++;
|
|
44
44
|
continue;
|
|
45
45
|
}
|
|
46
|
-
if (
|
|
47
|
-
|
|
46
|
+
if (c === "`") {
|
|
47
|
+
o = "template", i = 0, t++;
|
|
48
48
|
continue;
|
|
49
49
|
}
|
|
50
|
-
if (
|
|
50
|
+
if (n.startsWith(r, t))
|
|
51
51
|
return t;
|
|
52
52
|
t++;
|
|
53
53
|
break;
|
|
54
54
|
case "single":
|
|
55
|
-
if (
|
|
55
|
+
if (c === "\\") {
|
|
56
56
|
t += 2;
|
|
57
57
|
continue;
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
c === "'" && (o = "code"), t++;
|
|
60
60
|
break;
|
|
61
61
|
case "double":
|
|
62
|
-
if (
|
|
62
|
+
if (c === "\\") {
|
|
63
63
|
t += 2;
|
|
64
64
|
continue;
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
c === '"' && (o = "code"), t++;
|
|
67
67
|
break;
|
|
68
68
|
case "template":
|
|
69
|
-
if (
|
|
69
|
+
if (c === "\\") {
|
|
70
70
|
t += 2;
|
|
71
71
|
continue;
|
|
72
72
|
}
|
|
73
|
-
if (
|
|
74
|
-
if (
|
|
75
|
-
|
|
73
|
+
if (c === "`") {
|
|
74
|
+
if (i === 0) {
|
|
75
|
+
o = "code", t++;
|
|
76
76
|
break;
|
|
77
77
|
}
|
|
78
|
-
|
|
78
|
+
i--, t++;
|
|
79
79
|
break;
|
|
80
80
|
}
|
|
81
|
-
if (
|
|
82
|
-
|
|
81
|
+
if (c === "$" && s === "{") {
|
|
82
|
+
i++, t += 2;
|
|
83
83
|
break;
|
|
84
84
|
}
|
|
85
|
-
if (
|
|
86
|
-
|
|
85
|
+
if (c === "}") {
|
|
86
|
+
i > 0 && i--, t++;
|
|
87
87
|
break;
|
|
88
88
|
}
|
|
89
89
|
t++;
|
|
90
90
|
break;
|
|
91
91
|
case "lineComment":
|
|
92
|
-
|
|
93
|
-
` && (
|
|
92
|
+
c === `
|
|
93
|
+
` && (o = "code"), t++;
|
|
94
94
|
break;
|
|
95
95
|
case "blockComment":
|
|
96
|
-
if (
|
|
97
|
-
|
|
96
|
+
if (c === "*" && s === "/") {
|
|
97
|
+
o = "code", t += 2;
|
|
98
98
|
break;
|
|
99
99
|
}
|
|
100
100
|
t++;
|
|
@@ -102,50 +102,228 @@ export default ${u};
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
return -1;
|
|
105
|
-
},
|
|
106
|
-
const
|
|
107
|
-
new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${
|
|
108
|
-
(
|
|
109
|
-
),
|
|
110
|
-
new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${
|
|
111
|
-
(
|
|
112
|
-
), t =
|
|
113
|
-
return
|
|
114
|
-
},
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return `${i.slice(0, r)}${s}${i.slice(r)}`;
|
|
105
|
+
}, g = (n) => {
|
|
106
|
+
const r = (i, c) => i.replace(
|
|
107
|
+
new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${c}`, "gm"),
|
|
108
|
+
(s, a) => s.trimStart().startsWith("export ") ? s : `${a}export function ${c}`
|
|
109
|
+
), e = (i, c) => i.replace(
|
|
110
|
+
new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${c}`, "gm"),
|
|
111
|
+
(s, a) => s.trimStart().startsWith("export default") ? s : `${a}export default function ${c}`
|
|
112
|
+
), t = r(n, "_createMdxContent");
|
|
113
|
+
return e(t, "MDXContent");
|
|
114
|
+
}, $ = (n, r) => {
|
|
115
|
+
const e = k(n);
|
|
116
|
+
if (e >= 0) {
|
|
117
|
+
const o = e > 0 && n[e - 1] !== `
|
|
118
|
+
` ? `
|
|
119
|
+
${r}` : r;
|
|
120
|
+
return `${n.slice(0, e)}${o}${n.slice(e)}`;
|
|
122
121
|
}
|
|
123
|
-
return `${
|
|
124
|
-
},
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
122
|
+
return `${r}${n}`;
|
|
123
|
+
}, p = (n) => {
|
|
124
|
+
const r = [];
|
|
125
|
+
let e = 0, t = "code", o = 0;
|
|
126
|
+
for (; e < n.length; ) {
|
|
127
|
+
const i = n[e], c = n[e + 1];
|
|
128
|
+
switch (t) {
|
|
129
|
+
case "code": {
|
|
130
|
+
if (i === "/" && c === "/") {
|
|
131
|
+
t = "lineComment", e += 2;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (i === "/" && c === "*") {
|
|
135
|
+
t = "blockComment", e += 2;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (i === "'") {
|
|
139
|
+
t = "single", e++;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (i === '"') {
|
|
143
|
+
t = "double", e++;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (i === "`") {
|
|
147
|
+
t = "template", o = 0, e++;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (e === 0 && i === "#" && c === "!") {
|
|
151
|
+
t = "lineComment", e += 2;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (i === "i" && C(n, e) && !I(n, e)) {
|
|
155
|
+
const s = M(n, e);
|
|
156
|
+
r.push({
|
|
157
|
+
start: e,
|
|
158
|
+
end: s,
|
|
159
|
+
code: n.slice(e, s)
|
|
160
|
+
}), e = s;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
e++;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
case "single":
|
|
167
|
+
if (i === "\\") {
|
|
168
|
+
e += 2;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
i === "'" && (t = "code"), e++;
|
|
172
|
+
break;
|
|
173
|
+
case "double":
|
|
174
|
+
if (i === "\\") {
|
|
175
|
+
e += 2;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
i === '"' && (t = "code"), e++;
|
|
179
|
+
break;
|
|
180
|
+
case "template":
|
|
181
|
+
if (i === "\\") {
|
|
182
|
+
e += 2;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (i === "`") {
|
|
186
|
+
if (o === 0) {
|
|
187
|
+
t = "code", e++;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
o--, e++;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (i === "$" && c === "{") {
|
|
194
|
+
o++, e += 2;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
i === "}" && o > 0 && o--, e++;
|
|
198
|
+
break;
|
|
199
|
+
case "lineComment":
|
|
200
|
+
i === `
|
|
201
|
+
` && (t = "code"), e++;
|
|
202
|
+
break;
|
|
203
|
+
case "blockComment":
|
|
204
|
+
if (i === "*" && c === "/") {
|
|
205
|
+
t = "code", e += 2;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
e++;
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return r;
|
|
213
|
+
}, k = (n) => {
|
|
214
|
+
const r = p(n);
|
|
215
|
+
return r.length ? r[r.length - 1].end : -1;
|
|
216
|
+
}, w = (n, r, e) => {
|
|
217
|
+
const t = p(n), o = new RegExp(
|
|
218
|
+
`from\\s+['"]${u(r)}['"]`
|
|
219
|
+
), i = new RegExp(`\\b${u(e)}\\b`);
|
|
220
|
+
return t.some((c) => {
|
|
221
|
+
if (!o.test(c.code))
|
|
222
|
+
return !1;
|
|
223
|
+
const [s] = c.code.split(/from\s+/i);
|
|
224
|
+
return i.test(s);
|
|
225
|
+
});
|
|
226
|
+
}, u = (n) => n.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), C = (n, r) => {
|
|
227
|
+
if (!n.startsWith("import", r))
|
|
228
|
+
return !1;
|
|
229
|
+
const e = n[r - 1];
|
|
230
|
+
if (e && d(e))
|
|
231
|
+
return !1;
|
|
232
|
+
const t = n[r + 6];
|
|
233
|
+
return !(t && d(t));
|
|
234
|
+
}, d = (n) => /[A-Za-z0-9_$]/.test(n), I = (n, r) => {
|
|
235
|
+
const e = n[r + 6];
|
|
236
|
+
return !e || e === "(" || e === ".";
|
|
237
|
+
}, M = (n, r) => {
|
|
238
|
+
let e = r, t = "code";
|
|
239
|
+
for (; e < n.length; ) {
|
|
240
|
+
const o = n[e], i = n[e + 1];
|
|
241
|
+
switch (t) {
|
|
242
|
+
case "code":
|
|
243
|
+
if (o === ";")
|
|
244
|
+
return e + 1;
|
|
245
|
+
if (o === "/" && i === "/") {
|
|
246
|
+
t = "lineComment", e += 2;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (o === "/" && i === "*") {
|
|
250
|
+
t = "blockComment", e += 2;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (o === "'") {
|
|
254
|
+
t = "single", e++;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (o === '"') {
|
|
258
|
+
t = "double", e++;
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (o === "`") {
|
|
262
|
+
t = "template", e++;
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
e++;
|
|
266
|
+
break;
|
|
267
|
+
case "single":
|
|
268
|
+
if (o === "\\") {
|
|
269
|
+
e += 2;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
o === "'" && (t = "code"), e++;
|
|
273
|
+
break;
|
|
274
|
+
case "double":
|
|
275
|
+
if (o === "\\") {
|
|
276
|
+
e += 2;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
o === '"' && (t = "code"), e++;
|
|
280
|
+
break;
|
|
281
|
+
case "template":
|
|
282
|
+
if (o === "\\") {
|
|
283
|
+
e += 2;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
o === "`" && (t = "code"), e++;
|
|
287
|
+
break;
|
|
288
|
+
case "lineComment":
|
|
289
|
+
o === `
|
|
290
|
+
` && (t = "code"), e++;
|
|
291
|
+
break;
|
|
292
|
+
case "blockComment":
|
|
293
|
+
if (o === "*" && i === "/") {
|
|
294
|
+
t = "code", e += 2;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
e++;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return n.length;
|
|
302
|
+
}, D = (n = {}) => {
|
|
303
|
+
const r = n.jsxImportSource ?? "lithent";
|
|
304
|
+
return [x({
|
|
305
|
+
jsxImportSource: r,
|
|
306
|
+
...n.mdxOptions
|
|
129
307
|
}), {
|
|
130
308
|
name: "lithent:mdx-wrapper",
|
|
131
309
|
enforce: void 0,
|
|
132
310
|
// normal - runs after mdxPlugin in array order
|
|
133
|
-
transform(
|
|
134
|
-
const [
|
|
135
|
-
if (!/\.mdx$/i.test(
|
|
311
|
+
transform(o, i) {
|
|
312
|
+
const [c] = i.split("?", 1);
|
|
313
|
+
if (!/\.mdx$/i.test(c))
|
|
136
314
|
return null;
|
|
137
|
-
const
|
|
138
|
-
return
|
|
139
|
-
code:
|
|
315
|
+
const s = h(o);
|
|
316
|
+
return s.modified ? {
|
|
317
|
+
code: s.code,
|
|
140
318
|
map: null
|
|
141
319
|
} : null;
|
|
142
320
|
}
|
|
143
321
|
}];
|
|
144
322
|
};
|
|
145
323
|
export {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
324
|
+
l as MDX_WRAP_SENTINEL,
|
|
325
|
+
D as default,
|
|
326
|
+
D as lithentMdx,
|
|
327
|
+
h as wrapMdxModule
|
|
150
328
|
};
|
|
151
329
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/transform.ts","../src/plugin.ts"],"sourcesContent":["export interface MdxWrapResult {\n code: string;\n modified: boolean;\n}\n\nexport const MDX_WRAP_SENTINEL = '/* __lithentMdxWrapped */';\n\nexport const wrapMdxModule = (code: string): MdxWrapResult => {\n const fixedExports = wrapMdxExports(code);\n const exportsModified = fixedExports !== code;\n\n if (fixedExports.includes(MDX_WRAP_SENTINEL)) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const defaultExportIndex = findDefaultMdxExportIndex(fixedExports);\n\n if (defaultExportIndex < 0) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const fnName = 'MDXContent';\n\n const start = defaultExportIndex;\n const end = start + 'export default function MDXContent'.length;\n let updated = `${fixedExports.slice(0, start)}function ${fnName}${fixedExports.slice(end)}`;\n\n // Ensure mount import exists\n const hasMountImport =\n /import\\s+{[^}]*\\bmount\\b[^}]*}\\s+from\\s+['\"]lithent['\"]/.test(updated);\n\n if (!hasMountImport) {\n updated = insertImportString(updated, `import { mount } from 'lithent';\\n`);\n }\n\n const wrapperVar = `__LithentMdxComponent_${fnName}`;\n const wrapperSnippet = `\n${MDX_WRAP_SENTINEL}\nconst ${wrapperVar} = mount((_renew, props) => {\n return () => ${fnName}(props ?? {});\n});\nexport default ${wrapperVar};\n`;\n\n updated = `${updated}${wrapperSnippet}`;\n\n return { code: updated, modified: true };\n};\n\nconst findDefaultMdxExportIndex = (source: string): number => {\n const target = 'export default function MDXContent';\n const length = source.length;\n let i = 0;\n let state:\n | 'code'\n | 'single'\n | 'double'\n | 'template'\n | 'lineComment'\n | 'blockComment' = 'code';\n let templateDepth = 0;\n\n while (i < length) {\n const ch = source[i];\n const next = source[i + 1];\n\n switch (state) {\n case 'code':\n if (ch === '/') {\n if (next === '/') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (next === '*') {\n state = 'blockComment';\n i += 2;\n continue;\n }\n }\n if (ch === \"'\") {\n state = 'single';\n i++;\n continue;\n }\n if (ch === '\"') {\n state = 'double';\n i++;\n continue;\n }\n if (ch === '`') {\n state = 'template';\n templateDepth = 0;\n i++;\n continue;\n }\n if (source.startsWith(target, i)) {\n return i;\n }\n i++;\n break;\n\n case 'single':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'code';\n }\n i++;\n break;\n\n case 'double':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '\"') {\n state = 'code';\n }\n i++;\n break;\n\n case 'template':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '`') {\n if (templateDepth === 0) {\n state = 'code';\n i++;\n break;\n }\n templateDepth--;\n i++;\n break;\n }\n if (ch === '$' && next === '{') {\n templateDepth++;\n i += 2;\n break;\n }\n if (ch === '}') {\n if (templateDepth > 0) {\n templateDepth--;\n }\n i++;\n break;\n }\n i++;\n break;\n\n case 'lineComment':\n if (ch === '\\n') {\n state = 'code';\n }\n i++;\n break;\n\n case 'blockComment':\n if (ch === '*' && next === '/') {\n state = 'code';\n i += 2;\n break;\n }\n i++;\n break;\n }\n }\n\n return -1;\n};\n\nconst wrapMdxExports = (code: string): string => {\n const ensureNamedExport = (source: string, fnName: string): string =>\n source.replace(\n new RegExp(`^(\\\\s*)(?:export\\\\s+)?function\\\\s+${fnName}`, 'gm'),\n (match, leading) => {\n if (match.trimStart().startsWith('export ')) {\n return match;\n }\n return `${leading}export function ${fnName}`;\n }\n );\n\n const ensureDefaultExport = (source: string, fnName: string): string =>\n source.replace(\n new RegExp(`^(\\\\s*)(?:export\\\\s+)?function\\\\s+${fnName}`, 'gm'),\n (match, leading) => {\n if (match.trimStart().startsWith('export default')) {\n return match;\n }\n return `${leading}export default function ${fnName}`;\n }\n );\n\n const withNamed = ensureNamedExport(code, '_createMdxContent');\n const withDefault = ensureDefaultExport(withNamed, 'MDXContent');\n return withDefault;\n};\n\nconst insertImportString = (source: string, statement: string): string => {\n const importRegex = /^(import[\\s\\S]*?from\\s+['\"][^'\"]+['\"]\\s*;[ \\t]*\\n?)/gm;\n let lastMatch: RegExpExecArray | null = null;\n let match: RegExpExecArray | null;\n\n while ((match = importRegex.exec(source))) {\n lastMatch = match;\n }\n\n if (lastMatch) {\n const insertPos = lastMatch.index + lastMatch[0].length;\n return `${source.slice(0, insertPos)}${statement}${source.slice(insertPos)}`;\n }\n\n return `${statement}${source}`;\n};\n","import type { Plugin, PluginOption } from 'vite';\nimport mdxRollup from '@mdx-js/rollup';\nimport { wrapMdxModule } from './transform';\n\nexport interface LithentMdxPluginOptions {\n /**\n * MDX plugin options to pass to @mdx-js/rollup\n */\n mdxOptions?: Record<string, unknown>;\n\n /**\n * JSX import source for MDX files\n * @default 'lithent'\n */\n jsxImportSource?: string;\n}\n\nexport const lithentMdx = (\n options: LithentMdxPluginOptions = {}\n): PluginOption => {\n const jsxImportSource = options.jsxImportSource ?? 'lithent';\n\n // Create the MDX rollup plugin with Lithent-specific JSX settings\n const mdxPlugin = mdxRollup({\n jsxImportSource,\n ...options.mdxOptions,\n });\n\n // Create the wrapper plugin that adds mount() wrapping\n const wrapperPlugin: Plugin = {\n name: 'lithent:mdx-wrapper',\n enforce: undefined, // normal - runs after mdxPlugin in array order\n\n transform(code, id) {\n const [filepath] = id.split('?', 1);\n\n // Only process .mdx files\n if (!/\\.mdx$/i.test(filepath)) {\n return null;\n }\n\n // Wrap MDXContent with mount() and add export default\n const result = wrapMdxModule(code);\n\n if (!result.modified) {\n return null;\n }\n\n return {\n code: result.code,\n map: null,\n };\n },\n };\n\n // Return both plugins as an array\n // Order matters: mdxPlugin first (MDX → JSX), then wrapperPlugin (add mount())\n return [mdxPlugin, wrapperPlugin];\n};\n\nexport default lithentMdx;\n"],"names":["MDX_WRAP_SENTINEL","wrapMdxModule","code","fixedExports","wrapMdxExports","exportsModified","defaultExportIndex","findDefaultMdxExportIndex","fnName","start","end","updated","insertImportString","wrapperVar","wrapperSnippet","source","target","length","i","state","templateDepth","ch","next","ensureNamedExport","match","leading","ensureDefaultExport","withNamed","statement","importRegex","lastMatch","insertPos","lithentMdx","options","jsxImportSource","mdxRollup","id","filepath","result"],"mappings":";AAKO,MAAMA,IAAoB,6BAEpBC,IAAgB,CAACC,MAAgC;AACtD,QAAAC,IAAeC,EAAeF,CAAI,GAClCG,IAAkBF,MAAiBD;AAErC,MAAAC,EAAa,SAASH,CAAiB;AACzC,WAAO,EAAE,MAAMG,GAAc,UAAUE,EAAgB;AAGnD,QAAAC,IAAqBC,E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Y,WAAW,gBAAgB,IACxCA,IAEF,GAAGC,CAAO,2BAA2BjB,CAAM;AAAA,EACpD,GAGEmB,IAAYJ,EAAkBrB,GAAM,mBAAmB;AAEtD,SADawB,EAAoBC,GAAW,YAAY;AAEjE,GAEMf,IAAqB,CAACG,GAAgBa,MAA8B;AACxE,QAAMC,IAAc;AACpB,MAAIC,IAAoC,MACpCN;AAEJ,SAAQA,IAAQK,EAAY,KAAKd,CAAM;AACzB,IAAAe,IAAAN;AAGd,MAAIM,GAAW;AACb,UAAMC,IAAYD,EAAU,QAAQA,EAAU,CAAC,EAAE;AACjD,WAAO,GAAGf,EAAO,MAAM,GAAGgB,CAAS,CAAC,GAAGH,CAAS,GAAGb,EAAO,MAAMgB,CAAS,CAAC;AAAA,EAC5E;AAEO,SAAA,GAAGH,CAAS,GAAGb,CAAM;AAC9B,GCzMaiB,IAAa,CACxBC,IAAmC,OAClB;AACX,QAAAC,IAAkBD,EAAQ,mBAAmB;AAqC5C,SAAA,CAlCWE,EAAU;AAAA,IAC1B,iBAAAD;AAAA,IACA,GAAGD,EAAQ;AAAA,EAAA,CACZ,GAG6B;AAAA,IAC5B,MAAM;AAAA,IACN,SAAS;AAAA;AAAA,IAET,UAAU/B,GAAMkC,GAAI;AAClB,YAAM,CAACC,CAAQ,IAAID,EAAG,MAAM,KAAK,CAAC;AAGlC,UAAI,CAAC,UAAU,KAAKC,CAAQ;AACnB,eAAA;AAIH,YAAAC,IAASrC,EAAcC,CAAI;AAE7B,aAACoC,EAAO,WAIL;AAAA,QACL,MAAMA,EAAO;AAAA,QACb,KAAK;AAAA,MAAA,IALE;AAAA,IAOX;AAAA,EAAA,CAK8B;AAClC;"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/transform.ts","../src/plugin.ts"],"sourcesContent":["export interface MdxWrapResult {\n code: string;\n modified: boolean;\n}\n\nexport const MDX_WRAP_SENTINEL = '/* __lithentMdxWrapped */';\n\nexport const wrapMdxModule = (code: string): MdxWrapResult => {\n const fixedExports = wrapMdxExports(code);\n const exportsModified = fixedExports !== code;\n\n if (fixedExports.includes(MDX_WRAP_SENTINEL)) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const defaultExportIndex = findDefaultMdxExportIndex(fixedExports);\n\n if (defaultExportIndex < 0) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const fnName = 'MDXContent';\n\n const start = defaultExportIndex;\n const end = start + 'export default function MDXContent'.length;\n let updated = `${fixedExports.slice(0, start)}function ${fnName}${fixedExports.slice(end)}`;\n\n // Ensure mount import exists\n const hasMountImport = hasNamedImportFrom(updated, 'lithent', 'mount');\n\n if (!hasMountImport) {\n updated = insertImportString(updated, `import { mount } from 'lithent';\\n`);\n }\n\n const wrapperVar = `__LithentMdxComponent_${fnName}`;\n const wrapperSnippet = `\n${MDX_WRAP_SENTINEL}\nconst ${wrapperVar} = mount((_renew, props) => {\n return () => ${fnName}(props ?? {});\n});\nexport default ${wrapperVar};\n`;\n\n updated = `${updated}${wrapperSnippet}`;\n\n return { code: updated, modified: true };\n};\n\nconst findDefaultMdxExportIndex = (source: string): number => {\n const target = 'export default function MDXContent';\n const length = source.length;\n let i = 0;\n let state:\n | 'code'\n | 'single'\n | 'double'\n | 'template'\n | 'lineComment'\n | 'blockComment' = 'code';\n let templateDepth = 0;\n\n while (i < length) {\n const ch = source[i];\n const next = source[i + 1];\n\n switch (state) {\n case 'code':\n if (ch === '/') {\n if (next === '/') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (next === '*') {\n state = 'blockComment';\n i += 2;\n continue;\n }\n }\n if (ch === \"'\") {\n state = 'single';\n i++;\n continue;\n }\n if (ch === '\"') {\n state = 'double';\n i++;\n continue;\n }\n if (ch === '`') {\n state = 'template';\n templateDepth = 0;\n i++;\n continue;\n }\n if (source.startsWith(target, i)) {\n return i;\n }\n i++;\n break;\n\n case 'single':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'code';\n }\n i++;\n break;\n\n case 'double':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '\"') {\n state = 'code';\n }\n i++;\n break;\n\n case 'template':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '`') {\n if (templateDepth === 0) {\n state = 'code';\n i++;\n break;\n }\n templateDepth--;\n i++;\n break;\n }\n if (ch === '$' && next === '{') {\n templateDepth++;\n i += 2;\n break;\n }\n if (ch === '}') {\n if (templateDepth > 0) {\n templateDepth--;\n }\n i++;\n break;\n }\n i++;\n break;\n\n case 'lineComment':\n if (ch === '\\n') {\n state = 'code';\n }\n i++;\n break;\n\n case 'blockComment':\n if (ch === '*' && next === '/') {\n state = 'code';\n i += 2;\n break;\n }\n i++;\n break;\n }\n }\n\n return -1;\n};\n\nconst wrapMdxExports = (code: string): string => {\n const ensureNamedExport = (source: string, fnName: string): string =>\n source.replace(\n new RegExp(`^(\\\\s*)(?:export\\\\s+)?function\\\\s+${fnName}`, 'gm'),\n (match, leading) => {\n if (match.trimStart().startsWith('export ')) {\n return match;\n }\n return `${leading}export function ${fnName}`;\n }\n );\n\n const ensureDefaultExport = (source: string, fnName: string): string =>\n source.replace(\n new RegExp(`^(\\\\s*)(?:export\\\\s+)?function\\\\s+${fnName}`, 'gm'),\n (match, leading) => {\n if (match.trimStart().startsWith('export default')) {\n return match;\n }\n return `${leading}export default function ${fnName}`;\n }\n );\n\n const withNamed = ensureNamedExport(code, '_createMdxContent');\n const withDefault = ensureDefaultExport(withNamed, 'MDXContent');\n return withDefault;\n};\n\nconst insertImportString = (source: string, statement: string): string => {\n const insertPos = findImportInsertionIndex(source);\n\n if (insertPos >= 0) {\n const needsLeadingNewline =\n insertPos > 0 && source[insertPos - 1] !== '\\n';\n const snippet = needsLeadingNewline ? `\\n${statement}` : statement;\n return `${source.slice(0, insertPos)}${snippet}${source.slice(insertPos)}`;\n }\n\n return `${statement}${source}`;\n};\n\ntype ScannerState =\n | 'code'\n | 'single'\n | 'double'\n | 'template'\n | 'lineComment'\n | 'blockComment';\n\ninterface ImportStatement {\n start: number;\n end: number;\n code: string;\n}\n\nconst getImportStatements = (source: string): ImportStatement[] => {\n const statements: ImportStatement[] = [];\n let i = 0;\n let state: ScannerState = 'code';\n let templateDepth = 0;\n\n while (i < source.length) {\n const ch = source[i];\n const next = source[i + 1];\n\n switch (state) {\n case 'code': {\n if (ch === '/' && next === '/') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (ch === '/' && next === '*') {\n state = 'blockComment';\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'single';\n i++;\n continue;\n }\n if (ch === '\"') {\n state = 'double';\n i++;\n continue;\n }\n if (ch === '`') {\n state = 'template';\n templateDepth = 0;\n i++;\n continue;\n }\n if (i === 0 && ch === '#' && next === '!') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (\n ch === 'i' &&\n isImportKeyword(source, i) &&\n !isDynamicImport(source, i)\n ) {\n const end = findImportStatementEnd(source, i);\n statements.push({\n start: i,\n end,\n code: source.slice(i, end),\n });\n i = end;\n continue;\n }\n i++;\n break;\n }\n\n case 'single':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'code';\n }\n i++;\n break;\n\n case 'double':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '\"') {\n state = 'code';\n }\n i++;\n break;\n\n case 'template':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '`') {\n if (templateDepth === 0) {\n state = 'code';\n i++;\n continue;\n }\n templateDepth--;\n i++;\n continue;\n }\n if (ch === '$' && next === '{') {\n templateDepth++;\n i += 2;\n continue;\n }\n if (ch === '}' && templateDepth > 0) {\n templateDepth--;\n }\n i++;\n break;\n\n case 'lineComment':\n if (ch === '\\n') {\n state = 'code';\n }\n i++;\n break;\n\n case 'blockComment':\n if (ch === '*' && next === '/') {\n state = 'code';\n i += 2;\n continue;\n }\n i++;\n break;\n }\n }\n\n return statements;\n};\n\nconst findImportInsertionIndex = (source: string): number => {\n const statements = getImportStatements(source);\n\n if (!statements.length) {\n return -1;\n }\n\n return statements[statements.length - 1].end;\n};\n\nconst hasNamedImportFrom = (\n source: string,\n specifier: string,\n name: string\n): boolean => {\n const statements = getImportStatements(source);\n const fromPattern = new RegExp(\n `from\\\\s+['\"]${escapeRegExp(specifier)}['\"]`\n );\n const namePattern = new RegExp(`\\\\b${escapeRegExp(name)}\\\\b`);\n\n return statements.some(statement => {\n if (!fromPattern.test(statement.code)) {\n return false;\n }\n\n const [clause] = statement.code.split(/from\\s+/i);\n return namePattern.test(clause);\n });\n};\n\nconst escapeRegExp = (value: string): string =>\n value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n\nconst isImportKeyword = (source: string, index: number): boolean => {\n if (!source.startsWith('import', index)) {\n return false;\n }\n\n const prev = source[index - 1];\n if (prev && isIdentifierChar(prev)) {\n return false;\n }\n\n const next = source[index + 'import'.length];\n if (next && isIdentifierChar(next)) {\n return false;\n }\n\n return true;\n};\n\nconst isIdentifierChar = (ch: string): boolean => /[A-Za-z0-9_$]/.test(ch);\n\nconst isDynamicImport = (source: string, index: number): boolean => {\n const nextChar = source[index + 'import'.length];\n if (!nextChar) {\n return true;\n }\n if (nextChar === '(' || nextChar === '.') {\n return true;\n }\n return false;\n};\n\nconst findImportStatementEnd = (source: string, start: number): number => {\n let i = start;\n let state: ScannerState = 'code';\n\n while (i < source.length) {\n const ch = source[i];\n const next = source[i + 1];\n\n switch (state) {\n case 'code':\n if (ch === ';') {\n return i + 1;\n }\n if (ch === '/' && next === '/') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (ch === '/' && next === '*') {\n state = 'blockComment';\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'single';\n i++;\n continue;\n }\n if (ch === '\"') {\n state = 'double';\n i++;\n continue;\n }\n if (ch === '`') {\n state = 'template';\n i++;\n continue;\n }\n i++;\n break;\n\n case 'single':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'code';\n }\n i++;\n break;\n\n case 'double':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '\"') {\n state = 'code';\n }\n i++;\n break;\n\n case 'template':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '`') {\n state = 'code';\n }\n i++;\n break;\n\n case 'lineComment':\n if (ch === '\\n') {\n state = 'code';\n }\n i++;\n break;\n\n case 'blockComment':\n if (ch === '*' && next === '/') {\n state = 'code';\n i += 2;\n continue;\n }\n i++;\n break;\n }\n }\n\n return source.length;\n};\n","import type { Plugin, PluginOption } from 'vite';\nimport mdxRollup from '@mdx-js/rollup';\nimport { wrapMdxModule } from './transform';\n\nexport interface LithentMdxPluginOptions {\n /**\n * MDX plugin options to pass to @mdx-js/rollup\n */\n mdxOptions?: Record<string, unknown>;\n\n /**\n * JSX import source for MDX files\n * @default 'lithent'\n */\n jsxImportSource?: string;\n}\n\nexport const lithentMdx = (\n options: LithentMdxPluginOptions = {}\n): PluginOption => {\n const jsxImportSource = options.jsxImportSource ?? 'lithent';\n\n // Create the MDX rollup plugin with Lithent-specific JSX settings\n const mdxPlugin = mdxRollup({\n jsxImportSource,\n ...options.mdxOptions,\n });\n\n // Create the wrapper plugin that adds mount() wrapping\n const wrapperPlugin: Plugin = {\n name: 'lithent:mdx-wrapper',\n enforce: undefined, // normal - runs after mdxPlugin in array order\n\n transform(code, id) {\n const [filepath] = id.split('?', 1);\n\n // Only process .mdx files\n if (!/\\.mdx$/i.test(filepath)) {\n return null;\n }\n\n // Wrap MDXContent with mount() and add export default\n const result = wrapMdxModule(code);\n\n if (!result.modified) {\n return null;\n }\n\n return {\n code: result.code,\n map: null,\n };\n },\n };\n\n // Return both plugins as an array\n // Order matters: mdxPlugin first (MDX → JSX), then wrapperPlugin (add mount())\n return [mdxPlugin, wrapperPlugin];\n};\n\nexport default lithentMdx;\n"],"names":["MDX_WRAP_SENTINEL","wrapMdxModule","code","fixedExports","wrapMdxExports","exportsModified","defaultExportIndex","findDefaultMdxExportIndex","fnName","start","end","updated","hasNamedImportFrom","insertImportString","wrapperVar","wrapperSnippet","source","target","length","i","state","templateDepth","ch","next","ensureNamedExport","match","leading","ensureDefaultExport","withNamed","statement","insertPos","findImportInsertionIndex","snippet","getImportStatements","statements","isImportKeyword","isDynamicImport","findImportStatementEnd","specifier","name","fromPattern","escapeRegExp","namePattern","clause","value","index","prev","isIdentifierChar","nextChar","lithentMdx","options","jsxImportSource","mdxRollup","id","filepath","result"],"mappings":";AAKO,MAAMA,IAAoB,6BAEpBC,IAAgB,CAACC,MAAgC;AACtD,QAAAC,IAAeC,EAAeF,CAAI,GAClCG,IAAkBF,MAAiBD;AAErC,MAAAC,EAAa,SAASH,CAAiB;AACzC,WAAO,EAAE,MAAMG,GAAc,UAAUE,EAAgB;AAGnD,QAAAC,IAAqBC,EAA0BJ,CAAY;AAEjE,MAAIG,IAAqB;AACvB,WAAO,EAAE,MAAMH,GAAc,UAAUE,EAAgB;AAGzD,QAAMG,IAAS,cAETC,IAAQH,GACRI,IAAMD,IAAQ;AACpB,MAAIE,IAAU,GAAGR,EAAa,MAAM,GAAGM,CAAK,CAAC,YAAYD,CAAM,GAAGL,EAAa,MAAMO,CAAG,CAAC;AAKzF,EAFuBE,EAAmBD,GAAS,WAAW,OAAO,MAGnEA,IAAUE,EAAmBF,GAAS;AAAA,CAAoC;AAGtE,QAAAG,IAAa,yBAAyBN,CAAM,IAC5CO,IAAiB;AAAA,EACvBf,CAAiB;AAAA,QACXc,CAAU;AAAA,iBACDN,CAAM;AAAA;AAAA,iBAENM,CAAU;AAAA;AAGf,SAAAH,IAAA,GAAGA,CAAO,GAAGI,CAAc,IAE9B,EAAE,MAAMJ,GAAS,UAAU,GAAK;AACzC,GAEMJ,IAA4B,CAACS,MAA2B;AAC5D,QAAMC,IAAS,sCACTC,IAASF,EAAO;AACtB,MAAIG,IAAI,GACJC,IAMiB,QACjBC,IAAgB;AAEpB,SAAOF,IAAID,KAAQ;A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AAA,MAEF,KAAK;AACH,QAAIG,MAAO;AAAA,MACDF,IAAA,SAEVD;AACA;AAAA,MAEF,KAAK;AACC,YAAAG,MAAO,OAAOC,MAAS,KAAK;AACtB,UAAAH,IAAA,QACHD,KAAA;AACL;AAAA,QACF;AACA,QAAAA;AACA;AAAA,IACJ;AAAA,EACF;AAEA,SAAOH,EAAO;AAChB,GCrfaiC,IAAa,CACxBC,IAAmC,OAClB;AACX,QAAAC,IAAkBD,EAAQ,mBAAmB;AAqC5C,SAAA,CAlCWE,EAAU;AAAA,IAC1B,iBAAAD;AAAA,IACA,GAAGD,EAAQ;AAAA,EAAA,CACZ,GAG6B;AAAA,IAC5B,MAAM;AAAA,IACN,SAAS;AAAA;AAAA,IAET,UAAUhD,GAAMmD,GAAI;AAClB,YAAM,CAACC,CAAQ,IAAID,EAAG,MAAM,KAAK,CAAC;AAGlC,UAAI,CAAC,UAAU,KAAKC,CAAQ;AACnB,eAAA;AAIH,YAAAC,IAAStD,EAAcC,CAAI;AAE7B,aAACqD,EAAO,WAIL;AAAA,QACL,MAAMA,EAAO;AAAA,QACb,KAAK;AAAA,MAAA,IALE;AAAA,IAOX;AAAA,EAAA,CAK8B;AAClC;"}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
(function(l,
|
|
2
|
-
`));const
|
|
3
|
-
${
|
|
4
|
-
const ${
|
|
5
|
-
return () => ${
|
|
1
|
+
(function(l,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("@mdx-js/rollup")):typeof define=="function"&&define.amd?define(["exports","@mdx-js/rollup"],f):(l=typeof globalThis<"u"?globalThis:l||self,f(l.lithentMdx={},l.mdxRollup))})(this,function(l,f){"use strict";const u="/* __lithentMdxWrapped */",d=n=>{const r=$(n),e=r!==n;if(r.includes(u))return{code:r,modified:e};const t=g(r);if(t<0)return{code:r,modified:e};const i="MDXContent",o=t,c=o+34;let s=`${r.slice(0,o)}function ${i}${r.slice(c)}`;M(s,"lithent","mount")||(s=k(s,`import { mount } from 'lithent';
|
|
2
|
+
`));const b=`__LithentMdxComponent_${i}`,_=`
|
|
3
|
+
${u}
|
|
4
|
+
const ${b} = mount((_renew, props) => {
|
|
5
|
+
return () => ${i}(props ?? {});
|
|
6
6
|
});
|
|
7
|
-
export default ${
|
|
8
|
-
`;return
|
|
9
|
-
`&&(
|
|
7
|
+
export default ${b};
|
|
8
|
+
`;return s=`${s}${_}`,{code:s,modified:!0}},g=n=>{const r="export default function MDXContent",e=n.length;let t=0,i="code",o=0;for(;t<e;){const c=n[t],s=n[t+1];switch(i){case"code":if(c==="/"){if(s==="/"){i="lineComment",t+=2;continue}if(s==="*"){i="blockComment",t+=2;continue}}if(c==="'"){i="single",t++;continue}if(c==='"'){i="double",t++;continue}if(c==="`"){i="template",o=0,t++;continue}if(n.startsWith(r,t))return t;t++;break;case"single":if(c==="\\"){t+=2;continue}c==="'"&&(i="code"),t++;break;case"double":if(c==="\\"){t+=2;continue}c==='"'&&(i="code"),t++;break;case"template":if(c==="\\"){t+=2;continue}if(c==="`"){if(o===0){i="code",t++;break}o--,t++;break}if(c==="$"&&s==="{"){o++,t+=2;break}if(c==="}"){o>0&&o--,t++;break}t++;break;case"lineComment":c===`
|
|
9
|
+
`&&(i="code"),t++;break;case"blockComment":if(c==="*"&&s==="/"){i="code",t+=2;break}t++;break}}return-1},$=n=>{const r=(o,c)=>o.replace(new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${c}`,"gm"),(s,a)=>s.trimStart().startsWith("export ")?s:`${a}export function ${c}`),e=(o,c)=>o.replace(new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${c}`,"gm"),(s,a)=>s.trimStart().startsWith("export default")?s:`${a}export default function ${c}`),t=r(n,"_createMdxContent");return e(t,"MDXContent")},k=(n,r)=>{const e=w(n);if(e>=0){const i=e>0&&n[e-1]!==`
|
|
10
|
+
`?`
|
|
11
|
+
${r}`:r;return`${n.slice(0,e)}${i}${n.slice(e)}`}return`${r}${n}`},p=n=>{const r=[];let e=0,t="code",i=0;for(;e<n.length;){const o=n[e],c=n[e+1];switch(t){case"code":{if(o==="/"&&c==="/"){t="lineComment",e+=2;continue}if(o==="/"&&c==="*"){t="blockComment",e+=2;continue}if(o==="'"){t="single",e++;continue}if(o==='"'){t="double",e++;continue}if(o==="`"){t="template",i=0,e++;continue}if(e===0&&o==="#"&&c==="!"){t="lineComment",e+=2;continue}if(o==="i"&&C(n,e)&&!I(n,e)){const s=E(n,e);r.push({start:e,end:s,code:n.slice(e,s)}),e=s;continue}e++;break}case"single":if(o==="\\"){e+=2;continue}o==="'"&&(t="code"),e++;break;case"double":if(o==="\\"){e+=2;continue}o==='"'&&(t="code"),e++;break;case"template":if(o==="\\"){e+=2;continue}if(o==="`"){if(i===0){t="code",e++;continue}i--,e++;continue}if(o==="$"&&c==="{"){i++,e+=2;continue}o==="}"&&i>0&&i--,e++;break;case"lineComment":o===`
|
|
12
|
+
`&&(t="code"),e++;break;case"blockComment":if(o==="*"&&c==="/"){t="code",e+=2;continue}e++;break}}return r},w=n=>{const r=p(n);return r.length?r[r.length-1].end:-1},M=(n,r,e)=>{const t=p(n),i=new RegExp(`from\\s+['"]${m(r)}['"]`),o=new RegExp(`\\b${m(e)}\\b`);return t.some(c=>{if(!i.test(c.code))return!1;const[s]=c.code.split(/from\s+/i);return o.test(s)})},m=n=>n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),C=(n,r)=>{if(!n.startsWith("import",r))return!1;const e=n[r-1];if(e&&x(e))return!1;const t=n[r+6];return!(t&&x(t))},x=n=>/[A-Za-z0-9_$]/.test(n),I=(n,r)=>{const e=n[r+6];return!e||e==="("||e==="."},E=(n,r)=>{let e=r,t="code";for(;e<n.length;){const i=n[e],o=n[e+1];switch(t){case"code":if(i===";")return e+1;if(i==="/"&&o==="/"){t="lineComment",e+=2;continue}if(i==="/"&&o==="*"){t="blockComment",e+=2;continue}if(i==="'"){t="single",e++;continue}if(i==='"'){t="double",e++;continue}if(i==="`"){t="template",e++;continue}e++;break;case"single":if(i==="\\"){e+=2;continue}i==="'"&&(t="code"),e++;break;case"double":if(i==="\\"){e+=2;continue}i==='"'&&(t="code"),e++;break;case"template":if(i==="\\"){e+=2;continue}i==="`"&&(t="code"),e++;break;case"lineComment":i===`
|
|
13
|
+
`&&(t="code"),e++;break;case"blockComment":if(i==="*"&&o==="/"){t="code",e+=2;continue}e++;break}}return n.length},h=(n={})=>{const r=n.jsxImportSource??"lithent";return[f({jsxImportSource:r,...n.mdxOptions}),{name:"lithent:mdx-wrapper",enforce:void 0,transform(i,o){const[c]=o.split("?",1);if(!/\.mdx$/i.test(c))return null;const s=d(i);return s.modified?{code:s.code,map:null}:null}}]};l.MDX_WRAP_SENTINEL=u,l.default=h,l.lithentMdx=h,l.wrapMdxModule=d,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
10
14
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/transform.ts","../src/plugin.ts"],"sourcesContent":["export interface MdxWrapResult {\n code: string;\n modified: boolean;\n}\n\nexport const MDX_WRAP_SENTINEL = '/* __lithentMdxWrapped */';\n\nexport const wrapMdxModule = (code: string): MdxWrapResult => {\n const fixedExports = wrapMdxExports(code);\n const exportsModified = fixedExports !== code;\n\n if (fixedExports.includes(MDX_WRAP_SENTINEL)) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const defaultExportIndex = findDefaultMdxExportIndex(fixedExports);\n\n if (defaultExportIndex < 0) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const fnName = 'MDXContent';\n\n const start = defaultExportIndex;\n const end = start + 'export default function MDXContent'.length;\n let updated = `${fixedExports.slice(0, start)}function ${fnName}${fixedExports.slice(end)}`;\n\n // Ensure mount import exists\n const hasMountImport =\n /import\\s+{[^}]*\\bmount\\b[^}]*}\\s+from\\s+['\"]lithent['\"]/.test(updated);\n\n if (!hasMountImport) {\n updated = insertImportString(updated, `import { mount } from 'lithent';\\n`);\n }\n\n const wrapperVar = `__LithentMdxComponent_${fnName}`;\n const wrapperSnippet = `\n${MDX_WRAP_SENTINEL}\nconst ${wrapperVar} = mount((_renew, props) => {\n return () => ${fnName}(props ?? {});\n});\nexport default ${wrapperVar};\n`;\n\n updated = `${updated}${wrapperSnippet}`;\n\n return { code: updated, modified: true };\n};\n\nconst findDefaultMdxExportIndex = (source: string): number => {\n const target = 'export default function MDXContent';\n const length = source.length;\n let i = 0;\n let state:\n | 'code'\n | 'single'\n | 'double'\n | 'template'\n | 'lineComment'\n | 'blockComment' = 'code';\n let templateDepth = 0;\n\n while (i < length) {\n const ch = source[i];\n const next = source[i + 1];\n\n switch (state) {\n case 'code':\n if (ch === '/') {\n if (next === '/') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (next === '*') {\n state = 'blockComment';\n i += 2;\n continue;\n }\n }\n if (ch === \"'\") {\n state = 'single';\n i++;\n continue;\n }\n if (ch === '\"') {\n state = 'double';\n i++;\n continue;\n }\n if (ch === '`') {\n state = 'template';\n templateDepth = 0;\n i++;\n continue;\n }\n if (source.startsWith(target, i)) {\n return i;\n }\n i++;\n break;\n\n case 'single':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'code';\n }\n i++;\n break;\n\n case 'double':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '\"') {\n state = 'code';\n }\n i++;\n break;\n\n case 'template':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '`') {\n if (templateDepth === 0) {\n state = 'code';\n i++;\n break;\n }\n templateDepth--;\n i++;\n break;\n }\n if (ch === '$' && next === '{') {\n templateDepth++;\n i += 2;\n break;\n }\n if (ch === '}') {\n if (templateDepth > 0) {\n templateDepth--;\n }\n i++;\n break;\n }\n i++;\n break;\n\n case 'lineComment':\n if (ch === '\\n') {\n state = 'code';\n }\n i++;\n break;\n\n case 'blockComment':\n if (ch === '*' && next === '/') {\n state = 'code';\n i += 2;\n break;\n }\n i++;\n break;\n }\n }\n\n return -1;\n};\n\nconst wrapMdxExports = (code: string): string => {\n const ensureNamedExport = (source: string, fnName: string): string =>\n source.replace(\n new RegExp(`^(\\\\s*)(?:export\\\\s+)?function\\\\s+${fnName}`, 'gm'),\n (match, leading) => {\n if (match.trimStart().startsWith('export ')) {\n return match;\n }\n return `${leading}export function ${fnName}`;\n }\n );\n\n const ensureDefaultExport = (source: string, fnName: string): string =>\n source.replace(\n new RegExp(`^(\\\\s*)(?:export\\\\s+)?function\\\\s+${fnName}`, 'gm'),\n (match, leading) => {\n if (match.trimStart().startsWith('export default')) {\n return match;\n }\n return `${leading}export default function ${fnName}`;\n }\n );\n\n const withNamed = ensureNamedExport(code, '_createMdxContent');\n const withDefault = ensureDefaultExport(withNamed, 'MDXContent');\n return withDefault;\n};\n\nconst insertImportString = (source: string, statement: string): string => {\n const importRegex = /^(import[\\s\\S]*?from\\s+['\"][^'\"]+['\"]\\s*;[ \\t]*\\n?)/gm;\n let lastMatch: RegExpExecArray | null = null;\n let match: RegExpExecArray | null;\n\n while ((match = importRegex.exec(source))) {\n lastMatch = match;\n }\n\n if (lastMatch) {\n const insertPos = lastMatch.index + lastMatch[0].length;\n return `${source.slice(0, insertPos)}${statement}${source.slice(insertPos)}`;\n }\n\n return `${statement}${source}`;\n};\n","import type { Plugin, PluginOption } from 'vite';\nimport mdxRollup from '@mdx-js/rollup';\nimport { wrapMdxModule } from './transform';\n\nexport interface LithentMdxPluginOptions {\n /**\n * MDX plugin options to pass to @mdx-js/rollup\n */\n mdxOptions?: Record<string, unknown>;\n\n /**\n * JSX import source for MDX files\n * @default 'lithent'\n */\n jsxImportSource?: string;\n}\n\nexport const lithentMdx = (\n options: LithentMdxPluginOptions = {}\n): PluginOption => {\n const jsxImportSource = options.jsxImportSource ?? 'lithent';\n\n // Create the MDX rollup plugin with Lithent-specific JSX settings\n const mdxPlugin = mdxRollup({\n jsxImportSource,\n ...options.mdxOptions,\n });\n\n // Create the wrapper plugin that adds mount() wrapping\n const wrapperPlugin: Plugin = {\n name: 'lithent:mdx-wrapper',\n enforce: undefined, // normal - runs after mdxPlugin in array order\n\n transform(code, id) {\n const [filepath] = id.split('?', 1);\n\n // Only process .mdx files\n if (!/\\.mdx$/i.test(filepath)) {\n return null;\n }\n\n // Wrap MDXContent with mount() and add export default\n const result = wrapMdxModule(code);\n\n if (!result.modified) {\n return null;\n }\n\n return {\n code: result.code,\n map: null,\n };\n },\n };\n\n // Return both plugins as an array\n // Order matters: mdxPlugin first (MDX → JSX), then wrapperPlugin (add mount())\n return [mdxPlugin, wrapperPlugin];\n};\n\nexport default lithentMdx;\n"],"names":["MDX_WRAP_SENTINEL","wrapMdxModule","code","fixedExports","wrapMdxExports","exportsModified","defaultExportIndex","findDefaultMdxExportIndex","fnName","start","end","updated","insertImportString","wrapperVar","wrapperSnippet","source","target","length","i","state","templateDepth","ch","next","ensureNamedExport","match","leading","ensureDefaultExport","withNamed","statement","importRegex","lastMatch","insertPos","lithentMdx","options","jsxImportSource","mdxRollup","id","filepath","result"],"mappings":"2RAKa,MAAAA,EAAoB,4BAEpBC,EAAiBC,GAAgC,CACtD,MAAAC,EAAeC,EAAeF,CAAI,EAClCG,EAAkBF,IAAiBD,EAErC,GAAAC,EAAa,SAASH,CAAiB,EACzC,MAAO,CAAE,KAAMG,EAAc,SAAUE,CAAgB,EAGnD,MAAAC,EAAqBC,EAA0BJ,CAAY,EAEjE,GAAIG,EAAqB,EACvB,MAAO,CAAE,KAAMH,EAAc,SAAUE,CAAgB,EAGzD,MAAMG,EAAS,aAETC,EAAQH,EACRI,EAAMD,EAAQ,GACpB,IAAIE,EAAU,GAAGR,EAAa,MAAM,EAAGM,CAAK,CAAC,YAAYD,CAAM,GAAGL,EAAa,MAAMO,CAAG,CAAC,GAIvF,0DAA0D,KAAKC,CAAO,IAGtEA,EAAUC,EAAmBD,EAAS;AA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,CAlCWE,EAAU,CAC1B,gBAAAD,EACA,GAAGD,EAAQ,UAAA,CACZ,EAG6B,CAC5B,KAAM,sBACN,QAAS,OAET,UAAU/B,EAAMkC,EAAI,CAClB,KAAM,CAACC,CAAQ,EAAID,EAAG,MAAM,IAAK,CAAC,EAGlC,GAAI,CAAC,UAAU,KAAKC,CAAQ,EACnB,OAAA,KAIH,MAAAC,EAASrC,EAAcC,CAAI,EAE7B,OAACoC,EAAO,SAIL,CACL,KAAMA,EAAO,KACb,IAAK,IAAA,EALE,IAOX,CAAA,CAK8B,CAClC"}
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/transform.ts","../src/plugin.ts"],"sourcesContent":["export interface MdxWrapResult {\n code: string;\n modified: boolean;\n}\n\nexport const MDX_WRAP_SENTINEL = '/* __lithentMdxWrapped */';\n\nexport const wrapMdxModule = (code: string): MdxWrapResult => {\n const fixedExports = wrapMdxExports(code);\n const exportsModified = fixedExports !== code;\n\n if (fixedExports.includes(MDX_WRAP_SENTINEL)) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const defaultExportIndex = findDefaultMdxExportIndex(fixedExports);\n\n if (defaultExportIndex < 0) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const fnName = 'MDXContent';\n\n const start = defaultExportIndex;\n const end = start + 'export default function MDXContent'.length;\n let updated = `${fixedExports.slice(0, start)}function ${fnName}${fixedExports.slice(end)}`;\n\n // Ensure mount import exists\n const hasMountImport = hasNamedImportFrom(updated, 'lithent', 'mount');\n\n if (!hasMountImport) {\n updated = insertImportString(updated, `import { mount } from 'lithent';\\n`);\n }\n\n const wrapperVar = `__LithentMdxComponent_${fnName}`;\n const wrapperSnippet = `\n${MDX_WRAP_SENTINEL}\nconst ${wrapperVar} = mount((_renew, props) => {\n return () => ${fnName}(props ?? {});\n});\nexport default ${wrapperVar};\n`;\n\n updated = `${updated}${wrapperSnippet}`;\n\n return { code: updated, modified: true };\n};\n\nconst findDefaultMdxExportIndex = (source: string): number => {\n const target = 'export default function MDXContent';\n const length = source.length;\n let i = 0;\n let state:\n | 'code'\n | 'single'\n | 'double'\n | 'template'\n | 'lineComment'\n | 'blockComment' = 'code';\n let templateDepth = 0;\n\n while (i < length) {\n const ch = source[i];\n const next = source[i + 1];\n\n switch (state) {\n case 'code':\n if (ch === '/') {\n if (next === '/') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (next === '*') {\n state = 'blockComment';\n i += 2;\n continue;\n }\n }\n if (ch === \"'\") {\n state = 'single';\n i++;\n continue;\n }\n if (ch === '\"') {\n state = 'double';\n i++;\n continue;\n }\n if (ch === '`') {\n state = 'template';\n templateDepth = 0;\n i++;\n continue;\n }\n if (source.startsWith(target, i)) {\n return i;\n }\n i++;\n break;\n\n case 'single':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'code';\n }\n i++;\n break;\n\n case 'double':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '\"') {\n state = 'code';\n }\n i++;\n break;\n\n case 'template':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '`') {\n if (templateDepth === 0) {\n state = 'code';\n i++;\n break;\n }\n templateDepth--;\n i++;\n break;\n }\n if (ch === '$' && next === '{') {\n templateDepth++;\n i += 2;\n break;\n }\n if (ch === '}') {\n if (templateDepth > 0) {\n templateDepth--;\n }\n i++;\n break;\n }\n i++;\n break;\n\n case 'lineComment':\n if (ch === '\\n') {\n state = 'code';\n }\n i++;\n break;\n\n case 'blockComment':\n if (ch === '*' && next === '/') {\n state = 'code';\n i += 2;\n break;\n }\n i++;\n break;\n }\n }\n\n return -1;\n};\n\nconst wrapMdxExports = (code: string): string => {\n const ensureNamedExport = (source: string, fnName: string): string =>\n source.replace(\n new RegExp(`^(\\\\s*)(?:export\\\\s+)?function\\\\s+${fnName}`, 'gm'),\n (match, leading) => {\n if (match.trimStart().startsWith('export ')) {\n return match;\n }\n return `${leading}export function ${fnName}`;\n }\n );\n\n const ensureDefaultExport = (source: string, fnName: string): string =>\n source.replace(\n new RegExp(`^(\\\\s*)(?:export\\\\s+)?function\\\\s+${fnName}`, 'gm'),\n (match, leading) => {\n if (match.trimStart().startsWith('export default')) {\n return match;\n }\n return `${leading}export default function ${fnName}`;\n }\n );\n\n const withNamed = ensureNamedExport(code, '_createMdxContent');\n const withDefault = ensureDefaultExport(withNamed, 'MDXContent');\n return withDefault;\n};\n\nconst insertImportString = (source: string, statement: string): string => {\n const insertPos = findImportInsertionIndex(source);\n\n if (insertPos >= 0) {\n const needsLeadingNewline =\n insertPos > 0 && source[insertPos - 1] !== '\\n';\n const snippet = needsLeadingNewline ? `\\n${statement}` : statement;\n return `${source.slice(0, insertPos)}${snippet}${source.slice(insertPos)}`;\n }\n\n return `${statement}${source}`;\n};\n\ntype ScannerState =\n | 'code'\n | 'single'\n | 'double'\n | 'template'\n | 'lineComment'\n | 'blockComment';\n\ninterface ImportStatement {\n start: number;\n end: number;\n code: string;\n}\n\nconst getImportStatements = (source: string): ImportStatement[] => {\n const statements: ImportStatement[] = [];\n let i = 0;\n let state: ScannerState = 'code';\n let templateDepth = 0;\n\n while (i < source.length) {\n const ch = source[i];\n const next = source[i + 1];\n\n switch (state) {\n case 'code': {\n if (ch === '/' && next === '/') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (ch === '/' && next === '*') {\n state = 'blockComment';\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'single';\n i++;\n continue;\n }\n if (ch === '\"') {\n state = 'double';\n i++;\n continue;\n }\n if (ch === '`') {\n state = 'template';\n templateDepth = 0;\n i++;\n continue;\n }\n if (i === 0 && ch === '#' && next === '!') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (\n ch === 'i' &&\n isImportKeyword(source, i) &&\n !isDynamicImport(source, i)\n ) {\n const end = findImportStatementEnd(source, i);\n statements.push({\n start: i,\n end,\n code: source.slice(i, end),\n });\n i = end;\n continue;\n }\n i++;\n break;\n }\n\n case 'single':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'code';\n }\n i++;\n break;\n\n case 'double':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '\"') {\n state = 'code';\n }\n i++;\n break;\n\n case 'template':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '`') {\n if (templateDepth === 0) {\n state = 'code';\n i++;\n continue;\n }\n templateDepth--;\n i++;\n continue;\n }\n if (ch === '$' && next === '{') {\n templateDepth++;\n i += 2;\n continue;\n }\n if (ch === '}' && templateDepth > 0) {\n templateDepth--;\n }\n i++;\n break;\n\n case 'lineComment':\n if (ch === '\\n') {\n state = 'code';\n }\n i++;\n break;\n\n case 'blockComment':\n if (ch === '*' && next === '/') {\n state = 'code';\n i += 2;\n continue;\n }\n i++;\n break;\n }\n }\n\n return statements;\n};\n\nconst findImportInsertionIndex = (source: string): number => {\n const statements = getImportStatements(source);\n\n if (!statements.length) {\n return -1;\n }\n\n return statements[statements.length - 1].end;\n};\n\nconst hasNamedImportFrom = (\n source: string,\n specifier: string,\n name: string\n): boolean => {\n const statements = getImportStatements(source);\n const fromPattern = new RegExp(\n `from\\\\s+['\"]${escapeRegExp(specifier)}['\"]`\n );\n const namePattern = new RegExp(`\\\\b${escapeRegExp(name)}\\\\b`);\n\n return statements.some(statement => {\n if (!fromPattern.test(statement.code)) {\n return false;\n }\n\n const [clause] = statement.code.split(/from\\s+/i);\n return namePattern.test(clause);\n });\n};\n\nconst escapeRegExp = (value: string): string =>\n value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n\nconst isImportKeyword = (source: string, index: number): boolean => {\n if (!source.startsWith('import', index)) {\n return false;\n }\n\n const prev = source[index - 1];\n if (prev && isIdentifierChar(prev)) {\n return false;\n }\n\n const next = source[index + 'import'.length];\n if (next && isIdentifierChar(next)) {\n return false;\n }\n\n return true;\n};\n\nconst isIdentifierChar = (ch: string): boolean => /[A-Za-z0-9_$]/.test(ch);\n\nconst isDynamicImport = (source: string, index: number): boolean => {\n const nextChar = source[index + 'import'.length];\n if (!nextChar) {\n return true;\n }\n if (nextChar === '(' || nextChar === '.') {\n return true;\n }\n return false;\n};\n\nconst findImportStatementEnd = (source: string, start: number): number => {\n let i = start;\n let state: ScannerState = 'code';\n\n while (i < source.length) {\n const ch = source[i];\n const next = source[i + 1];\n\n switch (state) {\n case 'code':\n if (ch === ';') {\n return i + 1;\n }\n if (ch === '/' && next === '/') {\n state = 'lineComment';\n i += 2;\n continue;\n }\n if (ch === '/' && next === '*') {\n state = 'blockComment';\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'single';\n i++;\n continue;\n }\n if (ch === '\"') {\n state = 'double';\n i++;\n continue;\n }\n if (ch === '`') {\n state = 'template';\n i++;\n continue;\n }\n i++;\n break;\n\n case 'single':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === \"'\") {\n state = 'code';\n }\n i++;\n break;\n\n case 'double':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '\"') {\n state = 'code';\n }\n i++;\n break;\n\n case 'template':\n if (ch === '\\\\') {\n i += 2;\n continue;\n }\n if (ch === '`') {\n state = 'code';\n }\n i++;\n break;\n\n case 'lineComment':\n if (ch === '\\n') {\n state = 'code';\n }\n i++;\n break;\n\n case 'blockComment':\n if (ch === '*' && next === '/') {\n state = 'code';\n i += 2;\n continue;\n }\n i++;\n break;\n }\n }\n\n return source.length;\n};\n","import type { Plugin, PluginOption } from 'vite';\nimport mdxRollup from '@mdx-js/rollup';\nimport { wrapMdxModule } from './transform';\n\nexport interface LithentMdxPluginOptions {\n /**\n * MDX plugin options to pass to @mdx-js/rollup\n */\n mdxOptions?: Record<string, unknown>;\n\n /**\n * JSX import source for MDX files\n * @default 'lithent'\n */\n jsxImportSource?: string;\n}\n\nexport const lithentMdx = (\n options: LithentMdxPluginOptions = {}\n): PluginOption => {\n const jsxImportSource = options.jsxImportSource ?? 'lithent';\n\n // Create the MDX rollup plugin with Lithent-specific JSX settings\n const mdxPlugin = mdxRollup({\n jsxImportSource,\n ...options.mdxOptions,\n });\n\n // Create the wrapper plugin that adds mount() wrapping\n const wrapperPlugin: Plugin = {\n name: 'lithent:mdx-wrapper',\n enforce: undefined, // normal - runs after mdxPlugin in array order\n\n transform(code, id) {\n const [filepath] = id.split('?', 1);\n\n // Only process .mdx files\n if (!/\\.mdx$/i.test(filepath)) {\n return null;\n }\n\n // Wrap MDXContent with mount() and add export default\n const result = wrapMdxModule(code);\n\n if (!result.modified) {\n return null;\n }\n\n return {\n code: result.code,\n map: null,\n };\n },\n };\n\n // Return both plugins as an array\n // Order matters: mdxPlugin first (MDX → JSX), then wrapperPlugin (add mount())\n return [mdxPlugin, wrapperPlugin];\n};\n\nexport default lithentMdx;\n"],"names":["MDX_WRAP_SENTINEL","wrapMdxModule","code","fixedExports","wrapMdxExports","exportsModified","defaultExportIndex","findDefaultMdxExportIndex","fnName","start","end","updated","hasNamedImportFrom","insertImportString","wrapperVar","wrapperSnippet","source","target","length","i","state","templateDepth","ch","next","ensureNamedExport","match","leading","ensureDefaultExport","withNamed","statement","insertPos","findImportInsertionIndex","snippet","getImportStatements","statements","isImportKeyword","isDynamicImport","findImportStatementEnd","specifier","name","fromPattern","escapeRegExp","namePattern","clause","value","index","prev","isIdentifierChar","nextChar","lithentMdx","options","jsxImportSource","mdxRollup","id","filepath","result"],"mappings":"2RAKa,MAAAA,EAAoB,4BAEpBC,EAAiBC,GAAgC,CACtD,MAAAC,EAAeC,EAAeF,CAAI,EAClCG,EAAkBF,IAAiBD,EAErC,GAAAC,EAAa,SAASH,CAAiB,EACzC,MAAO,CAAE,KAAMG,EAAc,SAAUE,CAAgB,EAGnD,MAAAC,EAAqBC,EAA0BJ,CAAY,EAEjE,GAAIG,EAAqB,EACvB,MAAO,CAAE,KAAMH,EAAc,SAAUE,CAAgB,EAGzD,MAAMG,EAAS,aAETC,EAAQH,EACRI,EAAMD,EAAQ,GACpB,IAAIE,EAAU,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,EAAkBD,EAAQ,iBAAmB,UAqC5C,MAAA,CAlCWE,EAAU,CAC1B,gBAAAD,EACA,GAAGD,EAAQ,UAAA,CACZ,EAG6B,CAC5B,KAAM,sBACN,QAAS,OAET,UAAUhD,EAAMmD,EAAI,CAClB,KAAM,CAACC,CAAQ,EAAID,EAAG,MAAM,IAAK,CAAC,EAGlC,GAAI,CAAC,UAAU,KAAKC,CAAQ,EACnB,OAAA,KAIH,MAAAC,EAAStD,EAAcC,CAAI,EAE7B,OAACqD,EAAO,SAIL,CACL,KAAMA,EAAO,KACb,IAAK,IAAA,EALE,IAOX,CAAA,CAK8B,CAClC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@lithent/lithent-mdx",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.4",
|
|
5
5
|
"description": "Vite plugin for MDX support in Lithent applications",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
7
7
|
"module": "./dist/index.mjs",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"vite": "^5.4.8",
|
|
21
|
-
"lithent": "1.20.
|
|
21
|
+
"lithent": "1.20.2",
|
|
22
22
|
"@lithent/hmr-parser": "0.2.0"
|
|
23
23
|
},
|
|
24
24
|
"keywords": [
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"url": "https://github.com/superlucky84/lithent.git"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"lithent": "^1.20.
|
|
37
|
+
"lithent": "^1.20.2",
|
|
38
38
|
"vite": "^5.0.0"
|
|
39
39
|
},
|
|
40
40
|
"files": [
|
package/src/mdx.example.mdx
CHANGED
|
@@ -101,4 +101,25 @@ export default function MDXContent(props) {
|
|
|
101
101
|
expect(result.code).toContain(`export default ${wrappedName};`);
|
|
102
102
|
expect(result.code).not.toContain('__LithentMdxComponent__LithentMdxComponent_MDXContent');
|
|
103
103
|
});
|
|
104
|
+
|
|
105
|
+
it('should insert mount import after real imports, not inside template strings', () => {
|
|
106
|
+
const example = `
|
|
107
|
+
import { jsx as _jsx } from 'lithent/jsx-runtime';
|
|
108
|
+
|
|
109
|
+
const codeBlock = "import { h, mountCallback, mount } from 'lithent';";
|
|
110
|
+
|
|
111
|
+
export function _createMdxContent(props) {
|
|
112
|
+
return _jsx('pre', { children: codeBlock });
|
|
113
|
+
}
|
|
114
|
+
export default function MDXContent(props) {
|
|
115
|
+
return _createMdxContent(props);
|
|
116
|
+
}
|
|
117
|
+
`;
|
|
118
|
+
|
|
119
|
+
const result = wrapMdxModule(example);
|
|
120
|
+
|
|
121
|
+
expect(result.code).toMatch(
|
|
122
|
+
/import { jsx as _jsx } from 'lithent\/jsx-runtime';\s+import { mount } from 'lithent';/
|
|
123
|
+
);
|
|
124
|
+
});
|
|
104
125
|
});
|
package/src/transform.ts
CHANGED
|
@@ -26,8 +26,7 @@ export const wrapMdxModule = (code: string): MdxWrapResult => {
|
|
|
26
26
|
let updated = `${fixedExports.slice(0, start)}function ${fnName}${fixedExports.slice(end)}`;
|
|
27
27
|
|
|
28
28
|
// Ensure mount import exists
|
|
29
|
-
const hasMountImport =
|
|
30
|
-
/import\s+{[^}]*\bmount\b[^}]*}\s+from\s+['"]lithent['"]/.test(updated);
|
|
29
|
+
const hasMountImport = hasNamedImportFrom(updated, 'lithent', 'mount');
|
|
31
30
|
|
|
32
31
|
if (!hasMountImport) {
|
|
33
32
|
updated = insertImportString(updated, `import { mount } from 'lithent';\n`);
|
|
@@ -202,18 +201,319 @@ const wrapMdxExports = (code: string): string => {
|
|
|
202
201
|
};
|
|
203
202
|
|
|
204
203
|
const insertImportString = (source: string, statement: string): string => {
|
|
205
|
-
const
|
|
206
|
-
let lastMatch: RegExpExecArray | null = null;
|
|
207
|
-
let match: RegExpExecArray | null;
|
|
204
|
+
const insertPos = findImportInsertionIndex(source);
|
|
208
205
|
|
|
209
|
-
|
|
210
|
-
|
|
206
|
+
if (insertPos >= 0) {
|
|
207
|
+
const needsLeadingNewline =
|
|
208
|
+
insertPos > 0 && source[insertPos - 1] !== '\n';
|
|
209
|
+
const snippet = needsLeadingNewline ? `\n${statement}` : statement;
|
|
210
|
+
return `${source.slice(0, insertPos)}${snippet}${source.slice(insertPos)}`;
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
213
|
+
return `${statement}${source}`;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
type ScannerState =
|
|
217
|
+
| 'code'
|
|
218
|
+
| 'single'
|
|
219
|
+
| 'double'
|
|
220
|
+
| 'template'
|
|
221
|
+
| 'lineComment'
|
|
222
|
+
| 'blockComment';
|
|
223
|
+
|
|
224
|
+
interface ImportStatement {
|
|
225
|
+
start: number;
|
|
226
|
+
end: number;
|
|
227
|
+
code: string;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const getImportStatements = (source: string): ImportStatement[] => {
|
|
231
|
+
const statements: ImportStatement[] = [];
|
|
232
|
+
let i = 0;
|
|
233
|
+
let state: ScannerState = 'code';
|
|
234
|
+
let templateDepth = 0;
|
|
235
|
+
|
|
236
|
+
while (i < source.length) {
|
|
237
|
+
const ch = source[i];
|
|
238
|
+
const next = source[i + 1];
|
|
239
|
+
|
|
240
|
+
switch (state) {
|
|
241
|
+
case 'code': {
|
|
242
|
+
if (ch === '/' && next === '/') {
|
|
243
|
+
state = 'lineComment';
|
|
244
|
+
i += 2;
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (ch === '/' && next === '*') {
|
|
248
|
+
state = 'blockComment';
|
|
249
|
+
i += 2;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (ch === "'") {
|
|
253
|
+
state = 'single';
|
|
254
|
+
i++;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (ch === '"') {
|
|
258
|
+
state = 'double';
|
|
259
|
+
i++;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (ch === '`') {
|
|
263
|
+
state = 'template';
|
|
264
|
+
templateDepth = 0;
|
|
265
|
+
i++;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (i === 0 && ch === '#' && next === '!') {
|
|
269
|
+
state = 'lineComment';
|
|
270
|
+
i += 2;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (
|
|
274
|
+
ch === 'i' &&
|
|
275
|
+
isImportKeyword(source, i) &&
|
|
276
|
+
!isDynamicImport(source, i)
|
|
277
|
+
) {
|
|
278
|
+
const end = findImportStatementEnd(source, i);
|
|
279
|
+
statements.push({
|
|
280
|
+
start: i,
|
|
281
|
+
end,
|
|
282
|
+
code: source.slice(i, end),
|
|
283
|
+
});
|
|
284
|
+
i = end;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
i++;
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
case 'single':
|
|
292
|
+
if (ch === '\\') {
|
|
293
|
+
i += 2;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (ch === "'") {
|
|
297
|
+
state = 'code';
|
|
298
|
+
}
|
|
299
|
+
i++;
|
|
300
|
+
break;
|
|
301
|
+
|
|
302
|
+
case 'double':
|
|
303
|
+
if (ch === '\\') {
|
|
304
|
+
i += 2;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (ch === '"') {
|
|
308
|
+
state = 'code';
|
|
309
|
+
}
|
|
310
|
+
i++;
|
|
311
|
+
break;
|
|
312
|
+
|
|
313
|
+
case 'template':
|
|
314
|
+
if (ch === '\\') {
|
|
315
|
+
i += 2;
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (ch === '`') {
|
|
319
|
+
if (templateDepth === 0) {
|
|
320
|
+
state = 'code';
|
|
321
|
+
i++;
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
templateDepth--;
|
|
325
|
+
i++;
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (ch === '$' && next === '{') {
|
|
329
|
+
templateDepth++;
|
|
330
|
+
i += 2;
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
if (ch === '}' && templateDepth > 0) {
|
|
334
|
+
templateDepth--;
|
|
335
|
+
}
|
|
336
|
+
i++;
|
|
337
|
+
break;
|
|
338
|
+
|
|
339
|
+
case 'lineComment':
|
|
340
|
+
if (ch === '\n') {
|
|
341
|
+
state = 'code';
|
|
342
|
+
}
|
|
343
|
+
i++;
|
|
344
|
+
break;
|
|
345
|
+
|
|
346
|
+
case 'blockComment':
|
|
347
|
+
if (ch === '*' && next === '/') {
|
|
348
|
+
state = 'code';
|
|
349
|
+
i += 2;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
i++;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
216
355
|
}
|
|
217
356
|
|
|
218
|
-
return
|
|
357
|
+
return statements;
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const findImportInsertionIndex = (source: string): number => {
|
|
361
|
+
const statements = getImportStatements(source);
|
|
362
|
+
|
|
363
|
+
if (!statements.length) {
|
|
364
|
+
return -1;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return statements[statements.length - 1].end;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const hasNamedImportFrom = (
|
|
371
|
+
source: string,
|
|
372
|
+
specifier: string,
|
|
373
|
+
name: string
|
|
374
|
+
): boolean => {
|
|
375
|
+
const statements = getImportStatements(source);
|
|
376
|
+
const fromPattern = new RegExp(
|
|
377
|
+
`from\\s+['"]${escapeRegExp(specifier)}['"]`
|
|
378
|
+
);
|
|
379
|
+
const namePattern = new RegExp(`\\b${escapeRegExp(name)}\\b`);
|
|
380
|
+
|
|
381
|
+
return statements.some(statement => {
|
|
382
|
+
if (!fromPattern.test(statement.code)) {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const [clause] = statement.code.split(/from\s+/i);
|
|
387
|
+
return namePattern.test(clause);
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const escapeRegExp = (value: string): string =>
|
|
392
|
+
value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
const isImportKeyword = (source: string, index: number): boolean => {
|
|
396
|
+
if (!source.startsWith('import', index)) {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const prev = source[index - 1];
|
|
401
|
+
if (prev && isIdentifierChar(prev)) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const next = source[index + 'import'.length];
|
|
406
|
+
if (next && isIdentifierChar(next)) {
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return true;
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const isIdentifierChar = (ch: string): boolean => /[A-Za-z0-9_$]/.test(ch);
|
|
414
|
+
|
|
415
|
+
const isDynamicImport = (source: string, index: number): boolean => {
|
|
416
|
+
const nextChar = source[index + 'import'.length];
|
|
417
|
+
if (!nextChar) {
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
if (nextChar === '(' || nextChar === '.') {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
return false;
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const findImportStatementEnd = (source: string, start: number): number => {
|
|
427
|
+
let i = start;
|
|
428
|
+
let state: ScannerState = 'code';
|
|
429
|
+
|
|
430
|
+
while (i < source.length) {
|
|
431
|
+
const ch = source[i];
|
|
432
|
+
const next = source[i + 1];
|
|
433
|
+
|
|
434
|
+
switch (state) {
|
|
435
|
+
case 'code':
|
|
436
|
+
if (ch === ';') {
|
|
437
|
+
return i + 1;
|
|
438
|
+
}
|
|
439
|
+
if (ch === '/' && next === '/') {
|
|
440
|
+
state = 'lineComment';
|
|
441
|
+
i += 2;
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (ch === '/' && next === '*') {
|
|
445
|
+
state = 'blockComment';
|
|
446
|
+
i += 2;
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (ch === "'") {
|
|
450
|
+
state = 'single';
|
|
451
|
+
i++;
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
if (ch === '"') {
|
|
455
|
+
state = 'double';
|
|
456
|
+
i++;
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (ch === '`') {
|
|
460
|
+
state = 'template';
|
|
461
|
+
i++;
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
i++;
|
|
465
|
+
break;
|
|
466
|
+
|
|
467
|
+
case 'single':
|
|
468
|
+
if (ch === '\\') {
|
|
469
|
+
i += 2;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (ch === "'") {
|
|
473
|
+
state = 'code';
|
|
474
|
+
}
|
|
475
|
+
i++;
|
|
476
|
+
break;
|
|
477
|
+
|
|
478
|
+
case 'double':
|
|
479
|
+
if (ch === '\\') {
|
|
480
|
+
i += 2;
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
if (ch === '"') {
|
|
484
|
+
state = 'code';
|
|
485
|
+
}
|
|
486
|
+
i++;
|
|
487
|
+
break;
|
|
488
|
+
|
|
489
|
+
case 'template':
|
|
490
|
+
if (ch === '\\') {
|
|
491
|
+
i += 2;
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (ch === '`') {
|
|
495
|
+
state = 'code';
|
|
496
|
+
}
|
|
497
|
+
i++;
|
|
498
|
+
break;
|
|
499
|
+
|
|
500
|
+
case 'lineComment':
|
|
501
|
+
if (ch === '\n') {
|
|
502
|
+
state = 'code';
|
|
503
|
+
}
|
|
504
|
+
i++;
|
|
505
|
+
break;
|
|
506
|
+
|
|
507
|
+
case 'blockComment':
|
|
508
|
+
if (ch === '*' && next === '/') {
|
|
509
|
+
state = 'code';
|
|
510
|
+
i += 2;
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
i++;
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return source.length;
|
|
219
519
|
};
|