@temelj/mdx 0.13.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (369) hide show
  1. package/README.md +468 -82
  2. package/dist/character-entities.d.mts +4 -0
  3. package/dist/character-entities.d.mts.map +1 -0
  4. package/dist/character-entities.mjs +2132 -0
  5. package/dist/character-entities.mjs.map +1 -0
  6. package/dist/character-reference.d.mts +6 -0
  7. package/dist/character-reference.d.mts.map +1 -0
  8. package/dist/character-reference.mjs +18 -0
  9. package/dist/character-reference.mjs.map +1 -0
  10. package/dist/compile-analysis.d.mts +28 -0
  11. package/dist/compile-analysis.d.mts.map +1 -0
  12. package/dist/compile-analysis.mjs +74 -0
  13. package/dist/compile-analysis.mjs.map +1 -0
  14. package/dist/compile-emit.d.mts +9 -0
  15. package/dist/compile-emit.d.mts.map +1 -0
  16. package/dist/compile-emit.mjs +187 -0
  17. package/dist/compile-emit.mjs.map +1 -0
  18. package/dist/compile-esm.d.mts +34 -0
  19. package/dist/compile-esm.d.mts.map +1 -0
  20. package/dist/compile-esm.mjs +187 -0
  21. package/dist/compile-esm.mjs.map +1 -0
  22. package/dist/compile-names.d.mts +13 -0
  23. package/dist/compile-names.d.mts.map +1 -0
  24. package/dist/compile-names.mjs +32 -0
  25. package/dist/compile-names.mjs.map +1 -0
  26. package/dist/compiler.d.mts +69 -85
  27. package/dist/compiler.d.mts.map +1 -1
  28. package/dist/compiler.mjs +185 -418
  29. package/dist/compiler.mjs.map +1 -1
  30. package/dist/diagnostics.d.mts +37 -0
  31. package/dist/diagnostics.d.mts.map +1 -0
  32. package/dist/diagnostics.mjs +57 -0
  33. package/dist/diagnostics.mjs.map +1 -0
  34. package/dist/errors.d.mts +157 -0
  35. package/dist/errors.d.mts.map +1 -0
  36. package/dist/errors.mjs +263 -0
  37. package/dist/errors.mjs.map +1 -0
  38. package/dist/footnote-numbering.d.mts +27 -0
  39. package/dist/footnote-numbering.d.mts.map +1 -0
  40. package/dist/footnote-numbering.mjs +49 -0
  41. package/dist/footnote-numbering.mjs.map +1 -0
  42. package/dist/format-context.d.mts +46 -0
  43. package/dist/format-context.d.mts.map +1 -0
  44. package/dist/format-context.mjs +26 -0
  45. package/dist/format-context.mjs.map +1 -0
  46. package/dist/format-directive.d.mts +9 -0
  47. package/dist/format-directive.d.mts.map +1 -0
  48. package/dist/format-directive.mjs +99 -0
  49. package/dist/format-directive.mjs.map +1 -0
  50. package/dist/format-jsx.d.mts +8 -0
  51. package/dist/format-jsx.d.mts.map +1 -0
  52. package/dist/format-jsx.mjs +47 -0
  53. package/dist/format-jsx.mjs.map +1 -0
  54. package/dist/format-math.d.mts +9 -0
  55. package/dist/format-math.d.mts.map +1 -0
  56. package/dist/format-math.mjs +79 -0
  57. package/dist/format-math.mjs.map +1 -0
  58. package/dist/format-safe.d.mts +25 -0
  59. package/dist/format-safe.d.mts.map +1 -0
  60. package/dist/format-safe.mjs +366 -0
  61. package/dist/format-safe.mjs.map +1 -0
  62. package/dist/format-table.d.mts +8 -0
  63. package/dist/format-table.d.mts.map +1 -0
  64. package/dist/format-table.mjs +55 -0
  65. package/dist/format-table.mjs.map +1 -0
  66. package/dist/format-track.d.mts +18 -0
  67. package/dist/format-track.d.mts.map +1 -0
  68. package/dist/format-track.mjs +34 -0
  69. package/dist/format-track.mjs.map +1 -0
  70. package/dist/format.d.mts +7 -0
  71. package/dist/format.d.mts.map +1 -0
  72. package/dist/format.mjs +549 -0
  73. package/dist/format.mjs.map +1 -0
  74. package/dist/frontmatter.d.mts +15 -0
  75. package/dist/frontmatter.d.mts.map +1 -0
  76. package/dist/frontmatter.mjs +187 -0
  77. package/dist/frontmatter.mjs.map +1 -0
  78. package/dist/heading.d.mts +6 -0
  79. package/dist/heading.d.mts.map +1 -0
  80. package/dist/heading.mjs +19 -0
  81. package/dist/heading.mjs.map +1 -0
  82. package/dist/html-format.d.mts +32 -0
  83. package/dist/html-format.d.mts.map +1 -0
  84. package/dist/html-format.mjs +127 -0
  85. package/dist/html-format.mjs.map +1 -0
  86. package/dist/html-lower.d.mts +78 -0
  87. package/dist/html-lower.d.mts.map +1 -0
  88. package/dist/html-lower.mjs +583 -0
  89. package/dist/html-lower.mjs.map +1 -0
  90. package/dist/html-stream.d.mts +13 -0
  91. package/dist/html-stream.d.mts.map +1 -0
  92. package/dist/html-stream.mjs +32 -0
  93. package/dist/html-stream.mjs.map +1 -0
  94. package/dist/html.d.mts +120 -0
  95. package/dist/html.d.mts.map +1 -0
  96. package/dist/html.mjs +77 -0
  97. package/dist/html.mjs.map +1 -0
  98. package/dist/identifier.d.mts +4 -0
  99. package/dist/identifier.d.mts.map +1 -0
  100. package/dist/identifier.mjs +8 -0
  101. package/dist/identifier.mjs.map +1 -0
  102. package/dist/javascript-ast.d.mts +16 -0
  103. package/dist/javascript-ast.d.mts.map +1 -0
  104. package/dist/javascript-ast.mjs +39 -0
  105. package/dist/javascript-ast.mjs.map +1 -0
  106. package/dist/javascript-jsx.d.mts +24 -0
  107. package/dist/javascript-jsx.d.mts.map +1 -0
  108. package/dist/javascript-jsx.mjs +189 -0
  109. package/dist/javascript-jsx.mjs.map +1 -0
  110. package/dist/javascript.d.mts +9 -0
  111. package/dist/javascript.d.mts.map +1 -0
  112. package/dist/javascript.mjs +31 -0
  113. package/dist/javascript.mjs.map +1 -0
  114. package/dist/jsx-reference.d.mts +15 -0
  115. package/dist/jsx-reference.d.mts.map +1 -0
  116. package/dist/jsx-reference.mjs +31 -0
  117. package/dist/jsx-reference.mjs.map +1 -0
  118. package/dist/mod.d.mts +25 -8
  119. package/dist/mod.mjs +19 -7
  120. package/dist/model.d.mts +196 -0
  121. package/dist/model.d.mts.map +1 -0
  122. package/dist/model.mjs +113 -0
  123. package/dist/model.mjs.map +1 -0
  124. package/dist/parser/block-container-recognizers.d.mts +40 -0
  125. package/dist/parser/block-container-recognizers.d.mts.map +1 -0
  126. package/dist/parser/block-container-recognizers.mjs +90 -0
  127. package/dist/parser/block-container-recognizers.mjs.map +1 -0
  128. package/dist/parser/block-directive-recognizers.d.mts +22 -0
  129. package/dist/parser/block-directive-recognizers.d.mts.map +1 -0
  130. package/dist/parser/block-directive-recognizers.mjs +77 -0
  131. package/dist/parser/block-directive-recognizers.mjs.map +1 -0
  132. package/dist/parser/block-leaf-recognizers.d.mts +53 -0
  133. package/dist/parser/block-leaf-recognizers.d.mts.map +1 -0
  134. package/dist/parser/block-leaf-recognizers.mjs +386 -0
  135. package/dist/parser/block-leaf-recognizers.mjs.map +1 -0
  136. package/dist/parser/block-parser.d.mts +38 -0
  137. package/dist/parser/block-parser.d.mts.map +1 -0
  138. package/dist/parser/block-parser.mjs +895 -0
  139. package/dist/parser/block-parser.mjs.map +1 -0
  140. package/dist/parser/block-syntax.d.mts +240 -0
  141. package/dist/parser/block-syntax.d.mts.map +1 -0
  142. package/dist/parser/characters.d.mts +19 -0
  143. package/dist/parser/characters.d.mts.map +1 -0
  144. package/dist/parser/characters.mjs +52 -0
  145. package/dist/parser/characters.mjs.map +1 -0
  146. package/dist/parser/config.d.mts +16 -0
  147. package/dist/parser/config.d.mts.map +1 -0
  148. package/dist/parser/config.mjs +25 -0
  149. package/dist/parser/config.mjs.map +1 -0
  150. package/dist/parser/directive-attributes.d.mts +9 -0
  151. package/dist/parser/directive-attributes.d.mts.map +1 -0
  152. package/dist/parser/directive-attributes.mjs +24 -0
  153. package/dist/parser/directive-attributes.mjs.map +1 -0
  154. package/dist/parser/embedded-scanner.d.mts +18 -0
  155. package/dist/parser/embedded-scanner.d.mts.map +1 -0
  156. package/dist/parser/embedded-scanner.mjs +199 -0
  157. package/dist/parser/embedded-scanner.mjs.map +1 -0
  158. package/dist/parser/inline-autolink.d.mts +7 -0
  159. package/dist/parser/inline-autolink.d.mts.map +1 -0
  160. package/dist/parser/inline-autolink.mjs +42 -0
  161. package/dist/parser/inline-autolink.mjs.map +1 -0
  162. package/dist/parser/inline-character-reference.d.mts +7 -0
  163. package/dist/parser/inline-character-reference.d.mts.map +1 -0
  164. package/dist/parser/inline-character-reference.mjs +22 -0
  165. package/dist/parser/inline-character-reference.mjs.map +1 -0
  166. package/dist/parser/inline-code.d.mts +7 -0
  167. package/dist/parser/inline-code.d.mts.map +1 -0
  168. package/dist/parser/inline-code.mjs +29 -0
  169. package/dist/parser/inline-code.mjs.map +1 -0
  170. package/dist/parser/inline-directive.d.mts +9 -0
  171. package/dist/parser/inline-directive.d.mts.map +1 -0
  172. package/dist/parser/inline-directive.mjs +45 -0
  173. package/dist/parser/inline-directive.mjs.map +1 -0
  174. package/dist/parser/inline-embedded.d.mts +19 -0
  175. package/dist/parser/inline-embedded.d.mts.map +1 -0
  176. package/dist/parser/inline-embedded.mjs +92 -0
  177. package/dist/parser/inline-embedded.mjs.map +1 -0
  178. package/dist/parser/inline-emphasis.d.mts +11 -0
  179. package/dist/parser/inline-emphasis.d.mts.map +1 -0
  180. package/dist/parser/inline-emphasis.mjs +79 -0
  181. package/dist/parser/inline-emphasis.mjs.map +1 -0
  182. package/dist/parser/inline-input.d.mts +46 -0
  183. package/dist/parser/inline-input.d.mts.map +1 -0
  184. package/dist/parser/inline-input.mjs +137 -0
  185. package/dist/parser/inline-input.mjs.map +1 -0
  186. package/dist/parser/inline-link.d.mts +12 -0
  187. package/dist/parser/inline-link.d.mts.map +1 -0
  188. package/dist/parser/inline-link.mjs +187 -0
  189. package/dist/parser/inline-link.mjs.map +1 -0
  190. package/dist/parser/inline-math.d.mts +10 -0
  191. package/dist/parser/inline-math.d.mts.map +1 -0
  192. package/dist/parser/inline-math.mjs +46 -0
  193. package/dist/parser/inline-math.mjs.map +1 -0
  194. package/dist/parser/inline-output.d.mts +34 -0
  195. package/dist/parser/inline-output.d.mts.map +1 -0
  196. package/dist/parser/inline-output.mjs +62 -0
  197. package/dist/parser/inline-output.mjs.map +1 -0
  198. package/dist/parser/inline-parser.d.mts +14 -0
  199. package/dist/parser/inline-parser.d.mts.map +1 -0
  200. package/dist/parser/inline-parser.mjs +328 -0
  201. package/dist/parser/inline-parser.mjs.map +1 -0
  202. package/dist/parser/inline-pending.d.mts +23 -0
  203. package/dist/parser/inline-pending.d.mts.map +1 -0
  204. package/dist/parser/inline-pending.mjs +100 -0
  205. package/dist/parser/inline-pending.mjs.map +1 -0
  206. package/dist/parser/inline-syntax.d.mts +118 -0
  207. package/dist/parser/inline-syntax.d.mts.map +1 -0
  208. package/dist/parser/inline-syntax.mjs +1 -0
  209. package/dist/parser/javascript-boundary.d.mts +16 -0
  210. package/dist/parser/javascript-boundary.d.mts.map +1 -0
  211. package/dist/parser/javascript-boundary.mjs +42 -0
  212. package/dist/parser/javascript-boundary.mjs.map +1 -0
  213. package/dist/parser/javascript-scanner.d.mts +22 -0
  214. package/dist/parser/javascript-scanner.d.mts.map +1 -0
  215. package/dist/parser/javascript-scanner.mjs +122 -0
  216. package/dist/parser/javascript-scanner.mjs.map +1 -0
  217. package/dist/parser/materialize-cache.d.mts +34 -0
  218. package/dist/parser/materialize-cache.d.mts.map +1 -0
  219. package/dist/parser/materialize-cache.mjs +12 -0
  220. package/dist/parser/materialize-cache.mjs.map +1 -0
  221. package/dist/parser/materialize-jsx.d.mts +14 -0
  222. package/dist/parser/materialize-jsx.d.mts.map +1 -0
  223. package/dist/parser/materialize-jsx.mjs +76 -0
  224. package/dist/parser/materialize-jsx.mjs.map +1 -0
  225. package/dist/parser/materialize.d.mts +61 -0
  226. package/dist/parser/materialize.d.mts.map +1 -0
  227. package/dist/parser/materialize.mjs +736 -0
  228. package/dist/parser/materialize.mjs.map +1 -0
  229. package/dist/parser/recognizer.d.mts +20 -0
  230. package/dist/parser/recognizer.d.mts.map +1 -0
  231. package/dist/parser/recognizer.mjs +24 -0
  232. package/dist/parser/recognizer.mjs.map +1 -0
  233. package/dist/parser/session.d.mts +42 -0
  234. package/dist/parser/session.d.mts.map +1 -0
  235. package/dist/parser/session.mjs +89 -0
  236. package/dist/parser/session.mjs.map +1 -0
  237. package/dist/parser.d.mts +25 -0
  238. package/dist/parser.d.mts.map +1 -0
  239. package/dist/parser.mjs +37 -0
  240. package/dist/parser.mjs.map +1 -0
  241. package/dist/plugin-internal.d.mts +25 -0
  242. package/dist/plugin-internal.d.mts.map +1 -0
  243. package/dist/plugin-internal.mjs +99 -0
  244. package/dist/plugin-internal.mjs.map +1 -0
  245. package/dist/plugin.d.mts +37 -0
  246. package/dist/plugin.d.mts.map +1 -0
  247. package/dist/plugin.mjs +1 -0
  248. package/dist/plugins/code-block-metadata.d.mts +42 -0
  249. package/dist/plugins/code-block-metadata.d.mts.map +1 -0
  250. package/dist/plugins/code-block-metadata.mjs +133 -0
  251. package/dist/plugins/code-block-metadata.mjs.map +1 -0
  252. package/dist/plugins/heading-id.d.mts +15 -0
  253. package/dist/plugins/heading-id.d.mts.map +1 -0
  254. package/dist/plugins/heading-id.mjs +45 -0
  255. package/dist/plugins/heading-id.mjs.map +1 -0
  256. package/dist/plugins/lower-heading.d.mts +16 -0
  257. package/dist/plugins/lower-heading.d.mts.map +1 -0
  258. package/dist/plugins/lower-heading.mjs +28 -0
  259. package/dist/plugins/lower-heading.mjs.map +1 -0
  260. package/dist/plugins/math-render.d.mts +26 -0
  261. package/dist/plugins/math-render.d.mts.map +1 -0
  262. package/dist/plugins/math-render.mjs +51 -0
  263. package/dist/plugins/math-render.mjs.map +1 -0
  264. package/dist/plugins/mod.d.mts +9 -5
  265. package/dist/plugins/mod.mjs +8 -5
  266. package/dist/plugins/remove-esm.d.mts +10 -0
  267. package/dist/plugins/remove-esm.d.mts.map +1 -0
  268. package/dist/plugins/remove-esm.mjs +12 -0
  269. package/dist/plugins/remove-esm.mjs.map +1 -0
  270. package/dist/plugins/shiki.d.mts +21 -0
  271. package/dist/plugins/shiki.d.mts.map +1 -0
  272. package/dist/plugins/shiki.mjs +144 -0
  273. package/dist/plugins/shiki.mjs.map +1 -0
  274. package/dist/plugins/syntax-highlight.d.mts +38 -0
  275. package/dist/plugins/syntax-highlight.d.mts.map +1 -0
  276. package/dist/plugins/syntax-highlight.mjs +106 -0
  277. package/dist/plugins/syntax-highlight.mjs.map +1 -0
  278. package/dist/processor.d.mts +60 -0
  279. package/dist/processor.d.mts.map +1 -0
  280. package/dist/processor.mjs +106 -0
  281. package/dist/processor.mjs.map +1 -0
  282. package/dist/ranges.d.mts +10 -0
  283. package/dist/ranges.d.mts.map +1 -0
  284. package/dist/ranges.mjs +22 -0
  285. package/dist/ranges.mjs.map +1 -0
  286. package/dist/source-map.d.mts +28 -0
  287. package/dist/source-map.d.mts.map +1 -0
  288. package/dist/source-map.mjs +90 -0
  289. package/dist/source-map.mjs.map +1 -0
  290. package/dist/source-text.d.mts +35 -0
  291. package/dist/source-text.d.mts.map +1 -0
  292. package/dist/source-text.mjs +132 -0
  293. package/dist/source-text.mjs.map +1 -0
  294. package/dist/source.d.mts +86 -0
  295. package/dist/source.d.mts.map +1 -0
  296. package/dist/source.mjs +264 -0
  297. package/dist/source.mjs.map +1 -0
  298. package/dist/stream-events.d.mts +59 -0
  299. package/dist/stream-events.d.mts.map +1 -0
  300. package/dist/stream-events.mjs +115 -0
  301. package/dist/stream-events.mjs.map +1 -0
  302. package/dist/stream-session.d.mts +79 -0
  303. package/dist/stream-session.d.mts.map +1 -0
  304. package/dist/stream-session.mjs +904 -0
  305. package/dist/stream-session.mjs.map +1 -0
  306. package/dist/stream-tail.d.mts +84 -0
  307. package/dist/stream-tail.d.mts.map +1 -0
  308. package/dist/stream-tail.mjs +359 -0
  309. package/dist/stream-tail.mjs.map +1 -0
  310. package/dist/stream-types.d.mts +89 -0
  311. package/dist/stream-types.d.mts.map +1 -0
  312. package/dist/stream-types.mjs +1 -0
  313. package/dist/stream.d.mts +28 -0
  314. package/dist/stream.d.mts.map +1 -0
  315. package/dist/stream.mjs +68 -0
  316. package/dist/stream.mjs.map +1 -0
  317. package/dist/syntax-options.d.mts +21 -0
  318. package/dist/syntax-options.d.mts.map +1 -0
  319. package/dist/syntax-options.mjs +26 -0
  320. package/dist/syntax-options.mjs.map +1 -0
  321. package/dist/traversal.d.mts +25 -0
  322. package/dist/traversal.d.mts.map +1 -0
  323. package/dist/traversal.mjs +136 -0
  324. package/dist/traversal.mjs.map +1 -0
  325. package/dist/tree.d.mts +18 -0
  326. package/dist/tree.d.mts.map +1 -0
  327. package/dist/tree.mjs +43 -0
  328. package/dist/tree.mjs.map +1 -0
  329. package/dist/unified/adapter.d.mts +24 -0
  330. package/dist/unified/adapter.d.mts.map +1 -0
  331. package/dist/unified/adapter.mjs +44 -0
  332. package/dist/unified/adapter.mjs.map +1 -0
  333. package/dist/unified/mod.d.mts +2 -0
  334. package/dist/unified/mod.mjs +1 -0
  335. package/dist/unified/runtime.d.mts +10 -0
  336. package/dist/unified/runtime.d.mts.map +1 -0
  337. package/dist/unified/runtime.mjs +115 -0
  338. package/dist/unified/runtime.mjs.map +1 -0
  339. package/dist/unified/tree-bridge.d.mts +63 -0
  340. package/dist/unified/tree-bridge.d.mts.map +1 -0
  341. package/dist/unified/tree-bridge.mjs +876 -0
  342. package/dist/unified/tree-bridge.mjs.map +1 -0
  343. package/dist/uri.d.mts +5 -0
  344. package/dist/uri.d.mts.map +1 -0
  345. package/dist/uri.mjs +32 -0
  346. package/dist/uri.mjs.map +1 -0
  347. package/package.json +56 -27
  348. package/dist/plugins/heading-id/plugin.d.mts +0 -17
  349. package/dist/plugins/heading-id/plugin.d.mts.map +0 -1
  350. package/dist/plugins/heading-id/plugin.mjs +0 -23
  351. package/dist/plugins/heading-id/plugin.mjs.map +0 -1
  352. package/dist/plugins/remove-imports-exports.d.mts +0 -10
  353. package/dist/plugins/remove-imports-exports.d.mts.map +0 -1
  354. package/dist/plugins/remove-imports-exports.mjs +0 -15
  355. package/dist/plugins/remove-imports-exports.mjs.map +0 -1
  356. package/dist/plugins/syntax-highlight/code-meta.d.mts +0 -22
  357. package/dist/plugins/syntax-highlight/code-meta.d.mts.map +0 -1
  358. package/dist/plugins/syntax-highlight/code-meta.mjs +0 -38
  359. package/dist/plugins/syntax-highlight/code-meta.mjs.map +0 -1
  360. package/dist/plugins/syntax-highlight/plugin.d.mts +0 -57
  361. package/dist/plugins/syntax-highlight/plugin.d.mts.map +0 -1
  362. package/dist/plugins/syntax-highlight/plugin.mjs +0 -93
  363. package/dist/plugins/syntax-highlight/plugin.mjs.map +0 -1
  364. package/dist/plugins/tree-processor/plugin.d.mts +0 -19
  365. package/dist/plugins/tree-processor/plugin.d.mts.map +0 -1
  366. package/dist/plugins/tree-processor/plugin.mjs +0 -21
  367. package/dist/plugins/tree-processor/plugin.mjs.map +0 -1
  368. package/dist/types.d.mts +0 -2
  369. /package/dist/{types.mjs → parser/block-syntax.mjs} +0 -0
