@lithent/lithent-mdx 0.2.0 → 0.2.2

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 CHANGED
@@ -17,7 +17,7 @@ pnpm add -D @lithent/lithent-mdx @mdx-js/rollup
17
17
  Required peer dependencies:
18
18
  - `lithent` `^1.20.0`
19
19
  - `vite` `^5.0.0`
20
- - `@lithent/lithent-vite` `^0.2.0` for end-to-end HMR support
20
+ - `@lithent/lithent-vite` `^0.2.1` for end-to-end HMR support
21
21
 
22
22
  ## Usage
23
23
 
package/dist/index.mjs CHANGED
@@ -1,67 +1,151 @@
1
- import x from "@mdx-js/rollup";
2
- const c = "/* __lithentMdxWrapped */", a = (e) => {
3
- const t = f(e), u = t !== e;
4
- if (t.includes(c))
5
- return { code: t, modified: u };
6
- const r = /export\s+default\s+function\s+([A-Za-z0-9_]+)/m.exec(t);
7
- if (!r || r.index === void 0)
8
- return { code: t, modified: u };
9
- const n = r[1], o = r.index, s = o + r[0].length;
10
- let i = `${t.slice(0, o)}function ${n}${t.slice(s)}`;
11
- /import\s+{[^}]*\bmount\b[^}]*}\s+from\s+['"]lithent['"]/.test(i) || (i = m(i, `import { mount } from 'lithent';
1
+ import f from "@mdx-js/rollup";
2
+ const p = "/* __lithentMdxWrapped */", d = (i) => {
3
+ const s = x(i), c = s !== i;
4
+ if (s.includes(p))
5
+ return { code: s, modified: c };
6
+ const t = m(s);
7
+ if (t < 0)
8
+ return { code: s, modified: c };
9
+ const n = "MDXContent", r = t, e = r + 34;
10
+ let o = `${s.slice(0, r)}function ${n}${s.slice(e)}`;
11
+ /import\s+{[^}]*\bmount\b[^}]*}\s+from\s+['"]lithent['"]/.test(o) || (o = h(o, `import { mount } from 'lithent';
12
12
  `));
13
- const l = `__LithentMdxComponent_${n}`, d = `
14
- ${c}
15
- const ${l} = mount((_renew, props) => {
13
+ const u = `__LithentMdxComponent_${n}`, a = `
14
+ ${p}
15
+ const ${u} = mount((_renew, props) => {
16
16
  return () => ${n}(props ?? {});
17
17
  });
18
- export default ${l};
18
+ export default ${u};
19
19
  `;
20
- return i = `${i}${d}`, { code: i, modified: !0 };
21
- }, f = (e) => {
22
- const t = (n, o) => n.replace(
23
- new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${o}`, "gm"),
24
- (s, i) => s.trimStart().startsWith("export ") ? s : `${i}export function ${o}`
25
- ), u = (n, o) => n.replace(
26
- new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${o}`, "gm"),
27
- (s, i) => s.trimStart().startsWith("export default") ? s : `${i}export default function ${o}`
28
- ), p = t(e, "_createMdxContent");
29
- return u(p, "MDXContent");
30
- }, m = (e, t) => {
31
- const u = /^(import[\s\S]*?from\s+['"][^'"]+['"]\s*;[ \t]*\n?)/gm;
32
- let p = null, r;
33
- for (; r = u.exec(e); )
34
- p = r;
35
- if (p) {
36
- const n = p.index + p[0].length;
37
- return `${e.slice(0, n)}${t}${e.slice(n)}`;
20
+ return o = `${o}${a}`, { code: o, modified: !0 };
21
+ }, m = (i) => {
22
+ const s = "export default function MDXContent", c = i.length;
23
+ let t = 0, n = "code", r = 0;
24
+ for (; t < c; ) {
25
+ const e = i[t], o = i[t + 1];
26
+ switch (n) {
27
+ case "code":
28
+ if (e === "/") {
29
+ if (o === "/") {
30
+ n = "lineComment", t += 2;
31
+ continue;
32
+ }
33
+ if (o === "*") {
34
+ n = "blockComment", t += 2;
35
+ continue;
36
+ }
37
+ }
38
+ if (e === "'") {
39
+ n = "single", t++;
40
+ continue;
41
+ }
42
+ if (e === '"') {
43
+ n = "double", t++;
44
+ continue;
45
+ }
46
+ if (e === "`") {
47
+ n = "template", r = 0, t++;
48
+ continue;
49
+ }
50
+ if (i.startsWith(s, t))
51
+ return t;
52
+ t++;
53
+ break;
54
+ case "single":
55
+ if (e === "\\") {
56
+ t += 2;
57
+ continue;
58
+ }
59
+ e === "'" && (n = "code"), t++;
60
+ break;
61
+ case "double":
62
+ if (e === "\\") {
63
+ t += 2;
64
+ continue;
65
+ }
66
+ e === '"' && (n = "code"), t++;
67
+ break;
68
+ case "template":
69
+ if (e === "\\") {
70
+ t += 2;
71
+ continue;
72
+ }
73
+ if (e === "`") {
74
+ if (r === 0) {
75
+ n = "code", t++;
76
+ break;
77
+ }
78
+ r--, t++;
79
+ break;
80
+ }
81
+ if (e === "$" && o === "{") {
82
+ r++, t += 2;
83
+ break;
84
+ }
85
+ if (e === "}") {
86
+ r > 0 && r--, t++;
87
+ break;
88
+ }
89
+ t++;
90
+ break;
91
+ case "lineComment":
92
+ e === `
93
+ ` && (n = "code"), t++;
94
+ break;
95
+ case "blockComment":
96
+ if (e === "*" && o === "/") {
97
+ n = "code", t += 2;
98
+ break;
99
+ }
100
+ t++;
101
+ break;
102
+ }
103
+ }
104
+ return -1;
105
+ }, x = (i) => {
106
+ const s = (r, e) => r.replace(
107
+ new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${e}`, "gm"),
108
+ (o, l) => o.trimStart().startsWith("export ") ? o : `${l}export function ${e}`
109
+ ), c = (r, e) => r.replace(
110
+ new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${e}`, "gm"),
111
+ (o, l) => o.trimStart().startsWith("export default") ? o : `${l}export default function ${e}`
112
+ ), t = s(i, "_createMdxContent");
113
+ return c(t, "MDXContent");
114
+ }, h = (i, s) => {
115
+ const c = /^(import[\s\S]*?from\s+['"][^'"]+['"]\s*;[ \t]*\n?)/gm;
116
+ let t = null, n;
117
+ for (; n = c.exec(i); )
118
+ t = n;
119
+ if (t) {
120
+ const r = t.index + t[0].length;
121
+ return `${i.slice(0, r)}${s}${i.slice(r)}`;
38
122
  }
39
- return `${t}${e}`;
40
- }, g = (e = {}) => {
41
- const t = e.jsxImportSource ?? "lithent";
42
- return [x({
43
- jsxImportSource: t,
44
- ...e.mdxOptions
123
+ return `${s}${i}`;
124
+ }, g = (i = {}) => {
125
+ const s = i.jsxImportSource ?? "lithent";
126
+ return [f({
127
+ jsxImportSource: s,
128
+ ...i.mdxOptions
45
129
  }), {
46
130
  name: "lithent:mdx-wrapper",
47
131
  enforce: void 0,
48
132
  // normal - runs after mdxPlugin in array order
49
- transform(r, n) {
50
- const [o] = n.split("?", 1);
51
- if (!/\.mdx$/i.test(o))
133
+ transform(n, r) {
134
+ const [e] = r.split("?", 1);
135
+ if (!/\.mdx$/i.test(e))
52
136
  return null;
53
- const s = a(r);
54
- return s.modified ? {
55
- code: s.code,
137
+ const o = d(n);
138
+ return o.modified ? {
139
+ code: o.code,
56
140
  map: null
57
141
  } : null;
58
142
  }
59
143
  }];
60
144
  };
61
145
  export {
62
- c as MDX_WRAP_SENTINEL,
146
+ p as MDX_WRAP_SENTINEL,
63
147
  g as default,
64
148
  g as lithentMdx,
65
- a as wrapMdxModule
149
+ d as wrapMdxModule
66
150
  };
67
151
  //# sourceMappingURL=index.mjs.map
@@ -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 defaultExportRegex = /export\\s+default\\s+function\\s+([A-Za-z0-9_]+)/m;\n const defaultExportMatch = defaultExportRegex.exec(fixedExports);\n\n if (!defaultExportMatch || defaultExportMatch.index === undefined) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const fnName = defaultExportMatch[1];\n\n const start = defaultExportMatch.index;\n const end = start + defaultExportMatch[0].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 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","defaultExportMatch","fnName","start","end","updated","insertImportString","wrapperVar","wrapperSnippet","ensureNamedExport","source","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;AAInD,QAAAC,IADqB,iDACmB,KAAKH,CAAY;AAE/D,MAAI,CAACG,KAAsBA,EAAmB,UAAU;AACtD,WAAO,EAAE,MAAMH,GAAc,UAAUE,EAAgB;AAGnD,QAAAE,IAASD,EAAmB,CAAC,GAE7BE,IAAQF,EAAmB,OAC3BG,IAAMD,IAA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AAS,CAAC,GAAGH,CAAS,GAAGL,EAAO,MAAMQ,CAAS,CAAC;AAAA,EAC5E;AAEO,SAAA,GAAGH,CAAS,GAAGL,CAAM;AAC9B,GC5EaS,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,UAAUvB,GAAM0B,GAAI;AAClB,YAAM,CAACC,CAAQ,IAAID,EAAG,MAAM,KAAK,CAAC;AAGlC,UAAI,CAAC,UAAU,KAAKC,CAAQ;AACnB,eAAA;AAIH,YAAAC,IAAS7B,EAAcC,CAAI;AAE7B,aAAC4B,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 =\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,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;AAMzF,EAFE,0DAA0D,KAAKC,CAAO,MAGtEA,IAAUC,EAAmBD,GAAS;AA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AIM,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;"}
package/dist/index.umd.js CHANGED
@@ -1,9 +1,10 @@
1
- (function(o,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@mdx-js/rollup")):typeof define=="function"&&define.amd?define(["exports","@mdx-js/rollup"],l):(o=typeof globalThis<"u"?globalThis:o||self,l(o.lithentMdx={},o.mdxRollup))})(this,function(o,l){"use strict";const c="/* __lithentMdxWrapped */",f=e=>{const t=m(e),p=t!==e;if(t.includes(c))return{code:t,modified:p};const r=/export\s+default\s+function\s+([A-Za-z0-9_]+)/m.exec(t);if(!r||r.index===void 0)return{code:t,modified:p};const n=r[1],i=r.index,s=i+r[0].length;let u=`${t.slice(0,i)}function ${n}${t.slice(s)}`;/import\s+{[^}]*\bmount\b[^}]*}\s+from\s+['"]lithent['"]/.test(u)||(u=h(u,`import { mount } from 'lithent';
2
- `));const a=`__LithentMdxComponent_${n}`,$=`
3
- ${c}
4
- const ${a} = mount((_renew, props) => {
1
+ (function(l,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@mdx-js/rollup")):typeof define=="function"&&define.amd?define(["exports","@mdx-js/rollup"],c):(l=typeof globalThis<"u"?globalThis:l||self,c(l.lithentMdx={},l.mdxRollup))})(this,function(l,c){"use strict";const f="/* __lithentMdxWrapped */",p=r=>{const s=h(r),u=s!==r;if(s.includes(f))return{code:s,modified:u};const t=x(s);if(t<0)return{code:s,modified:u};const n="MDXContent",i=t,e=i+34;let o=`${s.slice(0,i)}function ${n}${s.slice(e)}`;/import\s+{[^}]*\bmount\b[^}]*}\s+from\s+['"]lithent['"]/.test(o)||(o=M(o,`import { mount } from 'lithent';
2
+ `));const m=`__LithentMdxComponent_${n}`,$=`
3
+ ${f}
4
+ const ${m} = mount((_renew, props) => {
5
5
  return () => ${n}(props ?? {});
6
6
  });
7
- export default ${a};
8
- `;return u=`${u}${$}`,{code:u,modified:!0}},m=e=>{const t=(n,i)=>n.replace(new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${i}`,"gm"),(s,u)=>s.trimStart().startsWith("export ")?s:`${u}export function ${i}`),p=(n,i)=>n.replace(new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${i}`,"gm"),(s,u)=>s.trimStart().startsWith("export default")?s:`${u}export default function ${i}`),d=t(e,"_createMdxContent");return p(d,"MDXContent")},h=(e,t)=>{const p=/^(import[\s\S]*?from\s+['"][^'"]+['"]\s*;[ \t]*\n?)/gm;let d=null,r;for(;r=p.exec(e);)d=r;if(d){const n=d.index+d[0].length;return`${e.slice(0,n)}${t}${e.slice(n)}`}return`${t}${e}`},x=(e={})=>{const t=e.jsxImportSource??"lithent";return[l({jsxImportSource:t,...e.mdxOptions}),{name:"lithent:mdx-wrapper",enforce:void 0,transform(r,n){const[i]=n.split("?",1);if(!/\.mdx$/i.test(i))return null;const s=f(r);return s.modified?{code:s.code,map:null}:null}}]};o.MDX_WRAP_SENTINEL=c,o.default=x,o.lithentMdx=x,o.wrapMdxModule=f,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
7
+ export default ${m};
8
+ `;return o=`${o}${$}`,{code:o,modified:!0}},x=r=>{const s="export default function MDXContent",u=r.length;let t=0,n="code",i=0;for(;t<u;){const e=r[t],o=r[t+1];switch(n){case"code":if(e==="/"){if(o==="/"){n="lineComment",t+=2;continue}if(o==="*"){n="blockComment",t+=2;continue}}if(e==="'"){n="single",t++;continue}if(e==='"'){n="double",t++;continue}if(e==="`"){n="template",i=0,t++;continue}if(r.startsWith(s,t))return t;t++;break;case"single":if(e==="\\"){t+=2;continue}e==="'"&&(n="code"),t++;break;case"double":if(e==="\\"){t+=2;continue}e==='"'&&(n="code"),t++;break;case"template":if(e==="\\"){t+=2;continue}if(e==="`"){if(i===0){n="code",t++;break}i--,t++;break}if(e==="$"&&o==="{"){i++,t+=2;break}if(e==="}"){i>0&&i--,t++;break}t++;break;case"lineComment":e===`
9
+ `&&(n="code"),t++;break;case"blockComment":if(e==="*"&&o==="/"){n="code",t+=2;break}t++;break}}return-1},h=r=>{const s=(i,e)=>i.replace(new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${e}`,"gm"),(o,d)=>o.trimStart().startsWith("export ")?o:`${d}export function ${e}`),u=(i,e)=>i.replace(new RegExp(`^(\\s*)(?:export\\s+)?function\\s+${e}`,"gm"),(o,d)=>o.trimStart().startsWith("export default")?o:`${d}export default function ${e}`),t=s(r,"_createMdxContent");return u(t,"MDXContent")},M=(r,s)=>{const u=/^(import[\s\S]*?from\s+['"][^'"]+['"]\s*;[ \t]*\n?)/gm;let t=null,n;for(;n=u.exec(r);)t=n;if(t){const i=t.index+t[0].length;return`${r.slice(0,i)}${s}${r.slice(i)}`}return`${s}${r}`},a=(r={})=>{const s=r.jsxImportSource??"lithent";return[c({jsxImportSource:s,...r.mdxOptions}),{name:"lithent:mdx-wrapper",enforce:void 0,transform(n,i){const[e]=i.split("?",1);if(!/\.mdx$/i.test(e))return null;const o=p(n);return o.modified?{code:o.code,map:null}:null}}]};l.MDX_WRAP_SENTINEL=f,l.default=a,l.lithentMdx=a,l.wrapMdxModule=p,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
9
10
  //# sourceMappingURL=index.umd.js.map
@@ -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 defaultExportRegex = /export\\s+default\\s+function\\s+([A-Za-z0-9_]+)/m;\n const defaultExportMatch = defaultExportRegex.exec(fixedExports);\n\n if (!defaultExportMatch || defaultExportMatch.index === undefined) {\n return { code: fixedExports, modified: exportsModified };\n }\n\n const fnName = defaultExportMatch[1];\n\n const start = defaultExportMatch.index;\n const end = start + defaultExportMatch[0].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 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","defaultExportMatch","fnName","start","end","updated","insertImportString","wrapperVar","wrapperSnippet","ensureNamedExport","source","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,EAInD,MAAAC,EADqB,iDACmB,KAAKH,CAAY,EAE/D,GAAI,CAACG,GAAsBA,EAAmB,QAAU,OACtD,MAAO,CAAE,KAAMH,EAAc,SAAUE,CAAgB,EAGnD,MAAAE,EAASD,EAAmB,CAAC,EAE7BE,EAAQF,EAAmB,MAC3BG,EAAMD,EAAQF,EAAmB,CAAC,EAAE,OAC1C,IAAII,EAAU,GAAGP,EAAa,MAAM,EAAGK,CAAK,CAAC,YAAYD,CAAM,GAAGJ,EAAa,MAAMM,CAAG,CAAC,GAIvF,0DAA0D,KAAKC,CAAO,IAGtEA,EAAUC,EAAmBD,EAAS;AAAA,CAAoC,GAGtE,MAAAE,EAAa,yBAAyBL,CAAM,GAC5CM,EAAiB;AAAA,EACvBb,CAAiB;AAAA,QACXY,CAAU;AAAA,iBACD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G,MAAM,IAAK,CAAC,EAGlC,GAAI,CAAC,UAAU,KAAKC,CAAQ,EACnB,OAAA,KAIH,MAAAC,EAAS7B,EAAcC,CAAI,EAE7B,OAAC4B,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 =\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;AAAA,CAAoC,GAGtE,MAAAE,EAAa,yBAAyBL,CAAM,GAC5CM,EAAiB;AAAA,EACvBd,CAAiB;AAAA,QACXa,CAAU;AAAA,iBACDL,CAAM;AAAA;AAAA,iBAENK,CAAU;AAAA,EAGf,OAAAF,EAAA,GAAGA,CAAO,GAAGG,CAAc,GAE9B,CAAE,KAAMH,EAAS,SAAU,EAAK,CACzC,EAEMJ,EAA6BQ,GAA2B,CAC5D,MAAMC,EAAS,qCACTC,EAASF,EAAO,OACtB,IAAIG,EAAI,EACJC,EAMiB,OACjBC,EAAgB,EAEpB,KAAOF,EAAID,GAAQ,CACX,MAAAI,EAAKN,EAAOG,CAAC,EACbI,EAAOP,EAAOG,EAAI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
4
- "version": "0.2.0",
4
+ "version": "0.2.2",
5
5
  "description": "Vite plugin for MDX support in Lithent applications",
6
- "main": "./dist/index.umd.js",
6
+ "main": "./dist/index.mjs",
7
7
  "module": "./dist/index.mjs",
8
8
  "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
13
+ "require": "./dist/index.umd.js"
14
+ }
15
+ },
9
16
  "dependencies": {
10
17
  "@mdx-js/rollup": "^3.1.0"
11
18
  },
12
19
  "devDependencies": {
13
20
  "vite": "^5.4.8",
14
- "@lithent/hmr-parser": "0.2.0",
15
- "lithent": "1.20.0"
21
+ "lithent": "1.20.0",
22
+ "@lithent/hmr-parser": "0.2.0"
16
23
  },
17
24
  "keywords": [
18
25
  "lithent",
@@ -79,4 +79,26 @@ export default function MDXContent(props) {
79
79
  expect(result.code).toContain('import { mount } from \'lithent\';');
80
80
  expect(result.code).toContain('export default __LithentMdxComponent_MDXContent');
81
81
  });
82
+
83
+ it('should ignore MDXContent pattern inside strings and templates', () => {
84
+ const example = `
85
+ const exampleString = "export default function MDXContent() { return null; }";
86
+ const exampleTemplate = \`/* export default function MDXContent() */\`;
87
+
88
+ export function _createMdxContent(props) {
89
+ return <div>{props.children}</div>;
90
+ }
91
+ export default function MDXContent(props) {
92
+ return _createMdxContent(props);
93
+ }
94
+ `;
95
+
96
+ const result = wrapMdxModule(example);
97
+ const wrappedName = '__LithentMdxComponent_MDXContent';
98
+
99
+ expect(result.modified).toBe(true);
100
+ expect(result.code).toContain(`const ${wrappedName} = mount`);
101
+ expect(result.code).toContain(`export default ${wrappedName};`);
102
+ expect(result.code).not.toContain('__LithentMdxComponent__LithentMdxComponent_MDXContent');
103
+ });
82
104
  });
package/src/transform.ts CHANGED
@@ -13,17 +13,16 @@ export const wrapMdxModule = (code: string): MdxWrapResult => {
13
13
  return { code: fixedExports, modified: exportsModified };
14
14
  }
15
15
 
16
- const defaultExportRegex = /export\s+default\s+function\s+([A-Za-z0-9_]+)/m;
17
- const defaultExportMatch = defaultExportRegex.exec(fixedExports);
16
+ const defaultExportIndex = findDefaultMdxExportIndex(fixedExports);
18
17
 
19
- if (!defaultExportMatch || defaultExportMatch.index === undefined) {
18
+ if (defaultExportIndex < 0) {
20
19
  return { code: fixedExports, modified: exportsModified };
21
20
  }
22
21
 
23
- const fnName = defaultExportMatch[1];
22
+ const fnName = 'MDXContent';
24
23
 
25
- const start = defaultExportMatch.index;
26
- const end = start + defaultExportMatch[0].length;
24
+ const start = defaultExportIndex;
25
+ const end = start + 'export default function MDXContent'.length;
27
26
  let updated = `${fixedExports.slice(0, start)}function ${fnName}${fixedExports.slice(end)}`;
28
27
 
29
28
  // Ensure mount import exists
@@ -48,6 +47,132 @@ export default ${wrapperVar};
48
47
  return { code: updated, modified: true };
49
48
  };
50
49
 
50
+ const findDefaultMdxExportIndex = (source: string): number => {
51
+ const target = 'export default function MDXContent';
52
+ const length = source.length;
53
+ let i = 0;
54
+ let state:
55
+ | 'code'
56
+ | 'single'
57
+ | 'double'
58
+ | 'template'
59
+ | 'lineComment'
60
+ | 'blockComment' = 'code';
61
+ let templateDepth = 0;
62
+
63
+ while (i < length) {
64
+ const ch = source[i];
65
+ const next = source[i + 1];
66
+
67
+ switch (state) {
68
+ case 'code':
69
+ if (ch === '/') {
70
+ if (next === '/') {
71
+ state = 'lineComment';
72
+ i += 2;
73
+ continue;
74
+ }
75
+ if (next === '*') {
76
+ state = 'blockComment';
77
+ i += 2;
78
+ continue;
79
+ }
80
+ }
81
+ if (ch === "'") {
82
+ state = 'single';
83
+ i++;
84
+ continue;
85
+ }
86
+ if (ch === '"') {
87
+ state = 'double';
88
+ i++;
89
+ continue;
90
+ }
91
+ if (ch === '`') {
92
+ state = 'template';
93
+ templateDepth = 0;
94
+ i++;
95
+ continue;
96
+ }
97
+ if (source.startsWith(target, i)) {
98
+ return i;
99
+ }
100
+ i++;
101
+ break;
102
+
103
+ case 'single':
104
+ if (ch === '\\') {
105
+ i += 2;
106
+ continue;
107
+ }
108
+ if (ch === "'") {
109
+ state = 'code';
110
+ }
111
+ i++;
112
+ break;
113
+
114
+ case 'double':
115
+ if (ch === '\\') {
116
+ i += 2;
117
+ continue;
118
+ }
119
+ if (ch === '"') {
120
+ state = 'code';
121
+ }
122
+ i++;
123
+ break;
124
+
125
+ case 'template':
126
+ if (ch === '\\') {
127
+ i += 2;
128
+ continue;
129
+ }
130
+ if (ch === '`') {
131
+ if (templateDepth === 0) {
132
+ state = 'code';
133
+ i++;
134
+ break;
135
+ }
136
+ templateDepth--;
137
+ i++;
138
+ break;
139
+ }
140
+ if (ch === '$' && next === '{') {
141
+ templateDepth++;
142
+ i += 2;
143
+ break;
144
+ }
145
+ if (ch === '}') {
146
+ if (templateDepth > 0) {
147
+ templateDepth--;
148
+ }
149
+ i++;
150
+ break;
151
+ }
152
+ i++;
153
+ break;
154
+
155
+ case 'lineComment':
156
+ if (ch === '\n') {
157
+ state = 'code';
158
+ }
159
+ i++;
160
+ break;
161
+
162
+ case 'blockComment':
163
+ if (ch === '*' && next === '/') {
164
+ state = 'code';
165
+ i += 2;
166
+ break;
167
+ }
168
+ i++;
169
+ break;
170
+ }
171
+ }
172
+
173
+ return -1;
174
+ };
175
+
51
176
  const wrapMdxExports = (code: string): string => {
52
177
  const ensureNamedExport = (source: string, fnName: string): string =>
53
178
  source.replace(