@temelj/mdx 0.14.0 → 0.16.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 (381) hide show
  1. package/README.md +470 -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 +167 -420
  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-decode.d.mts +15 -0
  75. package/dist/frontmatter-decode.d.mts.map +1 -0
  76. package/dist/frontmatter-decode.mjs +187 -0
  77. package/dist/frontmatter-decode.mjs.map +1 -0
  78. package/dist/frontmatter-schema.d.mts +8 -0
  79. package/dist/frontmatter-schema.d.mts.map +1 -0
  80. package/dist/frontmatter-schema.mjs +28 -0
  81. package/dist/frontmatter-schema.mjs.map +1 -0
  82. package/dist/frontmatter.d.mts +40 -0
  83. package/dist/frontmatter.d.mts.map +1 -0
  84. package/dist/frontmatter.mjs +109 -0
  85. package/dist/frontmatter.mjs.map +1 -0
  86. package/dist/heading.d.mts +6 -0
  87. package/dist/heading.d.mts.map +1 -0
  88. package/dist/heading.mjs +19 -0
  89. package/dist/heading.mjs.map +1 -0
  90. package/dist/html-format.d.mts +32 -0
  91. package/dist/html-format.d.mts.map +1 -0
  92. package/dist/html-format.mjs +127 -0
  93. package/dist/html-format.mjs.map +1 -0
  94. package/dist/html-lower.d.mts +78 -0
  95. package/dist/html-lower.d.mts.map +1 -0
  96. package/dist/html-lower.mjs +583 -0
  97. package/dist/html-lower.mjs.map +1 -0
  98. package/dist/html-stream.d.mts +13 -0
  99. package/dist/html-stream.d.mts.map +1 -0
  100. package/dist/html-stream.mjs +32 -0
  101. package/dist/html-stream.mjs.map +1 -0
  102. package/dist/html.d.mts +120 -0
  103. package/dist/html.d.mts.map +1 -0
  104. package/dist/html.mjs +77 -0
  105. package/dist/html.mjs.map +1 -0
  106. package/dist/identifier.d.mts +4 -0
  107. package/dist/identifier.d.mts.map +1 -0
  108. package/dist/identifier.mjs +8 -0
  109. package/dist/identifier.mjs.map +1 -0
  110. package/dist/javascript-ast.d.mts +16 -0
  111. package/dist/javascript-ast.d.mts.map +1 -0
  112. package/dist/javascript-ast.mjs +39 -0
  113. package/dist/javascript-ast.mjs.map +1 -0
  114. package/dist/javascript-jsx.d.mts +24 -0
  115. package/dist/javascript-jsx.d.mts.map +1 -0
  116. package/dist/javascript-jsx.mjs +189 -0
  117. package/dist/javascript-jsx.mjs.map +1 -0
  118. package/dist/javascript.d.mts +9 -0
  119. package/dist/javascript.d.mts.map +1 -0
  120. package/dist/javascript.mjs +31 -0
  121. package/dist/javascript.mjs.map +1 -0
  122. package/dist/jsx-reference.d.mts +15 -0
  123. package/dist/jsx-reference.d.mts.map +1 -0
  124. package/dist/jsx-reference.mjs +31 -0
  125. package/dist/jsx-reference.mjs.map +1 -0
  126. package/dist/mod.d.mts +26 -9
  127. package/dist/mod.mjs +20 -8
  128. package/dist/model.d.mts +196 -0
  129. package/dist/model.d.mts.map +1 -0
  130. package/dist/model.mjs +113 -0
  131. package/dist/model.mjs.map +1 -0
  132. package/dist/parser/block-container-recognizers.d.mts +40 -0
  133. package/dist/parser/block-container-recognizers.d.mts.map +1 -0
  134. package/dist/parser/block-container-recognizers.mjs +90 -0
  135. package/dist/parser/block-container-recognizers.mjs.map +1 -0
  136. package/dist/parser/block-directive-recognizers.d.mts +22 -0
  137. package/dist/parser/block-directive-recognizers.d.mts.map +1 -0
  138. package/dist/parser/block-directive-recognizers.mjs +77 -0
  139. package/dist/parser/block-directive-recognizers.mjs.map +1 -0
  140. package/dist/parser/block-leaf-recognizers.d.mts +53 -0
  141. package/dist/parser/block-leaf-recognizers.d.mts.map +1 -0
  142. package/dist/parser/block-leaf-recognizers.mjs +386 -0
  143. package/dist/parser/block-leaf-recognizers.mjs.map +1 -0
  144. package/dist/parser/block-parser.d.mts +38 -0
  145. package/dist/parser/block-parser.d.mts.map +1 -0
  146. package/dist/parser/block-parser.mjs +895 -0
  147. package/dist/parser/block-parser.mjs.map +1 -0
  148. package/dist/parser/block-syntax.d.mts +240 -0
  149. package/dist/parser/block-syntax.d.mts.map +1 -0
  150. package/dist/parser/characters.d.mts +19 -0
  151. package/dist/parser/characters.d.mts.map +1 -0
  152. package/dist/parser/characters.mjs +52 -0
  153. package/dist/parser/characters.mjs.map +1 -0
  154. package/dist/parser/config.d.mts +16 -0
  155. package/dist/parser/config.d.mts.map +1 -0
  156. package/dist/parser/config.mjs +25 -0
  157. package/dist/parser/config.mjs.map +1 -0
  158. package/dist/parser/directive-attributes.d.mts +9 -0
  159. package/dist/parser/directive-attributes.d.mts.map +1 -0
  160. package/dist/parser/directive-attributes.mjs +24 -0
  161. package/dist/parser/directive-attributes.mjs.map +1 -0
  162. package/dist/parser/embedded-scanner.d.mts +18 -0
  163. package/dist/parser/embedded-scanner.d.mts.map +1 -0
  164. package/dist/parser/embedded-scanner.mjs +199 -0
  165. package/dist/parser/embedded-scanner.mjs.map +1 -0
  166. package/dist/parser/inline-autolink.d.mts +7 -0
  167. package/dist/parser/inline-autolink.d.mts.map +1 -0
  168. package/dist/parser/inline-autolink.mjs +42 -0
  169. package/dist/parser/inline-autolink.mjs.map +1 -0
  170. package/dist/parser/inline-character-reference.d.mts +7 -0
  171. package/dist/parser/inline-character-reference.d.mts.map +1 -0
  172. package/dist/parser/inline-character-reference.mjs +22 -0
  173. package/dist/parser/inline-character-reference.mjs.map +1 -0
  174. package/dist/parser/inline-code.d.mts +7 -0
  175. package/dist/parser/inline-code.d.mts.map +1 -0
  176. package/dist/parser/inline-code.mjs +29 -0
  177. package/dist/parser/inline-code.mjs.map +1 -0
  178. package/dist/parser/inline-directive.d.mts +9 -0
  179. package/dist/parser/inline-directive.d.mts.map +1 -0
  180. package/dist/parser/inline-directive.mjs +45 -0
  181. package/dist/parser/inline-directive.mjs.map +1 -0
  182. package/dist/parser/inline-embedded.d.mts +19 -0
  183. package/dist/parser/inline-embedded.d.mts.map +1 -0
  184. package/dist/parser/inline-embedded.mjs +92 -0
  185. package/dist/parser/inline-embedded.mjs.map +1 -0
  186. package/dist/parser/inline-emphasis.d.mts +11 -0
  187. package/dist/parser/inline-emphasis.d.mts.map +1 -0
  188. package/dist/parser/inline-emphasis.mjs +79 -0
  189. package/dist/parser/inline-emphasis.mjs.map +1 -0
  190. package/dist/parser/inline-input.d.mts +46 -0
  191. package/dist/parser/inline-input.d.mts.map +1 -0
  192. package/dist/parser/inline-input.mjs +137 -0
  193. package/dist/parser/inline-input.mjs.map +1 -0
  194. package/dist/parser/inline-link.d.mts +12 -0
  195. package/dist/parser/inline-link.d.mts.map +1 -0
  196. package/dist/parser/inline-link.mjs +187 -0
  197. package/dist/parser/inline-link.mjs.map +1 -0
  198. package/dist/parser/inline-math.d.mts +10 -0
  199. package/dist/parser/inline-math.d.mts.map +1 -0
  200. package/dist/parser/inline-math.mjs +46 -0
  201. package/dist/parser/inline-math.mjs.map +1 -0
  202. package/dist/parser/inline-output.d.mts +34 -0
  203. package/dist/parser/inline-output.d.mts.map +1 -0
  204. package/dist/parser/inline-output.mjs +62 -0
  205. package/dist/parser/inline-output.mjs.map +1 -0
  206. package/dist/parser/inline-parser.d.mts +14 -0
  207. package/dist/parser/inline-parser.d.mts.map +1 -0
  208. package/dist/parser/inline-parser.mjs +328 -0
  209. package/dist/parser/inline-parser.mjs.map +1 -0
  210. package/dist/parser/inline-pending.d.mts +23 -0
  211. package/dist/parser/inline-pending.d.mts.map +1 -0
  212. package/dist/parser/inline-pending.mjs +100 -0
  213. package/dist/parser/inline-pending.mjs.map +1 -0
  214. package/dist/parser/inline-syntax.d.mts +118 -0
  215. package/dist/parser/inline-syntax.d.mts.map +1 -0
  216. package/dist/parser/inline-syntax.mjs +1 -0
  217. package/dist/parser/javascript-boundary.d.mts +16 -0
  218. package/dist/parser/javascript-boundary.d.mts.map +1 -0
  219. package/dist/parser/javascript-boundary.mjs +42 -0
  220. package/dist/parser/javascript-boundary.mjs.map +1 -0
  221. package/dist/parser/javascript-scanner.d.mts +22 -0
  222. package/dist/parser/javascript-scanner.d.mts.map +1 -0
  223. package/dist/parser/javascript-scanner.mjs +122 -0
  224. package/dist/parser/javascript-scanner.mjs.map +1 -0
  225. package/dist/parser/materialize-cache.d.mts +34 -0
  226. package/dist/parser/materialize-cache.d.mts.map +1 -0
  227. package/dist/parser/materialize-cache.mjs +12 -0
  228. package/dist/parser/materialize-cache.mjs.map +1 -0
  229. package/dist/parser/materialize-jsx.d.mts +14 -0
  230. package/dist/parser/materialize-jsx.d.mts.map +1 -0
  231. package/dist/parser/materialize-jsx.mjs +76 -0
  232. package/dist/parser/materialize-jsx.mjs.map +1 -0
  233. package/dist/parser/materialize.d.mts +61 -0
  234. package/dist/parser/materialize.d.mts.map +1 -0
  235. package/dist/parser/materialize.mjs +736 -0
  236. package/dist/parser/materialize.mjs.map +1 -0
  237. package/dist/parser/recognizer.d.mts +20 -0
  238. package/dist/parser/recognizer.d.mts.map +1 -0
  239. package/dist/parser/recognizer.mjs +24 -0
  240. package/dist/parser/recognizer.mjs.map +1 -0
  241. package/dist/parser/session.d.mts +42 -0
  242. package/dist/parser/session.d.mts.map +1 -0
  243. package/dist/parser/session.mjs +89 -0
  244. package/dist/parser/session.mjs.map +1 -0
  245. package/dist/parser.d.mts +25 -0
  246. package/dist/parser.d.mts.map +1 -0
  247. package/dist/parser.mjs +37 -0
  248. package/dist/parser.mjs.map +1 -0
  249. package/dist/plugin-internal.d.mts +25 -0
  250. package/dist/plugin-internal.d.mts.map +1 -0
  251. package/dist/plugin-internal.mjs +99 -0
  252. package/dist/plugin-internal.mjs.map +1 -0
  253. package/dist/plugin.d.mts +37 -0
  254. package/dist/plugin.d.mts.map +1 -0
  255. package/dist/plugin.mjs +1 -0
  256. package/dist/plugins/code-block-metadata.d.mts +42 -0
  257. package/dist/plugins/code-block-metadata.d.mts.map +1 -0
  258. package/dist/plugins/code-block-metadata.mjs +133 -0
  259. package/dist/plugins/code-block-metadata.mjs.map +1 -0
  260. package/dist/plugins/heading-id.d.mts +15 -0
  261. package/dist/plugins/heading-id.d.mts.map +1 -0
  262. package/dist/plugins/heading-id.mjs +45 -0
  263. package/dist/plugins/heading-id.mjs.map +1 -0
  264. package/dist/plugins/lower-heading.d.mts +16 -0
  265. package/dist/plugins/lower-heading.d.mts.map +1 -0
  266. package/dist/plugins/lower-heading.mjs +28 -0
  267. package/dist/plugins/lower-heading.mjs.map +1 -0
  268. package/dist/plugins/math-render.d.mts +26 -0
  269. package/dist/plugins/math-render.d.mts.map +1 -0
  270. package/dist/plugins/math-render.mjs +51 -0
  271. package/dist/plugins/math-render.mjs.map +1 -0
  272. package/dist/plugins/mod.d.mts +9 -6
  273. package/dist/plugins/mod.mjs +8 -6
  274. package/dist/plugins/remove-esm.d.mts +10 -0
  275. package/dist/plugins/remove-esm.d.mts.map +1 -0
  276. package/dist/plugins/remove-esm.mjs +12 -0
  277. package/dist/plugins/remove-esm.mjs.map +1 -0
  278. package/dist/plugins/shiki.d.mts +21 -0
  279. package/dist/plugins/shiki.d.mts.map +1 -0
  280. package/dist/plugins/shiki.mjs +144 -0
  281. package/dist/plugins/shiki.mjs.map +1 -0
  282. package/dist/plugins/syntax-highlight.d.mts +38 -0
  283. package/dist/plugins/syntax-highlight.d.mts.map +1 -0
  284. package/dist/plugins/syntax-highlight.mjs +106 -0
  285. package/dist/plugins/syntax-highlight.mjs.map +1 -0
  286. package/dist/processor.d.mts +60 -0
  287. package/dist/processor.d.mts.map +1 -0
  288. package/dist/processor.mjs +106 -0
  289. package/dist/processor.mjs.map +1 -0
  290. package/dist/ranges.d.mts +10 -0
  291. package/dist/ranges.d.mts.map +1 -0
  292. package/dist/ranges.mjs +22 -0
  293. package/dist/ranges.mjs.map +1 -0
  294. package/dist/source-map.d.mts +28 -0
  295. package/dist/source-map.d.mts.map +1 -0
  296. package/dist/source-map.mjs +90 -0
  297. package/dist/source-map.mjs.map +1 -0
  298. package/dist/source-text.d.mts +35 -0
  299. package/dist/source-text.d.mts.map +1 -0
  300. package/dist/source-text.mjs +136 -0
  301. package/dist/source-text.mjs.map +1 -0
  302. package/dist/source.d.mts +86 -0
  303. package/dist/source.d.mts.map +1 -0
  304. package/dist/source.mjs +264 -0
  305. package/dist/source.mjs.map +1 -0
  306. package/dist/stream-events.d.mts +59 -0
  307. package/dist/stream-events.d.mts.map +1 -0
  308. package/dist/stream-events.mjs +115 -0
  309. package/dist/stream-events.mjs.map +1 -0
  310. package/dist/stream-session.d.mts +79 -0
  311. package/dist/stream-session.d.mts.map +1 -0
  312. package/dist/stream-session.mjs +904 -0
  313. package/dist/stream-session.mjs.map +1 -0
  314. package/dist/stream-tail.d.mts +84 -0
  315. package/dist/stream-tail.d.mts.map +1 -0
  316. package/dist/stream-tail.mjs +359 -0
  317. package/dist/stream-tail.mjs.map +1 -0
  318. package/dist/stream-types.d.mts +89 -0
  319. package/dist/stream-types.d.mts.map +1 -0
  320. package/dist/stream-types.mjs +1 -0
  321. package/dist/stream.d.mts +28 -0
  322. package/dist/stream.d.mts.map +1 -0
  323. package/dist/stream.mjs +68 -0
  324. package/dist/stream.mjs.map +1 -0
  325. package/dist/syntax-options.d.mts +21 -0
  326. package/dist/syntax-options.d.mts.map +1 -0
  327. package/dist/syntax-options.mjs +26 -0
  328. package/dist/syntax-options.mjs.map +1 -0
  329. package/dist/traversal.d.mts +25 -0
  330. package/dist/traversal.d.mts.map +1 -0
  331. package/dist/traversal.mjs +136 -0
  332. package/dist/traversal.mjs.map +1 -0
  333. package/dist/tree.d.mts +18 -0
  334. package/dist/tree.d.mts.map +1 -0
  335. package/dist/tree.mjs +43 -0
  336. package/dist/tree.mjs.map +1 -0
  337. package/dist/unified/adapter.d.mts +24 -0
  338. package/dist/unified/adapter.d.mts.map +1 -0
  339. package/dist/unified/adapter.mjs +44 -0
  340. package/dist/unified/adapter.mjs.map +1 -0
  341. package/dist/unified/mod.d.mts +2 -0
  342. package/dist/unified/mod.mjs +1 -0
  343. package/dist/unified/runtime.d.mts +10 -0
  344. package/dist/unified/runtime.d.mts.map +1 -0
  345. package/dist/unified/runtime.mjs +115 -0
  346. package/dist/unified/runtime.mjs.map +1 -0
  347. package/dist/unified/tree-bridge.d.mts +63 -0
  348. package/dist/unified/tree-bridge.d.mts.map +1 -0
  349. package/dist/unified/tree-bridge.mjs +876 -0
  350. package/dist/unified/tree-bridge.mjs.map +1 -0
  351. package/dist/uri.d.mts +5 -0
  352. package/dist/uri.d.mts.map +1 -0
  353. package/dist/uri.mjs +32 -0
  354. package/dist/uri.mjs.map +1 -0
  355. package/package.json +56 -30
  356. package/dist/plugins/heading-id/plugin.d.mts +0 -17
  357. package/dist/plugins/heading-id/plugin.d.mts.map +0 -1
  358. package/dist/plugins/heading-id/plugin.mjs +0 -23
  359. package/dist/plugins/heading-id/plugin.mjs.map +0 -1
  360. package/dist/plugins/katex/plugin.d.mts +0 -39
  361. package/dist/plugins/katex/plugin.d.mts.map +0 -1
  362. package/dist/plugins/katex/plugin.mjs +0 -107
  363. package/dist/plugins/katex/plugin.mjs.map +0 -1
  364. package/dist/plugins/remove-imports-exports.d.mts +0 -10
  365. package/dist/plugins/remove-imports-exports.d.mts.map +0 -1
  366. package/dist/plugins/remove-imports-exports.mjs +0 -15
  367. package/dist/plugins/remove-imports-exports.mjs.map +0 -1
  368. package/dist/plugins/syntax-highlight/code-meta.d.mts +0 -22
  369. package/dist/plugins/syntax-highlight/code-meta.d.mts.map +0 -1
  370. package/dist/plugins/syntax-highlight/code-meta.mjs +0 -38
  371. package/dist/plugins/syntax-highlight/code-meta.mjs.map +0 -1
  372. package/dist/plugins/syntax-highlight/plugin.d.mts +0 -57
  373. package/dist/plugins/syntax-highlight/plugin.d.mts.map +0 -1
  374. package/dist/plugins/syntax-highlight/plugin.mjs +0 -93
  375. package/dist/plugins/syntax-highlight/plugin.mjs.map +0 -1
  376. package/dist/plugins/tree-processor/plugin.d.mts +0 -19
  377. package/dist/plugins/tree-processor/plugin.d.mts.map +0 -1
  378. package/dist/plugins/tree-processor/plugin.mjs +0 -21
  379. package/dist/plugins/tree-processor/plugin.mjs.map +0 -1
  380. package/dist/types.d.mts +0 -2
  381. /package/dist/{types.mjs → parser/block-syntax.mjs} +0 -0