package/dist/compiler.mjs CHANGED
@@ -1,441 +1,208 @@
1
- import { compile } from "@mdx-js/mdx";
2
- import { validateStandardSchema } from "@temelj/standard-schema";
3
- import remarkFrontmatterPlugin from "remark-frontmatter";
4
- import remarkGfmPlugin from "remark-gfm";
5
- import { VFile } from "vfile";
6
- import { matter } from "vfile-matter";
1
+ import { createDiagnostic } from "./diagnostics.mjs";
2
+ import { CompileError, ConfigurationError, FrontmatterError, LimitError, MdxError } from "./errors.mjs";
3
+ import { javascriptPropertyName } from "./javascript-ast.mjs";
4
+ import { parseJavaScriptProgram } from "./javascript.mjs";
5
+ import { analyzeDocument } from "./compile-analysis.mjs";
6
+ import { contentUsesAwait, emitContent } from "./compile-emit.mjs";
7
+ import { documentFrontmatter } from "./model.mjs";
8
+ import { lowerHtml } from "./html-lower.mjs";
9
+ import { CodeWriter } from "./source-map.mjs";
10
+ import { isBigInt, isBoolean, isNumber, isObject, isString } from "@temelj/value";
7
11
  //#region src/compiler.ts
8
- var MdxCompileError = class MdxCompileError extends Error {
9
- reason;
10
- source;
11
- ruleId;
12
- line;
13
- column;
14
- place;
15
- sourceLine;
16
- snippet;
17
- sourcePointer;
18
- hint;
19
- diagnostics;
20
- constructor(reason, options = {}, context) {
21
- super(formatDiagnosticMessage(reason, {
22
- source: options.source,
23
- ruleId: options.ruleId,
24
- line: options.line,
25
- column: options.column,
26
- sourceLine: options.sourceLine,
27
- snippet: options.snippet,
28
- sourcePointer: options.sourcePointer,
29
- hint: options.hint,
30
- causeMessage: options.causeMessage
31
- }), options.cause === void 0 ? void 0 : { cause: options.cause });
32
- this.name = "MdxCompileError";
33
- this.reason = reason;
34
- this.source = options.source;
35
- this.ruleId = options.ruleId;
36
- this.line = options.line;
37
- this.column = options.column;
38
- this.place = options.place;
39
- this.sourceLine = options.sourceLine;
40
- this.snippet = options.snippet;
41
- this.sourcePointer = options.sourcePointer;
42
- this.hint = options.hint;
43
- this.diagnostics = options.diagnostics ?? [];
44
- if (Error.captureStackTrace !== void 0) Error.captureStackTrace(this, context ?? this.constructor);
45
- }
46
- static create(reason, options = {}) {
47
- return new MdxCompileError(reason, options, MdxCompileError.create);
48
- }
49
- };
50
- /**
51
- * A reusable compiler for compiling MDX.
52
- */
53
- var MdxCompiler = class {
54
- mdxOptions;
55
- remarkPlugins;
56
- rehypePlugins;
57
- constructor(mdxOptions = {}) {
58
- this.mdxOptions = mdxOptions;
59
- this.remarkPlugins = [remarkFrontmatterPlugin, remarkGfmPlugin];
60
- this.rehypePlugins = [];
61
- }
62
- withRemarkPlugin(plugin, options) {
63
- if (options === void 0) this.remarkPlugins.push(plugin);
64
- else this.remarkPlugins.push([plugin, options]);
65
- return this;
66
- }
67
- withRehypePlugin(plugin, options) {
68
- if (options === void 0) this.rehypePlugins.push(plugin);
69
- else this.rehypePlugins.push([plugin, options]);
70
- return this;
71
- }
72
- async compile(source, { frontmatterOnly = false, mdxOptions } = {}, frontmatterSchema) {
73
- const sourceText = toSourceText(source);
74
- const vfile = new VFile({
75
- value: source,
76
- cwd: "/",
77
- path: "/source.mdx"
12
+ const DEFAULT_MAXIMUM_GENERATED_CODE_BYTES = 10485760;
13
+ const DEFAULT_MAXIMUM_SOURCE_MAP_BYTES = 5242880;
14
+ /** Emits JavaScript for a strict document. Throws for configuration errors and compiler limits. */
15
+ function emitArtifact(document, options, frontmatter) {
16
+ try {
17
+ if (options.maximumGeneratedCodeBytes !== void 0 && (!Number.isSafeInteger(options.maximumGeneratedCodeBytes) || options.maximumGeneratedCodeBytes < 0)) throw ConfigurationError.invalidCompilerLimit("maximumGeneratedCodeBytes", options.maximumGeneratedCodeBytes);
18
+ if (options.maximumSourceMapBytes !== void 0 && (!Number.isSafeInteger(options.maximumSourceMapBytes) || options.maximumSourceMapBytes < 0)) throw ConfigurationError.invalidCompilerLimit("maximumSourceMapBytes", options.maximumSourceMapBytes);
19
+ const content = lowerHtml(document, options);
20
+ const analysis = analyzeDocument(document, content, options);
21
+ const usesAwait = contentUsesAwait(content);
22
+ const format = options.outputFormat ?? "functionBody";
23
+ const writer = new CodeWriter(options.source);
24
+ emitModulePreamble(writer, analysis, options, format);
25
+ emitContentFunction(writer, content, analysis, options, usesAwait);
26
+ emitPublicComponent(writer, analysis, options, usesAwait, format);
27
+ if (analysis.scope.missingReferences.size > 0) emitMissingReferenceHelper(writer, analysis);
28
+ if (format === "functionBody") writer.write(`return {${[...analysis.esm.exports, `default: ${analysis.scope.names.get("Content")}`].join(", ")}};\n}\n`);
29
+ const code = writer.toString();
30
+ enforceGeneratedCodeLimit(options.source, code, options.maximumGeneratedCodeBytes);
31
+ try {
32
+ parseJavaScriptProgram(format === "program" ? code : `async function __evaluateMdx(){\n${code}\n}`);
33
+ } catch (cause) {
34
+ throw CompileError.generatedProgramInvalid(cause);
35
+ }
36
+ const map = writer.sourceMap(analysis.scope.sourceName, options.outputName);
37
+ enforceSourceMapLimit(options.source, map, options.maximumSourceMapBytes);
38
+ return format === "program" ? Object.freeze({
39
+ format,
40
+ code,
41
+ map,
42
+ frontmatter
43
+ }) : Object.freeze({
44
+ format,
45
+ code,
46
+ map,
47
+ imports: Object.freeze(options.providerImportSource === void 0 || analysis.esm.imports.includes(options.providerImportSource) ? [...analysis.esm.imports] : [...analysis.esm.imports, options.providerImportSource]),
48
+ frontmatter
78
49
  });
79
- matter(vfile, { strip: true });
80
- const sourceContext = createDiagnosticSourceContext(sourceText, toSourceText(vfile.value));
81
- const remarkPlugins = [
82
- ...this.remarkPlugins,
83
- ...this.mdxOptions?.remarkPlugins ?? [],
84
- ...mdxOptions?.remarkPlugins ?? []
85
- ];
86
- const rehypePlugins = [
87
- ...this.rehypePlugins,
88
- ...this.mdxOptions?.rehypePlugins ?? [],
89
- ...mdxOptions?.rehypePlugins ?? []
90
- ];
91
- let compiled;
92
- if (!frontmatterOnly) try {
93
- compiled = (await compile(vfile, {
94
- outputFormat: "function-body",
95
- ...this.mdxOptions,
96
- ...mdxOptions,
97
- remarkPlugins,
98
- rehypePlugins
99
- })).value.toString();
100
- } catch (error) {
101
- throw toMdxCompileError(error, sourceContext, vfile.messages);
50
+ } catch (cause) {
51
+ if (cause instanceof MdxError) throw cause;
52
+ throw CompileError.generationFailed(cause);
53
+ }
54
+ }
55
+ async function compile(input, options = {}) {
56
+ const frontmatterNode = documentFrontmatter(input.document);
57
+ let frontmatter = frontmatterNode?.data ?? Object.freeze({});
58
+ if (options.frontmatterSchema !== void 0) {
59
+ const validated = await options.frontmatterSchema["~standard"].validate(frontmatter);
60
+ if (validated.issues !== void 0) {
61
+ const diagnostics = validated.issues.map((issue) => createDiagnostic({
62
+ severity: "error",
63
+ code: "mdx.frontmatter.schema",
64
+ message: formatSchemaIssue(issue),
65
+ span: fileSpan(input.file, frontmatterNode?.origin)
66
+ }));
67
+ throw FrontmatterError.schemaValidationFailed(input.file, diagnostics, validated.issues);
102
68
  }
103
- const frontmatter = frontmatterSchema === void 0 ? vfile.data.matter ?? {} : await validateStandardSchema(frontmatterSchema, vfile.data.matter ?? {}, "Frontmatter validation failed");
104
- return {
105
- compiled,
106
- frontmatter,
107
- messages: normalizeMessages(vfile.messages, sourceContext)
108
- };
69
+ frontmatter = validated.value;
109
70
  }
110
- };
111
- function toMdxCompileError(error, sourceContext, diagnostics) {
112
- const details = getDiagnosticDetails(sourceContext, getErrorPlace(error), error);
113
- const cause = getErrorCause(error);
114
- return MdxCompileError.create(getErrorReason(error), {
115
- source: getErrorField(error, "source"),
116
- ruleId: getErrorField(error, "ruleId"),
117
- line: details.line,
118
- column: details.column,
119
- place: details.place,
120
- sourceLine: details.sourceLine,
121
- snippet: details.snippet,
122
- sourcePointer: details.sourcePointer,
123
- hint: details.hint,
124
- causeMessage: cause?.message,
125
- diagnostics: normalizeMessages(diagnostics, sourceContext),
126
- cause: error
71
+ const transported = copyTransportValue(frontmatter, "frontmatter", {
72
+ file: input.file,
73
+ origin: frontmatterNode?.origin,
74
+ ancestors: /* @__PURE__ */ new WeakSet()
127
75
  });
128
- }
129
- function normalizeMessages(messages, sourceContext) {
130
- return messages.map((message) => {
131
- const details = getDiagnosticDetails(sourceContext, toMessagePlace(message), message);
132
- const reason = getErrorReason(message);
133
- const source = getErrorField(message, "source");
134
- const ruleId = getErrorField(message, "ruleId");
135
- const cause = getErrorCause(message);
136
- return {
137
- message: formatDiagnosticMessage(reason, {
138
- source,
139
- ruleId,
140
- line: details.line,
141
- column: details.column,
142
- sourceLine: details.sourceLine,
143
- snippet: details.snippet,
144
- sourcePointer: details.sourcePointer,
145
- hint: details.hint,
146
- causeMessage: cause?.message
147
- }),
148
- reason,
149
- source,
150
- ruleId,
151
- fatal: typeof message.fatal === "boolean" ? message.fatal : void 0,
152
- line: details.line,
153
- column: details.column,
154
- place: details.place,
155
- sourceLine: details.sourceLine,
156
- snippet: details.snippet,
157
- sourcePointer: details.sourcePointer,
158
- hint: details.hint,
159
- cause
160
- };
76
+ const artifact = emitArtifact(input.document, {
77
+ ...options,
78
+ source: input.file
79
+ }, transported);
80
+ return Object.freeze({
81
+ file: input.file,
82
+ document: input.document,
83
+ diagnostics: input.diagnostics,
84
+ artifact,
85
+ frontmatter
161
86
  });
162
87
  }
163
- function formatDiagnosticMessage(reason, options) {
164
- const lines = [[
165
- reason,
166
- options.source === void 0 ? void 0 : options.ruleId === void 0 ? options.source : `${options.source}:${options.ruleId}`,
167
- options.line === void 0 ? void 0 : options.column === void 0 ? `at ${options.line}` : `at ${options.line}:${options.column}`
168
- ].filter(Boolean).join(" ")];
169
- if (options.snippet !== void 0) lines.push(`Source: ${options.snippet}`);
170
- if (options.sourceLine !== void 0 && options.sourceLine !== options.snippet) {
171
- lines.push(`Line: ${options.sourceLine}`);
172
- if (options.sourcePointer !== void 0) lines.push(` ${options.sourcePointer}`);
173
- }
174
- if (options.causeMessage !== void 0 && options.causeMessage !== "" && options.causeMessage !== reason) lines.push(`Cause: ${options.causeMessage}`);
175
- if (options.hint !== void 0) lines.push(`Hint: ${options.hint}`);
176
- return lines.join("\n");
177
- }
178
- function getDiagnosticDetails(sourceContext, place, error) {
179
- const compileLine = place?.start?.line ?? getErrorNumberField(error, "line");
180
- let line = compileLine === void 0 ? void 0 : compileLine + sourceContext.lineOffset;
181
- let column = place?.start?.column ?? getErrorNumberField(error, "column");
182
- let normalizedPlace = mapPlaceToOriginalSource(place === void 0 && compileLine === void 0 && column === void 0 ? void 0 : {
183
- start: {
184
- line: compileLine,
185
- column,
186
- offset: place?.start?.offset
187
- },
188
- end: place?.end === void 0 ? void 0 : {
189
- line: place.end.line,
190
- column: place.end.column,
191
- offset: place.end.offset
88
+ function formatSchemaIssue(issue) {
89
+ const path = issue.path?.map((part) => String(isObject(part) ? part.key : part)).join(".");
90
+ return path === void 0 || path.length === 0 ? issue.message : `${path}: ${issue.message}`;
91
+ }
92
+ function copyTransportValue(value, path, context) {
93
+ const { ancestors } = context;
94
+ if (value === null || isBoolean(value) || isNumber(value) || isBigInt(value) || isString(value)) return value;
95
+ if (!isObject(value)) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path} contains unsupported ${Object.prototype.toString.call(value)}`);
96
+ if (ancestors.has(value)) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path} contains a cycle`);
97
+ ancestors.add(value);
98
+ if (Array.isArray(value)) {
99
+ const output = [];
100
+ for (let index = 0; index < value.length; index++) {
101
+ const item = copyTransportValue(value[index], `${path}[${index}]`, context);
102
+ output.push(item);
192
103
  }
193
- }, sourceContext.lineOffset);
194
- let sourceLine = getSourceLine(sourceContext.originalSourceText, line);
195
- let snippet = getSourceSnippet(sourceContext.originalSourceText, normalizedPlace);
196
- let sourcePointer = getSourcePointer(column);
197
- let hint;
198
- const refinedExpressionContext = getMdxExpressionContext(sourceContext.originalSourceText, {
199
- line,
200
- column,
201
- sourceLine,
202
- snippet,
203
- error
204
- });
205
- if (refinedExpressionContext !== void 0) {
206
- line = refinedExpressionContext.line;
207
- column = refinedExpressionContext.column;
208
- sourceLine = refinedExpressionContext.sourceLine;
209
- snippet = refinedExpressionContext.snippet;
210
- sourcePointer = refinedExpressionContext.sourcePointer;
211
- hint = refinedExpressionContext.hint;
212
- normalizedPlace = {
213
- start: {
214
- line,
215
- column,
216
- offset: void 0
217
- },
218
- end: void 0
219
- };
220
- }
221
- return {
222
- line,
223
- column,
224
- place: normalizedPlace,
225
- sourceLine,
226
- snippet,
227
- sourcePointer,
228
- hint
229
- };
230
- }
231
- function getMdxExpressionContext(sourceText, options) {
232
- if (!shouldRefineMdxExpressionContext(options)) return;
233
- const candidate = findSuspiciousBraceExpression(sourceText, options.line, options.column);
234
- if (candidate === void 0) return;
235
- return {
236
- line: candidate.line,
237
- column: candidate.column,
238
- sourceLine: candidate.sourceLine,
239
- snippet: candidate.snippet,
240
- sourcePointer: getSourcePointer(candidate.column) ?? "^",
241
- hint: getErrorCause(options.error)?.message === "Unexpected content after expression" ? "MDX treats `{...}` as JavaScript. If you meant literal braces in text, escape them or wrap the text in code." : void 0
242
- };
243
- }
244
- function shouldRefineMdxExpressionContext(options) {
245
- const trimmedSourceLine = options.sourceLine?.trim();
246
- return isMdxExpressionError(options.error) && getErrorCause(options.error)?.message === "Unexpected content after expression" && (options.sourceLine === void 0 || options.sourceLine.trim() === "" || options.snippet === void 0) && !(trimmedSourceLine?.startsWith("{") && trimmedSourceLine.endsWith("}"));
247
- }
248
- function getSourceLine(sourceText, line) {
249
- if (line === void 0 || line < 1) return;
250
- return sourceText.split(/\r?\n/u)[line - 1];
251
- }
252
- function getSourceSnippet(sourceText, place) {
253
- if (place?.start?.offset !== void 0 && place.end?.offset !== void 0) return sourceText.slice(place.start.offset, place.end.offset) || void 0;
254
- if (place?.start?.line === void 0 || place.start.column === void 0) return;
255
- if (place.end === void 0) {
256
- const line = sourceText.split(/\r?\n/u)[place.start.line - 1];
257
- if (line === void 0) return;
258
- const character = line.at(place.start.column - 1);
259
- return character === void 0 || character.trim() === "" ? void 0 : character;
104
+ ancestors.delete(value);
105
+ return Object.freeze(output);
260
106
  }
261
- const lines = sourceText.split(/\r?\n/u);
262
- const startLineIndex = place.start.line - 1;
263
- const endLineIndex = (place.end?.line ?? place.start.line) - 1;
264
- if (startLineIndex < 0 || startLineIndex >= lines.length) return;
265
- const selectedLines = lines.slice(startLineIndex, endLineIndex + 1);
266
- if (selectedLines.length === 0) return;
267
- selectedLines[0] = selectedLines[0].slice(place.start.column - 1);
268
- const endColumn = place.end?.column;
269
- if (endColumn !== void 0) {
270
- const lastIndex = selectedLines.length - 1;
271
- selectedLines[lastIndex] = selectedLines[lastIndex].slice(0, endColumn - 1);
107
+ const prototype = Object.getPrototypeOf(value);
108
+ if (prototype !== Object.prototype && prototype !== null) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path} contains a non-plain object`);
109
+ if (Object.getOwnPropertySymbols(value).length > 0) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path} contains a symbol property`);
110
+ const output = {};
111
+ for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(value))) {
112
+ if (!descriptor.enumerable) continue;
113
+ if (!("value" in descriptor)) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path}.${key} is an accessor property`);
114
+ const item = copyTransportValue(descriptor.value, `${path}.${key}`, context);
115
+ Object.defineProperty(output, key, {
116
+ enumerable: true,
117
+ value: item
118
+ });
272
119
  }
273
- return selectedLines.join("\n") || void 0;
274
- }
275
- function getSourcePointer(column) {
276
- if (column === void 0 || column < 1) return;
277
- return `${" ".repeat(column - 1)}^`;
278
- }
279
- function mapPlaceToOriginalSource(place, lineOffset) {
280
- if (place === void 0) return;
281
- return {
282
- start: mapPointToOriginalSource(place.start, lineOffset),
283
- end: mapPointToOriginalSource(place.end, lineOffset)
120
+ ancestors.delete(value);
121
+ return Object.freeze(output);
122
+ }
123
+ function fileSpan(file, range) {
124
+ return range === void 0 ? void 0 : {
125
+ file,
126
+ start: range.start,
127
+ end: range.end
284
128
  };
285
129
  }
286
- function mapPointToOriginalSource(point, lineOffset) {
287
- if (point === void 0) return;
288
- return {
289
- line: point.line === void 0 ? void 0 : point.line + lineOffset,
290
- column: point.column,
291
- offset: lineOffset === 0 ? point.offset : void 0
292
- };
293
- }
294
- function findSuspiciousBraceExpression(sourceText, line, column) {
295
- const lines = sourceText.split(/\r?\n/u);
296
- const searchLines = getNearbyLineNumbers(line, lines.length, 2);
297
- let bestMatch;
298
- for (const lineNumber of searchLines) {
299
- const sourceLine = lines[lineNumber - 1];
300
- if (sourceLine === void 0 || sourceLine.trim() === "") continue;
301
- const expression = findLineBraceExpression(sourceLine, lineNumber, column);
302
- if (expression === void 0) continue;
303
- const score = (line === void 0 ? 0 : Math.abs(lineNumber - line)) * 10 + expression.distance;
304
- if (bestMatch === void 0 || score < bestMatch.score) bestMatch = {
305
- ...expression,
306
- score
307
- };
308
- }
309
- return bestMatch;
310
- }
311
- function getNearbyLineNumbers(line, totalLines, radius) {
312
- if (line === void 0) return Array.from({ length: totalLines }, (_, index) => index + 1);
313
- const lineNumbers = [];
314
- for (let distance = 0; distance <= radius; distance++) {
315
- const before = line - distance;
316
- if (before >= 1) lineNumbers.push(before);
317
- if (distance === 0) continue;
318
- const after = line + distance;
319
- if (after <= totalLines) lineNumbers.push(after);
320
- }
321
- return lineNumbers;
322
- }
323
- function findLineBraceExpression(sourceLine, line, column) {
324
- let bestMatch;
325
- for (const match of sourceLine.matchAll(/\{[^{}\n]+\}/gu)) {
326
- const braceStart = match.index;
327
- if (braceStart === void 0) continue;
328
- const expanded = expandBraceExpression(sourceLine, braceStart);
329
- if (expanded === void 0) continue;
330
- const distance = column === void 0 ? 0 : Math.abs(braceStart + 1 - column);
331
- if (bestMatch === void 0 || distance < bestMatch.distance) bestMatch = {
332
- line,
333
- column: braceStart + 1,
334
- sourceLine,
335
- snippet: expanded,
336
- distance
337
- };
338
- }
339
- return bestMatch;
340
- }
341
- function expandBraceExpression(sourceLine, braceStart) {
342
- const braceEnd = sourceLine.indexOf("}", braceStart);
343
- if (braceEnd === -1) return;
344
- let start = braceStart;
345
- while (start > 0 && /[A-Za-z0-9_]/u.test(sourceLine[start - 1])) start--;
346
- let end = braceEnd + 1;
347
- while (end < sourceLine.length) {
348
- while (end < sourceLine.length && /[A-Za-z0-9_]/u.test(sourceLine[end])) end++;
349
- if (sourceLine[end] !== "{") break;
350
- const chainedEnd = sourceLine.indexOf("}", end);
351
- if (chainedEnd === -1) break;
352
- end = chainedEnd + 1;
130
+ /**
131
+ * Authored ESM, the JSX runtime, and the optional provider. A function body
132
+ * receives the runtime as `__mdxRuntime` and resolves imports through
133
+ * `__resolveMdxImport` instead of importing.
134
+ */
135
+ function emitModulePreamble(writer, analysis, options, format) {
136
+ const names = analysis.scope.names;
137
+ const runtime = analysis.scope.development ? `{Fragment: ${names.get("_Fragment")}, jsxDEV: ${names.get("_jsxDEV")}}` : `{Fragment: ${names.get("_Fragment")}, jsx: ${names.get("_jsx")}, jsxs: ${names.get("_jsxs")}}`;
138
+ const importSource = `${options.jsxImportSource ?? "react"}/${analysis.scope.development ? "jsx-dev-runtime" : "jsx-runtime"}`;
139
+ writer.write(format === "program" ? `import ${runtime.replaceAll(": ", " as ")} from ${JSON.stringify(importSource)};\n` : `const ${runtime} = __mdxRuntime;\n`);
140
+ if (format === "functionBody") {
141
+ if (analysis.esm.imports.length > 0 || options.providerImportSource !== void 0) writer.write(`const ${names.get("_resolveMdxImport")} = __resolveMdxImport;\n`);
142
+ writer.write("{\n");
353
143
  }
354
- return sourceLine.slice(start, end) || void 0;
355
- }
356
- function isMdxExpressionError(error) {
357
- return getErrorField(error, "source") === "micromark-extension-mdx-expression" && getErrorField(error, "ruleId") === "acorn";
358
- }
359
- function createDiagnosticSourceContext(originalSourceText, compileSourceText) {
360
- return {
361
- originalSourceText,
362
- lineOffset: getSourceLineOffset(originalSourceText, compileSourceText)
363
- };
364
- }
365
- function getSourceLineOffset(originalSourceText, compileSourceText) {
366
- const originalLines = originalSourceText.split(/\r?\n/u);
367
- const compileLines = compileSourceText.split(/\r?\n/u);
368
- const maxOffset = originalLines.length - compileLines.length;
369
- for (let offset = 0; offset <= maxOffset; offset++) {
370
- let isMatch = true;
371
- for (let index = 0; index < compileLines.length; index++) if (originalLines[offset + index] !== compileLines[index]) {
372
- isMatch = false;
373
- break;
374
- }
375
- if (isMatch) return offset;
144
+ for (const entry of analysis.esm.blocks) writer.write(`${entry.code.trimEnd()}\n`, entry.origin);
145
+ if (options.providerImportSource !== void 0) writer.write(format === "program" ? `import {useMDXComponents as ${names.get("_provideComponents")}} from ${JSON.stringify(options.providerImportSource)};\n` : `const {useMDXComponents: ${names.get("_provideComponents")}} = await ${names.get("_resolveMdxImport")}(${JSON.stringify(options.providerImportSource)});\n`);
146
+ }
147
+ function emitContentFunction(writer, content, analysis, options, usesAwait) {
148
+ const names = analysis.scope.names;
149
+ const props = analysis.propsName;
150
+ const componentsName = names.get("_components");
151
+ writer.write(`${usesAwait ? "async " : ""}function ${names.get("_createContent")}(${props}) {\n`);
152
+ writer.write(` const ${componentsName} = {`);
153
+ const tags = [...analysis.intrinsicTags].sort();
154
+ tags.forEach((tag, index) => {
155
+ if (index > 0) writer.write(", ");
156
+ writer.write(`${javascriptPropertyName(tag)}:${JSON.stringify(tag)}`);
157
+ });
158
+ if (options.providerImportSource !== void 0) writer.write(`${tags.length === 0 ? "" : ", "}...${names.get("_provideComponents")}()`);
159
+ writer.write(`${tags.length === 0 && options.providerImportSource === void 0 ? "" : ", "}...${props}.components};\n`);
160
+ const components = [...analysis.scope.referencedComponents].sort();
161
+ for (const component of components) writer.write(` const ${component} = ${componentsName}[${JSON.stringify(component)}];\n`);
162
+ for (const [reference, component] of analysis.scope.missingReferences) writer.write(` if (!${reference}) ${names.get("_missingReference")}(${JSON.stringify(reference)}, ${component});\n`);
163
+ writer.write(" return ");
164
+ emitContent(writer, content, analysis);
165
+ writer.write(";\n}\n");
166
+ }
167
+ function emitPublicComponent(writer, analysis, options, usesAwait, format) {
168
+ const names = analysis.scope.names;
169
+ const props = analysis.propsName;
170
+ const layout = names.get("Layout");
171
+ writer.write(`${format === "program" ? "export default " : ""}${usesAwait ? "async " : ""}function ${names.get("Content")}(${props} = {}) {\n`);
172
+ const content = `${usesAwait ? "await " : ""}${names.get("_createContent")}(${props})`;
173
+ if (analysis.esm.hasLayout) writer.write(` return ${runtimeCall(analysis, layout, `{...${props}, children: ${content}}`)};\n`);
174
+ else {
175
+ writer.write(` const {wrapper: ${layout}} = {`);
176
+ if (options.providerImportSource !== void 0) writer.write(`...${names.get("_provideComponents")}(), `);
177
+ writer.write(`...${props}.components};\n`);
178
+ writer.write(` return ${layout} ? ${runtimeCall(analysis, layout, `{...${props}, children: ${content}}`)} : ${content};\n`);
376
179
  }
377
- return maxOffset > 0 ? maxOffset : 0;
378
- }
379
- function getErrorPlace(error) {
380
- if (typeof error !== "object" || error === null) return;
381
- const candidate = error;
382
- return toPlace(candidate.place) ?? toPlace(candidate.position);
180
+ writer.write("}\n");
383
181
  }
384
- function toMessagePlace(message) {
385
- return toPlace(message.place);
182
+ function emitMissingReferenceHelper(writer, analysis) {
183
+ const names = analysis.scope.names;
184
+ writer.write(`class ${names.get("ReferenceError")} extends Error {\n constructor(id, component) {
185
+ super(\`Expected \${component ? "component" : "object"} \${id} to be defined: you likely forgot to import, pass, or provide it.\`);
186
+ this.name = "ReferenceError";
187
+ }
386
188
  }
387
- function toPlace(value) {
388
- if (typeof value !== "object" || value === null) return;
389
- const candidate = value;
390
- if (candidate.start !== void 0 || candidate.end !== void 0) return {
391
- start: toPoint(candidate.start),
392
- end: toPoint(candidate.end)
393
- };
394
- const point = toPoint(candidate);
395
- return point === void 0 ? void 0 : { start: point };
396
- }
397
- function toPoint(value) {
398
- if (typeof value !== "object" || value === null) return;
399
- const candidate = value;
400
- const line = typeof candidate.line === "number" ? candidate.line : void 0;
401
- const column = typeof candidate.column === "number" ? candidate.column : void 0;
402
- const offset = typeof candidate.offset === "number" ? candidate.offset : void 0;
403
- if (line === void 0 && column === void 0 && offset === void 0) return;
404
- return {
405
- line,
406
- column,
407
- offset
408
- };
409
- }
410
- function getErrorReason(error) {
411
- if (error instanceof Error && error.message !== "") return error.message;
412
- const reason = getErrorField(error, "reason");
413
- if (reason !== void 0 && reason !== "") return reason;
414
- return String(error);
415
- }
416
- function getErrorCause(error) {
417
- if (typeof error !== "object" || error === null || !("cause" in error)) return;
418
- const cause = error.cause;
419
- if (cause instanceof Error) return {
420
- name: cause.name,
421
- message: cause.message
422
- };
423
- if (typeof cause === "string" && cause !== "") return { message: cause };
189
+ function ${names.get("_missingReference")}(id, component) {\n throw new ${names.get("ReferenceError")}(id, component);\n}
190
+ `);
424
191
  }
425
- function getErrorField(error, field) {
426
- if (typeof error !== "object" || error === null || !(field in error)) return;
427
- const value = error[field];
428
- return typeof value === "string" && value !== "" ? value : void 0;
192
+ function enforceGeneratedCodeLimit(file, code, configuredMaximum) {
193
+ const maximum = configuredMaximum ?? DEFAULT_MAXIMUM_GENERATED_CODE_BYTES;
194
+ const actual = new TextEncoder().encode(code).byteLength;
195
+ if (actual > maximum) throw LimitError.exceeded(file, "maximumGeneratedCodeBytes", maximum, void 0, actual);
429
196
  }
430
- function getErrorNumberField(error, field) {
431
- if (typeof error !== "object" || error === null || !(field in error)) return;
432
- const value = error[field];
433
- return typeof value === "number" ? value : void 0;
197
+ function enforceSourceMapLimit(file, map, configuredMaximum) {
198
+ const maximum = configuredMaximum ?? DEFAULT_MAXIMUM_SOURCE_MAP_BYTES;
199
+ const actual = new TextEncoder().encode(JSON.stringify(map)).byteLength;
200
+ if (actual > maximum) throw LimitError.exceeded(file, "maximumSourceMapBytes", maximum, void 0, actual);
434
201
  }
435
- function toSourceText(source) {
436
- return typeof source === "string" ? source : new TextDecoder().decode(source);
202
+ function runtimeCall(analysis, tag, properties) {
203
+ return analysis.scope.development ? `${analysis.scope.names.get("_jsxDEV")}(${tag}, ${properties}, undefined, false, undefined, undefined)` : `${analysis.scope.names.get("_jsx")}(${tag}, ${properties})`;
437
204
  }
438
205
  //#endregion
439
- export { MdxCompileError, MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin };
206
+ export { compile };
440
207
 
441
208
  //# sourceMappingURL=compiler.mjs.map