package/dist/compiler.mjs CHANGED
@@ -1,441 +1,188 @@
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 { CompileError, ConfigurationError, FrontmatterError, LimitError, MdxError } from "./errors.mjs";
2
+ import { javascriptPropertyName } from "./javascript-ast.mjs";
3
+ import { parseJavaScriptProgram } from "./javascript.mjs";
4
+ import { analyzeDocument } from "./compile-analysis.mjs";
5
+ import { contentUsesAwait, emitContent } from "./compile-emit.mjs";
6
+ import { frontmatterSchemaValue } from "./frontmatter-schema.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"
78
- });
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);
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);
102
35
  }
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
- };
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
49
+ });
50
+ } catch (cause) {
51
+ if (cause instanceof MdxError) throw cause;
52
+ throw CompileError.generationFailed(cause);
109
53
  }
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
127
- });
128
54
  }
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
- };
161
- });
162
- }
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}`);
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
+ frontmatter = frontmatterSchemaValue(validated, input.file, frontmatterNode?.origin);
173
61
  }
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
192
- }
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
62
+ const transported = copyTransportValue(frontmatter, "frontmatter", {
63
+ file: input.file,
64
+ origin: frontmatterNode?.origin,
65
+ ancestors: /* @__PURE__ */ new WeakSet()
66
+ });
67
+ const artifact = emitArtifact(input.document, {
68
+ ...options,
69
+ source: input.file
70
+ }, transported);
71
+ return Object.freeze({
72
+ file: input.file,
73
+ document: input.document,
74
+ diagnostics: input.diagnostics,
75
+ artifact,
76
+ frontmatter
204
77
  });
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;
260
- }
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);
272
- }
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)
284
- };
285
- }
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
78
  }
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);
79
+ function copyTransportValue(value, path, context) {
80
+ const { ancestors } = context;
81
+ if (value === null || isBoolean(value) || isNumber(value) || isBigInt(value) || isString(value)) return value;
82
+ if (!isObject(value)) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path} contains unsupported ${Object.prototype.toString.call(value)}`);
83
+ if (ancestors.has(value)) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path} contains a cycle`);
84
+ ancestors.add(value);
85
+ if (Array.isArray(value)) {
86
+ const output = [];
87
+ for (let index = 0; index < value.length; index++) {
88
+ const item = copyTransportValue(value[index], `${path}[${index}]`, context);
89
+ output.push(item);
90
+ }
91
+ ancestors.delete(value);
92
+ return Object.freeze(output);
320
93
  }
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
- };
94
+ const prototype = Object.getPrototypeOf(value);
95
+ if (prototype !== Object.prototype && prototype !== null) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path} contains a non-plain object`);
96
+ if (Object.getOwnPropertySymbols(value).length > 0) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path} contains a symbol property`);
97
+ const output = {};
98
+ for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(value))) {
99
+ if (!descriptor.enumerable) continue;
100
+ if (!("value" in descriptor)) throw FrontmatterError.nonTransportableValue(context.file, context.origin, `${path}.${key} is an accessor property`);
101
+ const item = copyTransportValue(descriptor.value, `${path}.${key}`, context);
102
+ Object.defineProperty(output, key, {
103
+ enumerable: true,
104
+ value: item
105
+ });
338
106
  }
339
- return bestMatch;
107
+ ancestors.delete(value);
108
+ return Object.freeze(output);
340
109
  }
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;
110
+ /**
111
+ * Authored ESM, the JSX runtime, and the optional provider. A function body
112
+ * receives the runtime as `__mdxRuntime` and resolves imports through
113
+ * `__resolveMdxImport` instead of importing.
114
+ */
115
+ function emitModulePreamble(writer, analysis, options, format) {
116
+ const names = analysis.scope.names;
117
+ 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")}}`;
118
+ const importSource = `${options.jsxImportSource ?? "react"}/${analysis.scope.development ? "jsx-dev-runtime" : "jsx-runtime"}`;
119
+ writer.write(format === "program" ? `import ${runtime.replaceAll(": ", " as ")} from ${JSON.stringify(importSource)};\n` : `const ${runtime} = __mdxRuntime;\n`);
120
+ if (format === "functionBody") {
121
+ if (analysis.esm.imports.length > 0 || options.providerImportSource !== void 0) writer.write(`const ${names.get("_resolveMdxImport")} = __resolveMdxImport;\n`);
122
+ writer.write("{\n");
353
123
  }
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;
124
+ for (const entry of analysis.esm.blocks) writer.write(`${entry.code.trimEnd()}\n`, entry.origin);
125
+ 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`);
126
+ }
127
+ function emitContentFunction(writer, content, analysis, options, usesAwait) {
128
+ const names = analysis.scope.names;
129
+ const props = analysis.propsName;
130
+ const componentsName = names.get("_components");
131
+ writer.write(`${usesAwait ? "async " : ""}function ${names.get("_createContent")}(${props}) {\n`);
132
+ writer.write(` const ${componentsName} = {`);
133
+ const tags = [...analysis.intrinsicTags].sort();
134
+ tags.forEach((tag, index) => {
135
+ if (index > 0) writer.write(", ");
136
+ writer.write(`${javascriptPropertyName(tag)}:${JSON.stringify(tag)}`);
137
+ });
138
+ if (options.providerImportSource !== void 0) writer.write(`${tags.length === 0 ? "" : ", "}...${names.get("_provideComponents")}()`);
139
+ writer.write(`${tags.length === 0 && options.providerImportSource === void 0 ? "" : ", "}...${props}.components};\n`);
140
+ const components = [...analysis.scope.referencedComponents].sort();
141
+ for (const component of components) writer.write(` const ${component} = ${componentsName}[${JSON.stringify(component)}];\n`);
142
+ for (const [reference, component] of analysis.scope.missingReferences) writer.write(` if (!${reference}) ${names.get("_missingReference")}(${JSON.stringify(reference)}, ${component});\n`);
143
+ writer.write(" return ");
144
+ emitContent(writer, content, analysis);
145
+ writer.write(";\n}\n");
146
+ }
147
+ function emitPublicComponent(writer, analysis, options, usesAwait, format) {
148
+ const names = analysis.scope.names;
149
+ const props = analysis.propsName;
150
+ const layout = names.get("Layout");
151
+ writer.write(`${format === "program" ? "export default " : ""}${usesAwait ? "async " : ""}function ${names.get("Content")}(${props} = {}) {\n`);
152
+ const content = `${usesAwait ? "await " : ""}${names.get("_createContent")}(${props})`;
153
+ if (analysis.esm.hasLayout) writer.write(` return ${runtimeCall(analysis, layout, `{...${props}, children: ${content}}`)};\n`);
154
+ else {
155
+ writer.write(` const {wrapper: ${layout}} = {`);
156
+ if (options.providerImportSource !== void 0) writer.write(`...${names.get("_provideComponents")}(), `);
157
+ writer.write(`...${props}.components};\n`);
158
+ writer.write(` return ${layout} ? ${runtimeCall(analysis, layout, `{...${props}, children: ${content}}`)} : ${content};\n`);
376
159
  }
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);
383
- }
384
- function toMessagePlace(message) {
385
- return toPlace(message.place);
386
- }
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
- };
160
+ writer.write("}\n");
409
161
  }
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);
162
+ function emitMissingReferenceHelper(writer, analysis) {
163
+ const names = analysis.scope.names;
164
+ writer.write(`class ${names.get("ReferenceError")} extends Error {\n constructor(id, component) {
165
+ super(\`Expected \${component ? "component" : "object"} \${id} to be defined: you likely forgot to import, pass, or provide it.\`);
166
+ this.name = "ReferenceError";
167
+ }
415
168
  }
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 };
169
+ function ${names.get("_missingReference")}(id, component) {\n throw new ${names.get("ReferenceError")}(id, component);\n}
170
+ `);
424
171
  }
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;
172
+ function enforceGeneratedCodeLimit(file, code, configuredMaximum) {
173
+ const maximum = configuredMaximum ?? DEFAULT_MAXIMUM_GENERATED_CODE_BYTES;
174
+ const actual = new TextEncoder().encode(code).byteLength;
175
+ if (actual > maximum) throw LimitError.exceeded(file, "maximumGeneratedCodeBytes", maximum, void 0, actual);
429
176
  }
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;
177
+ function enforceSourceMapLimit(file, map, configuredMaximum) {
178
+ const maximum = configuredMaximum ?? DEFAULT_MAXIMUM_SOURCE_MAP_BYTES;
179
+ const actual = new TextEncoder().encode(JSON.stringify(map)).byteLength;
180
+ if (actual > maximum) throw LimitError.exceeded(file, "maximumSourceMapBytes", maximum, void 0, actual);
434
181
  }
435
- function toSourceText(source) {
436
- return typeof source === "string" ? source : new TextDecoder().decode(source);
182
+ function runtimeCall(analysis, tag, properties) {
183
+ return analysis.scope.development ? `${analysis.scope.names.get("_jsxDEV")}(${tag}, ${properties}, undefined, false, undefined, undefined)` : `${analysis.scope.names.get("_jsx")}(${tag}, ${properties})`;
437
184
  }
438
185
  //#endregion
439
- export { MdxCompileError, MdxCompiler, remarkFrontmatterPlugin, remarkGfmPlugin };
186
+ export { compile };
440
187
 
441
188
  //# sourceMappingURL=compiler.mjs.map