@mui/internal-docs-infra 0.1.1-canary.9 → 0.2.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +3 -11
  3. package/esm/CodeControllerContext/CodeControllerContext.d.ts +62 -0
  4. package/esm/CodeControllerContext/CodeControllerContext.js +33 -0
  5. package/esm/CodeControllerContext/index.d.ts +1 -0
  6. package/esm/CodeControllerContext/index.js +1 -0
  7. package/esm/CodeExternalsContext/CodeExternalsContext.d.ts +8 -0
  8. package/esm/CodeExternalsContext/CodeExternalsContext.js +8 -0
  9. package/esm/CodeExternalsContext/index.d.ts +1 -0
  10. package/esm/CodeExternalsContext/index.js +1 -0
  11. package/esm/CodeHighlighter/CodeHighlighter.d.ts +2 -0
  12. package/esm/CodeHighlighter/CodeHighlighter.js +441 -0
  13. package/esm/CodeHighlighter/CodeHighlighterClient.d.ts +2 -0
  14. package/esm/CodeHighlighter/CodeHighlighterClient.js +984 -0
  15. package/esm/CodeHighlighter/CodeHighlighterContext.d.ts +16 -0
  16. package/esm/CodeHighlighter/CodeHighlighterContext.js +15 -0
  17. package/esm/CodeHighlighter/CodeHighlighterFallbackContext.d.ts +7 -0
  18. package/esm/CodeHighlighter/CodeHighlighterFallbackContext.js +14 -0
  19. package/esm/CodeHighlighter/addPathsToVariant.d.ts +14 -0
  20. package/esm/CodeHighlighter/addPathsToVariant.js +68 -0
  21. package/esm/CodeHighlighter/applyTransform.d.ts +19 -0
  22. package/esm/CodeHighlighter/applyTransform.js +75 -0
  23. package/esm/CodeHighlighter/calculateMainFilePath.d.ts +1 -0
  24. package/esm/CodeHighlighter/calculateMainFilePath.js +108 -0
  25. package/esm/CodeHighlighter/codeToFallbackProps.d.ts +2 -0
  26. package/esm/CodeHighlighter/codeToFallbackProps.js +73 -0
  27. package/esm/CodeHighlighter/errors.d.ts +141 -0
  28. package/esm/CodeHighlighter/errors.js +441 -0
  29. package/esm/CodeHighlighter/examineVariant.d.ts +25 -0
  30. package/esm/CodeHighlighter/examineVariant.js +73 -0
  31. package/esm/CodeHighlighter/hasAllVariants.d.ts +27 -0
  32. package/esm/CodeHighlighter/hasAllVariants.js +63 -0
  33. package/esm/CodeHighlighter/index.d.ts +1 -0
  34. package/esm/CodeHighlighter/index.js +1 -0
  35. package/esm/CodeHighlighter/loadFallbackCode.d.ts +10 -0
  36. package/esm/CodeHighlighter/loadFallbackCode.js +679 -0
  37. package/esm/CodeHighlighter/loadVariant.d.ts +12 -0
  38. package/esm/CodeHighlighter/loadVariant.js +1044 -0
  39. package/esm/CodeHighlighter/maybeInitialData.d.ts +108 -0
  40. package/esm/CodeHighlighter/maybeInitialData.js +192 -0
  41. package/esm/CodeHighlighter/mergeMetadata.d.ts +40 -0
  42. package/esm/CodeHighlighter/mergeMetadata.js +165 -0
  43. package/esm/CodeHighlighter/parseCode.d.ts +6 -0
  44. package/esm/CodeHighlighter/parseCode.js +134 -0
  45. package/esm/CodeHighlighter/parseControlledCode.d.ts +6 -0
  46. package/esm/CodeHighlighter/parseControlledCode.js +87 -0
  47. package/esm/CodeHighlighter/pathUtils.d.ts +120 -0
  48. package/esm/CodeHighlighter/pathUtils.js +258 -0
  49. package/esm/CodeHighlighter/transformCode.d.ts +21 -0
  50. package/esm/CodeHighlighter/transformCode.js +251 -0
  51. package/esm/CodeHighlighter/transformParsedSource.d.ts +3 -0
  52. package/esm/CodeHighlighter/transformParsedSource.js +60 -0
  53. package/esm/CodeHighlighter/transformSource.d.ts +2 -0
  54. package/esm/CodeHighlighter/transformSource.js +103 -0
  55. package/esm/CodeHighlighter/types.d.ts +276 -0
  56. package/esm/CodeHighlighter/types.js +1 -0
  57. package/esm/CodeProvider/CodeContext.d.ts +31 -0
  58. package/esm/CodeProvider/CodeContext.js +12 -0
  59. package/esm/CodeProvider/CodeProvider.d.ts +13 -0
  60. package/esm/CodeProvider/CodeProvider.js +83 -0
  61. package/esm/CodeProvider/index.d.ts +1 -0
  62. package/esm/CodeProvider/index.js +1 -0
  63. package/esm/abstractCreateDemo/abstractCreateDemo.d.ts +45 -0
  64. package/esm/abstractCreateDemo/abstractCreateDemo.js +100 -0
  65. package/esm/abstractCreateDemo/index.d.ts +1 -0
  66. package/esm/abstractCreateDemo/index.js +1 -0
  67. package/esm/abstractCreateDemoClient/abstractCreateDemoClient.d.ts +32 -0
  68. package/esm/abstractCreateDemoClient/abstractCreateDemoClient.js +52 -0
  69. package/esm/abstractCreateDemoClient/index.d.ts +1 -0
  70. package/esm/abstractCreateDemoClient/index.js +1 -0
  71. package/esm/createDemoData/createDemoData.d.ts +44 -0
  72. package/esm/createDemoData/createDemoData.js +74 -0
  73. package/esm/createDemoData/index.d.ts +1 -0
  74. package/esm/createDemoData/index.js +1 -0
  75. package/esm/createDemoData/types.d.ts +25 -0
  76. package/esm/createDemoData/types.js +1 -0
  77. package/esm/pipeline/hastUtils/hastUtils.d.ts +11 -0
  78. package/esm/pipeline/hastUtils/hastUtils.js +67 -0
  79. package/esm/pipeline/hastUtils/index.d.ts +1 -0
  80. package/esm/pipeline/hastUtils/index.js +1 -0
  81. package/esm/pipeline/loadPrecomputedCodeHighlighter/index.d.ts +2 -0
  82. package/esm/pipeline/loadPrecomputedCodeHighlighter/index.js +4 -0
  83. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.d.ts +14 -0
  84. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.js +178 -0
  85. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.d.ts +47 -0
  86. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js +849 -0
  87. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.d.ts +85 -0
  88. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.js +715 -0
  89. package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.d.ts +14 -0
  90. package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.js +73 -0
  91. package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.d.ts +11 -0
  92. package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.js +203 -0
  93. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.d.ts +6 -0
  94. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.js +23 -0
  95. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.d.ts +6 -0
  96. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.js +148 -0
  97. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.d.ts +9 -0
  98. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.js +198 -0
  99. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/index.d.ts +2 -0
  100. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/index.js +4 -0
  101. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.d.ts +5 -0
  102. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.js +21 -0
  103. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.d.ts +15 -0
  104. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.js +233 -0
  105. package/esm/pipeline/loadServerCodeMeta/index.d.ts +1 -0
  106. package/esm/pipeline/loadServerCodeMeta/index.js +1 -0
  107. package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.d.ts +24 -0
  108. package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.js +90 -0
  109. package/esm/pipeline/loadServerSource/index.d.ts +1 -0
  110. package/esm/pipeline/loadServerSource/index.js +1 -0
  111. package/esm/pipeline/loadServerSource/loadServerSource.d.ts +25 -0
  112. package/esm/pipeline/loadServerSource/loadServerSource.js +134 -0
  113. package/esm/pipeline/loaderUtils/externalsToPackages.d.ts +1 -0
  114. package/esm/pipeline/loaderUtils/externalsToPackages.js +46 -0
  115. package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.d.ts +34 -0
  116. package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.js +161 -0
  117. package/esm/pipeline/loaderUtils/getFileNameFromUrl.d.ts +12 -0
  118. package/esm/pipeline/loaderUtils/getFileNameFromUrl.js +66 -0
  119. package/esm/pipeline/loaderUtils/index.d.ts +7 -0
  120. package/esm/pipeline/loaderUtils/index.js +7 -0
  121. package/esm/pipeline/loaderUtils/mergeExternals.d.ts +32 -0
  122. package/esm/pipeline/loaderUtils/mergeExternals.js +72 -0
  123. package/esm/pipeline/loaderUtils/parseImports.d.ts +19 -0
  124. package/esm/pipeline/loaderUtils/parseImports.js +306 -0
  125. package/esm/pipeline/loaderUtils/processRelativeImports.d.ts +19 -0
  126. package/esm/pipeline/loaderUtils/processRelativeImports.js +352 -0
  127. package/esm/pipeline/loaderUtils/resolveModulePath.d.ts +87 -0
  128. package/esm/pipeline/loaderUtils/resolveModulePath.js +1435 -0
  129. package/esm/pipeline/loaderUtils/resolveModulePathWithFs.d.ts +47 -0
  130. package/esm/pipeline/loaderUtils/resolveModulePathWithFs.js +150 -0
  131. package/esm/pipeline/loaderUtils/rewriteImports.d.ts +9 -0
  132. package/esm/pipeline/loaderUtils/rewriteImports.js +35 -0
  133. package/esm/pipeline/parseSource/addLineGutters.d.ts +9 -0
  134. package/esm/pipeline/parseSource/addLineGutters.js +181 -0
  135. package/esm/pipeline/parseSource/grammars.d.ts +2 -0
  136. package/esm/pipeline/parseSource/grammars.js +27 -0
  137. package/esm/pipeline/parseSource/index.d.ts +1 -0
  138. package/esm/pipeline/parseSource/index.js +1 -0
  139. package/esm/pipeline/parseSource/parseSource.d.ts +3 -0
  140. package/esm/pipeline/parseSource/parseSource.js +51 -0
  141. package/esm/pipeline/transformHtmlCode/index.d.ts +2 -0
  142. package/esm/pipeline/transformHtmlCode/index.js +4 -0
  143. package/esm/pipeline/transformHtmlCode/transformHtmlCode.d.ts +13 -0
  144. package/esm/pipeline/transformHtmlCode/transformHtmlCode.js +300 -0
  145. package/esm/pipeline/transformMarkdownCode/index.d.ts +2 -0
  146. package/esm/pipeline/transformMarkdownCode/index.js +4 -0
  147. package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.d.ts +2 -0
  148. package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.js +514 -0
  149. package/esm/pipeline/transformTypescriptToJavascript/index.d.ts +1 -0
  150. package/esm/pipeline/transformTypescriptToJavascript/index.js +1 -0
  151. package/esm/pipeline/transformTypescriptToJavascript/removeTypes.d.ts +13 -0
  152. package/esm/pipeline/transformTypescriptToJavascript/removeTypes.js +131 -0
  153. package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.d.ts +3 -0
  154. package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.js +31 -0
  155. package/esm/useCode/Pre.d.ts +15 -0
  156. package/esm/useCode/Pre.js +164 -0
  157. package/esm/useCode/index.d.ts +1 -0
  158. package/esm/useCode/index.js +1 -0
  159. package/esm/useCode/useCode.d.ts +41 -0
  160. package/esm/useCode/useCode.js +128 -0
  161. package/esm/useCode/useCodeUtils.d.ts +44 -0
  162. package/esm/useCode/useCodeUtils.js +245 -0
  163. package/esm/useCode/useCopyFunctionality.d.ts +18 -0
  164. package/esm/useCode/useCopyFunctionality.js +28 -0
  165. package/esm/useCode/useFileNavigation.d.ts +41 -0
  166. package/esm/useCode/useFileNavigation.js +453 -0
  167. package/esm/useCode/useSourceEditing.d.ts +21 -0
  168. package/esm/useCode/useSourceEditing.js +32 -0
  169. package/esm/useCode/useTransformManagement.d.ts +30 -0
  170. package/esm/useCode/useTransformManagement.js +72 -0
  171. package/esm/useCode/useUIState.d.ts +16 -0
  172. package/esm/useCode/useUIState.js +21 -0
  173. package/esm/useCode/useVariantSelection.d.ts +23 -0
  174. package/esm/useCode/useVariantSelection.js +75 -0
  175. package/esm/useCopier/index.d.ts +1 -1
  176. package/esm/useCopier/index.js +5 -5
  177. package/esm/useDemo/createCodeSandbox.d.ts +15 -0
  178. package/esm/useDemo/createCodeSandbox.js +42 -0
  179. package/esm/useDemo/createStackBlitz.d.ts +22 -0
  180. package/esm/useDemo/createStackBlitz.js +38 -0
  181. package/esm/useDemo/exportVariant.d.ts +184 -0
  182. package/esm/useDemo/exportVariant.js +422 -0
  183. package/esm/useDemo/exportVariantAsCra.d.ts +15 -0
  184. package/esm/useDemo/exportVariantAsCra.js +57 -0
  185. package/esm/useDemo/flattenVariant.d.ts +19 -0
  186. package/esm/useDemo/flattenVariant.js +49 -0
  187. package/esm/useDemo/index.d.ts +6 -51
  188. package/esm/useDemo/index.js +6 -104
  189. package/esm/useDemo/useDemo.d.ts +81 -0
  190. package/esm/useDemo/useDemo.js +193 -0
  191. package/esm/useErrors/ErrorsContext.d.ts +6 -0
  192. package/esm/useErrors/ErrorsContext.js +8 -0
  193. package/esm/useErrors/index.d.ts +1 -0
  194. package/esm/useErrors/index.js +1 -0
  195. package/esm/useErrors/useErrors.d.ts +5 -0
  196. package/esm/useErrors/useErrors.js +7 -0
  197. package/esm/useLocalStorageState/index.d.ts +2 -0
  198. package/esm/useLocalStorageState/index.js +2 -0
  199. package/esm/useLocalStorageState/useLocalStorageState.d.ts +14 -0
  200. package/esm/useLocalStorageState/useLocalStorageState.js +128 -0
  201. package/esm/usePreference/PreferencesProvider.d.ts +6 -0
  202. package/esm/usePreference/PreferencesProvider.js +8 -0
  203. package/esm/usePreference/index.d.ts +2 -0
  204. package/esm/usePreference/index.js +2 -0
  205. package/esm/usePreference/usePreference.d.ts +2 -0
  206. package/esm/usePreference/usePreference.js +25 -0
  207. package/esm/useUrlHashState/index.d.ts +1 -0
  208. package/esm/useUrlHashState/index.js +1 -0
  209. package/esm/useUrlHashState/useUrlHashState.d.ts +5 -0
  210. package/esm/useUrlHashState/useUrlHashState.js +68 -0
  211. package/esm/withDocsInfra/index.d.ts +1 -0
  212. package/esm/withDocsInfra/index.js +1 -0
  213. package/esm/withDocsInfra/withDocsInfra.d.ts +82 -0
  214. package/esm/withDocsInfra/withDocsInfra.js +147 -0
  215. package/package.json +178 -11
  216. package/esm/useCopier/index.d.ts.map +0 -1
  217. package/esm/useDemo/index.d.ts.map +0 -1
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Versions
2
+
3
+ ## 2.0.7
4
+
5
+ Test release
6
+
7
+ ## 2.0.6
8
+
9
+ Changelog start
package/README.md CHANGED
@@ -2,16 +2,8 @@
2
2
 
3
3
  This package hosts the tools that help create the documentation.
4
4
 
5
- ## Installation
6
-
7
- Install the package in your project directory with:
8
-
9
- <!-- #npm-tag-reference -->
10
-
11
- ```bash
12
- npm install @mui/internal-docs-infra
13
- ```
14
-
15
5
  ## Documentation
16
6
 
17
- We have no documentation for these components.
7
+ This is stored in the `docs` directory.
8
+
9
+ [Read More](../../docs/app/docs-infra/page.mdx)
@@ -0,0 +1,62 @@
1
+ import * as React from 'react';
2
+ import type { ControlledCode } from "../CodeHighlighter/types.js";
3
+ export type Selection = {
4
+ variant: string;
5
+ fileName?: string;
6
+ transformKey?: string;
7
+ };
8
+ /**
9
+ * Context for controlling the code shown within the CodeHighlighter component.
10
+ *
11
+ * To benefit from server or build-time rendering, the initial code should not be provided
12
+ * to the controller context. It's recommended to only set `code` after the first `setCode`
13
+ * event fires.
14
+ */
15
+ export interface CodeControllerContext {
16
+ /**
17
+ * Controls the code shown within the code highlighter. Unlike the CodeHighlighter component,
18
+ * code is always a string to simplify use. It will be highlighted when it is passed as `code`.
19
+ * This behavior depends on client-side highlighting and the CodeProvider component.
20
+ */
21
+ code?: ControlledCode;
22
+ /**
23
+ * Controls the state for displaying the given code. This works with build-time and client-side
24
+ * loading. If using server loading, the selection won't work for fallback loading and would
25
+ * have to be passed directly into the CodeHighlighter component within a server component.
26
+ */
27
+ selection?: Selection;
28
+ /**
29
+ * Setter function for updating the code. When provided in the context, this function will be
30
+ * called when the user interacts with the code highlighting. It's recommended to only set `code`
31
+ * after the first `setCode` event fires to benefit from server or build-time rendering.
32
+ */
33
+ setCode?: React.Dispatch<React.SetStateAction<ControlledCode | undefined>>;
34
+ /**
35
+ * Setter function for updating the selection state. When provided in the context, this function
36
+ * will be called when the user interacts with the code highlighting interface.
37
+ */
38
+ setSelection?: React.Dispatch<React.SetStateAction<Selection>>;
39
+ /**
40
+ * Allows overriding the preview components shown within the CodeHighlighter.
41
+ * It's recommended to keep this value undefined until there are any changes made to a
42
+ * component's code and passed as `code`. Each variant has a given component,
43
+ * e.g. `{ variantA: {}, variantB: {} }`.
44
+ */
45
+ components?: Record<string, React.ReactNode> | undefined;
46
+ }
47
+ export declare const CodeControllerContext: React.Context<CodeControllerContext | undefined>;
48
+ /**
49
+ * Hook for accessing the controlled code context.
50
+ *
51
+ * This hook provides access to the controlled code, selection state, and setter functions
52
+ * from the CodeControllerContext. It's worth noting that useCode and useDemo handle
53
+ * controlling selection in typical cases.
54
+ *
55
+ * @returns An object containing:
56
+ * - code: The current code being controlled
57
+ * - selection: The current selection state
58
+ * - setCode: Function to update the controlled code
59
+ * - setSelection: Function to update the selection
60
+ * - components: Override components for the preview
61
+ */
62
+ export declare function useControlledCode(): CodeControllerContext | undefined;
@@ -0,0 +1,33 @@
1
+ 'use client';
2
+
3
+ import * as React from 'react';
4
+
5
+ /**
6
+ * Context for controlling the code shown within the CodeHighlighter component.
7
+ *
8
+ * To benefit from server or build-time rendering, the initial code should not be provided
9
+ * to the controller context. It's recommended to only set `code` after the first `setCode`
10
+ * event fires.
11
+ */
12
+
13
+ export var CodeControllerContext = /*#__PURE__*/React.createContext(undefined);
14
+
15
+ /**
16
+ * Hook for accessing the controlled code context.
17
+ *
18
+ * This hook provides access to the controlled code, selection state, and setter functions
19
+ * from the CodeControllerContext. It's worth noting that useCode and useDemo handle
20
+ * controlling selection in typical cases.
21
+ *
22
+ * @returns An object containing:
23
+ * - code: The current code being controlled
24
+ * - selection: The current selection state
25
+ * - setCode: Function to update the controlled code
26
+ * - setSelection: Function to update the selection
27
+ * - components: Override components for the preview
28
+ */
29
+ if (process.env.NODE_ENV !== "production") CodeControllerContext.displayName = "CodeControllerContext";
30
+ export function useControlledCode() {
31
+ var context = React.useContext(CodeControllerContext);
32
+ return context;
33
+ }
@@ -0,0 +1 @@
1
+ export * from "./CodeControllerContext.js";
@@ -0,0 +1 @@
1
+ export * from "./CodeControllerContext.js";
@@ -0,0 +1,8 @@
1
+ import * as React from 'react';
2
+ type Module = NonNullable<unknown>;
3
+ export interface CodeExternalsContext {
4
+ externals?: Record<string, Module>;
5
+ }
6
+ export declare const CodeExternalsContext: React.Context<CodeExternalsContext | undefined>;
7
+ export declare function useCodeExternals(): CodeExternalsContext | undefined;
8
+ export {};
@@ -0,0 +1,8 @@
1
+ 'use client';
2
+
3
+ import * as React from 'react';
4
+ export var CodeExternalsContext = /*#__PURE__*/React.createContext(undefined);
5
+ if (process.env.NODE_ENV !== "production") CodeExternalsContext.displayName = "CodeExternalsContext";
6
+ export function useCodeExternals() {
7
+ return React.useContext(CodeExternalsContext);
8
+ }
@@ -0,0 +1 @@
1
+ export * from "./CodeExternalsContext.js";
@@ -0,0 +1 @@
1
+ export * from "./CodeExternalsContext.js";
@@ -0,0 +1,2 @@
1
+ import type { CodeHighlighterProps } from "./types.js";
2
+ export declare function CodeHighlighter<T extends {}>(props: CodeHighlighterProps<T>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,441 @@
1
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import _regenerator from "@babel/runtime/helpers/esm/regenerator";
3
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
4
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
5
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
6
+ import _extends from "@babel/runtime/helpers/esm/extends";
7
+ import * as React from 'react';
8
+ import { loadVariant } from "./loadVariant.js";
9
+ import { loadFallbackCode } from "./loadFallbackCode.js";
10
+ import { CodeHighlighterClient } from "./CodeHighlighterClient.js";
11
+ import { maybeInitialData } from "./maybeInitialData.js";
12
+ import { hasAllVariants } from "./hasAllVariants.js";
13
+ import { getFileNameFromUrl } from "../pipeline/loaderUtils/getFileNameFromUrl.js";
14
+ import { codeToFallbackProps } from "./codeToFallbackProps.js";
15
+ import * as Errors from "./errors.js";
16
+ import { jsx as _jsx } from "react/jsx-runtime";
17
+ var DEFAULT_HIGHLIGHT_AT = 'stream';
18
+ var DEBUG = false; // Set to true for debugging purposes
19
+
20
+ function createClientProps(props) {
21
+ var highlightAt = props.highlightAt === 'stream' ? 'init' : props.highlightAt;
22
+ var contentProps = _extends({
23
+ code: props.code || props.precompute,
24
+ components: props.components,
25
+ name: props.name,
26
+ slug: props.slug,
27
+ url: props.url,
28
+ variantType: props.variantType
29
+ }, props.contentProps);
30
+ return {
31
+ url: props.url,
32
+ code: props.code,
33
+ precompute: props.precompute,
34
+ components: props.components,
35
+ variants: props.variants,
36
+ variant: props.variant,
37
+ fileName: props.fileName,
38
+ initialVariant: props.initialVariant,
39
+ defaultVariant: props.defaultVariant,
40
+ highlightAt: highlightAt || 'init',
41
+ skipFallback: props.skipFallback,
42
+ controlled: props.controlled,
43
+ name: props.name,
44
+ slug: props.slug,
45
+ // Use processedGlobalsCode if available, otherwise fall back to raw globalsCode
46
+ globalsCode: props.processedGlobalsCode || props.globalsCode,
47
+ // Note: it is important that we render components before passing them to the client
48
+ // otherwise we will get an error because functions can't be serialized
49
+ // On the client, in order to send data to these components, we have to set context
50
+ fallback: props.fallback,
51
+ children: /*#__PURE__*/_jsx(props.Content, _extends({}, contentProps))
52
+ };
53
+ }
54
+ function CodeSourceLoader(_x) {
55
+ return _CodeSourceLoader.apply(this, arguments);
56
+ }
57
+ function _CodeSourceLoader() {
58
+ _CodeSourceLoader = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(props) {
59
+ var loadedCode, processedGlobalsCode, hasStringUrls, globalsPromises, variantNames, variantCodes, processedCode, errors, _iterator2, _step2, item, clientProps, _t2;
60
+ return _regenerator().w(function (_context2) {
61
+ while (1) switch (_context2.p = _context2.n) {
62
+ case 0:
63
+ // Start with the loaded code from precompute, or load it if needed
64
+ loadedCode = props.code || props.precompute;
65
+ if (loadedCode) {
66
+ _context2.n = 5;
67
+ break;
68
+ }
69
+ if (props.loadCodeMeta) {
70
+ _context2.n = 1;
71
+ break;
72
+ }
73
+ throw new Errors.ErrorCodeHighlighterServerMissingLoadCodeMeta();
74
+ case 1:
75
+ if (props.url) {
76
+ _context2.n = 2;
77
+ break;
78
+ }
79
+ throw new Errors.ErrorCodeHighlighterServerMissingUrlForLoadCodeMeta();
80
+ case 2:
81
+ _context2.p = 2;
82
+ _context2.n = 3;
83
+ return props.loadCodeMeta(props.url);
84
+ case 3:
85
+ loadedCode = _context2.v;
86
+ _context2.n = 5;
87
+ break;
88
+ case 4:
89
+ _context2.p = 4;
90
+ _t2 = _context2.v;
91
+ throw new Errors.ErrorCodeHighlighterServerLoadCodeFailure(props.url, _t2);
92
+ case 5:
93
+ // TODO: if props.variant is provided, we should only load that variant
94
+ // Process globalsCode: use already processed version if available, otherwise convert string URLs to Code objects
95
+ processedGlobalsCode = props.processedGlobalsCode;
96
+ if (!(!processedGlobalsCode && props.globalsCode && props.globalsCode.length > 0)) {
97
+ _context2.n = 8;
98
+ break;
99
+ }
100
+ hasStringUrls = props.globalsCode.some(function (item) {
101
+ return typeof item === 'string';
102
+ });
103
+ if (!(hasStringUrls && !props.loadCodeMeta)) {
104
+ _context2.n = 6;
105
+ break;
106
+ }
107
+ throw new Errors.ErrorCodeHighlighterServerMissingLoadCodeMetaForGlobals();
108
+ case 6:
109
+ // Load all string URLs in parallel, keep Code objects as-is
110
+ globalsPromises = props.globalsCode.map(/*#__PURE__*/function () {
111
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(globalItem) {
112
+ var _t;
113
+ return _regenerator().w(function (_context) {
114
+ while (1) switch (_context.p = _context.n) {
115
+ case 0:
116
+ if (!(typeof globalItem === 'string')) {
117
+ _context.n = 4;
118
+ break;
119
+ }
120
+ _context.p = 1;
121
+ _context.n = 2;
122
+ return props.loadCodeMeta(globalItem);
123
+ case 2:
124
+ return _context.a(2, _context.v);
125
+ case 3:
126
+ _context.p = 3;
127
+ _t = _context.v;
128
+ throw new Errors.ErrorCodeHighlighterServerLoadGlobalsFailure(globalItem, _t);
129
+ case 4:
130
+ return _context.a(2, globalItem);
131
+ case 5:
132
+ return _context.a(2);
133
+ }
134
+ }, _callee, null, [[1, 3]]);
135
+ }));
136
+ return function (_x4) {
137
+ return _ref.apply(this, arguments);
138
+ };
139
+ }());
140
+ _context2.n = 7;
141
+ return Promise.all(globalsPromises);
142
+ case 7:
143
+ processedGlobalsCode = _context2.v;
144
+ case 8:
145
+ variantNames = Object.keys(props.components || loadedCode || {});
146
+ _context2.n = 9;
147
+ return Promise.all(variantNames.map(function (variantName) {
148
+ var variantCode = loadedCode[variantName];
149
+ var variantUrl = _typeof(variantCode) === 'object' && variantCode != null && variantCode.url ? variantCode.url : props.url;
150
+
151
+ // Convert processedGlobalsCode to VariantCode | string for this specific variant
152
+ var resolvedGlobalsCode;
153
+ if (processedGlobalsCode && processedGlobalsCode.length > 0) {
154
+ resolvedGlobalsCode = [];
155
+ var _iterator = _createForOfIteratorHelper(processedGlobalsCode),
156
+ _step;
157
+ try {
158
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
159
+ var codeObj = _step.value;
160
+ // Only include if this variant exists in the globalsCode
161
+ var targetVariant = codeObj[variantName];
162
+ if (targetVariant) {
163
+ resolvedGlobalsCode.push(targetVariant);
164
+ }
165
+ }
166
+ } catch (err) {
167
+ _iterator.e(err);
168
+ } finally {
169
+ _iterator.f();
170
+ }
171
+ }
172
+ return loadVariant(variantUrl, variantName, variantCode, {
173
+ sourceParser: props.sourceParser,
174
+ loadSource: props.loadSource,
175
+ loadVariantMeta: props.loadVariantMeta,
176
+ sourceTransformers: props.sourceTransformers,
177
+ globalsCode: resolvedGlobalsCode
178
+ }).then(function (variant) {
179
+ return {
180
+ name: variantName,
181
+ variant: variant
182
+ };
183
+ })["catch"](function (error) {
184
+ return {
185
+ error: error
186
+ };
187
+ });
188
+ }));
189
+ case 9:
190
+ variantCodes = _context2.v;
191
+ processedCode = {};
192
+ errors = [];
193
+ _iterator2 = _createForOfIteratorHelper(variantCodes);
194
+ try {
195
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
196
+ item = _step2.value;
197
+ if ('error' in item) {
198
+ console.error(new Errors.ErrorCodeHighlighterServerLoadVariantFailure(props.url, item.error));
199
+ errors.push(item.error);
200
+ } else {
201
+ processedCode[item.name] = item.variant.code;
202
+ }
203
+ }
204
+ } catch (err) {
205
+ _iterator2.e(err);
206
+ } finally {
207
+ _iterator2.f();
208
+ }
209
+ if (!(errors.length > 0)) {
210
+ _context2.n = 10;
211
+ break;
212
+ }
213
+ throw new Errors.ErrorCodeHighlighterServerLoadVariantsFailure(props.url, errors);
214
+ case 10:
215
+ clientProps = createClientProps(_extends(_extends({}, props), {}, {
216
+ code: processedCode,
217
+ processedGlobalsCode: processedGlobalsCode
218
+ }));
219
+ return _context2.a(2, /*#__PURE__*/_jsx(CodeHighlighterClient, _extends({}, clientProps)));
220
+ }
221
+ }, _callee2, null, [[2, 4]]);
222
+ }));
223
+ return _CodeSourceLoader.apply(this, arguments);
224
+ }
225
+ function renderCodeHighlighter(props) {
226
+ var code = props.code || props.precompute;
227
+ var variants = props.variants || Object.keys(props.components || code || {});
228
+ var allCodeVariantsLoaded = code && hasAllVariants(variants, code, true);
229
+
230
+ // Check if any loader functions are available before trying async loading
231
+ var hasAnyLoaderFunction = !!(props.loadCodeMeta || props.loadVariantMeta || props.loadSource || props.sourceParser || props.sourceTransformers);
232
+ if (!allCodeVariantsLoaded && hasAnyLoaderFunction && !props.forceClient) {
233
+ return /*#__PURE__*/_jsx(CodeSourceLoader, _extends({}, props));
234
+ }
235
+ var clientProps = createClientProps(props);
236
+ return /*#__PURE__*/_jsx(CodeHighlighterClient, _extends({}, clientProps));
237
+ }
238
+
239
+ /**
240
+ * Ensures that the suspense boundary is always rendered, even if none of the children have async operations.
241
+ */
242
+ function CodeHighlighterSuspense(_x2) {
243
+ return _CodeHighlighterSuspense.apply(this, arguments);
244
+ }
245
+ function _CodeHighlighterSuspense() {
246
+ _CodeHighlighterSuspense = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(props) {
247
+ return _regenerator().w(function (_context3) {
248
+ while (1) switch (_context3.n) {
249
+ case 0:
250
+ return _context3.a(2, props.children);
251
+ }
252
+ }, _callee3);
253
+ }));
254
+ return _CodeHighlighterSuspense.apply(this, arguments);
255
+ }
256
+ function renderWithInitialSource(props) {
257
+ var _props$components;
258
+ var ContentLoading = props.ContentLoading;
259
+ var url = props.url,
260
+ slug = props.slug,
261
+ name = props.name,
262
+ initialVariant = props.initialVariant,
263
+ code = props.code,
264
+ initialFilename = props.initialFilename,
265
+ fallbackUsesExtraFiles = props.fallbackUsesExtraFiles,
266
+ fallbackUsesAllVariants = props.fallbackUsesAllVariants;
267
+ var fallbackProps = codeToFallbackProps(initialVariant, code, initialFilename, fallbackUsesExtraFiles, fallbackUsesAllVariants);
268
+
269
+ // Get the component for the selected variant
270
+ var component = (_props$components = props.components) == null ? void 0 : _props$components[initialVariant];
271
+
272
+ // Only include components (plural) if we're also including extraVariants
273
+ var components = fallbackProps.extraVariants ? props.components : undefined;
274
+ var contentProps = _extends(_extends({
275
+ name: name,
276
+ slug: slug,
277
+ url: url,
278
+ initialFilename: initialFilename,
279
+ component: component,
280
+ components: components
281
+ }, fallbackProps), props.contentProps);
282
+ var fallback = /*#__PURE__*/_jsx(ContentLoading, _extends({}, contentProps));
283
+ if (props.forceClient) {
284
+ return renderCodeHighlighter(_extends(_extends({}, props), {}, {
285
+ fallback: fallback
286
+ }));
287
+ }
288
+ return /*#__PURE__*/_jsx(React.Suspense, {
289
+ fallback: fallback,
290
+ children: /*#__PURE__*/_jsx(CodeHighlighterSuspense, {
291
+ children: renderCodeHighlighter(_extends(_extends({}, props), {}, {
292
+ fallback: fallback,
293
+ skipFallback: true
294
+ }))
295
+ })
296
+ });
297
+ }
298
+ function CodeInitialSourceLoader(_x3) {
299
+ return _CodeInitialSourceLoader.apply(this, arguments);
300
+ }
301
+ function _CodeInitialSourceLoader() {
302
+ _CodeInitialSourceLoader = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(props) {
303
+ var url, initialVariant, highlightAt, fallbackUsesExtraFiles, fallbackUsesAllVariants, sourceParser, loadSource, loadVariantMeta, loadCodeMeta, fileName, variants, globalsCode, ContentLoading, _yield$loadFallbackCo, code, initialFilename, initialSource, initialExtraFiles, processedGlobalsCode;
304
+ return _regenerator().w(function (_context4) {
305
+ while (1) switch (_context4.n) {
306
+ case 0:
307
+ url = props.url, initialVariant = props.initialVariant, highlightAt = props.highlightAt, fallbackUsesExtraFiles = props.fallbackUsesExtraFiles, fallbackUsesAllVariants = props.fallbackUsesAllVariants, sourceParser = props.sourceParser, loadSource = props.loadSource, loadVariantMeta = props.loadVariantMeta, loadCodeMeta = props.loadCodeMeta, fileName = props.fileName, variants = props.variants, globalsCode = props.globalsCode, ContentLoading = props.ContentLoading;
308
+ if (url) {
309
+ _context4.n = 1;
310
+ break;
311
+ }
312
+ throw new Errors.ErrorCodeHighlighterServerMissingUrl();
313
+ case 1:
314
+ _context4.n = 2;
315
+ return loadFallbackCode(url, initialVariant, props.code, {
316
+ shouldHighlight: highlightAt === 'init',
317
+ fallbackUsesExtraFiles: fallbackUsesExtraFiles,
318
+ fallbackUsesAllVariants: fallbackUsesAllVariants,
319
+ sourceParser: sourceParser,
320
+ loadSource: loadSource,
321
+ loadVariantMeta: loadVariantMeta,
322
+ loadCodeMeta: loadCodeMeta,
323
+ initialFilename: fileName,
324
+ variants: variants,
325
+ globalsCode: globalsCode
326
+ });
327
+ case 2:
328
+ _yield$loadFallbackCo = _context4.v;
329
+ code = _yield$loadFallbackCo.code;
330
+ initialFilename = _yield$loadFallbackCo.initialFilename;
331
+ initialSource = _yield$loadFallbackCo.initialSource;
332
+ initialExtraFiles = _yield$loadFallbackCo.initialExtraFiles;
333
+ processedGlobalsCode = _yield$loadFallbackCo.processedGlobalsCode;
334
+ return _context4.a(2, renderWithInitialSource(_extends(_extends({}, props), {}, {
335
+ ContentLoading: ContentLoading,
336
+ code: code,
337
+ initialFilename: initialFilename,
338
+ initialSource: initialSource,
339
+ initialExtraFiles: initialExtraFiles,
340
+ processedGlobalsCode: processedGlobalsCode
341
+ })));
342
+ }
343
+ }, _callee4);
344
+ }));
345
+ return _CodeInitialSourceLoader.apply(this, arguments);
346
+ }
347
+ export function CodeHighlighter(props) {
348
+ var _code, _props$components2;
349
+ // Validate mutually exclusive props
350
+ if (props.children && (props.code || props.precompute)) {
351
+ throw new Errors.ErrorCodeHighlighterServerInvalidProps();
352
+ }
353
+
354
+ // Handle children as string -> Default variant
355
+ var code = props.code;
356
+ if (props.children && typeof props.children === 'string') {
357
+ var fileName = props.fileName || (props.url ? getFileNameFromUrl(props.url).fileName : undefined);
358
+ code = {
359
+ Default: {
360
+ fileName: fileName,
361
+ source: props.children,
362
+ url: props.url
363
+ }
364
+ };
365
+ }
366
+ var variants = props.variants || Object.keys(props.components || code || props.precompute || {});
367
+ if (variants.length === 0) {
368
+ throw new Errors.ErrorCodeHighlighterServerMissingData();
369
+ }
370
+
371
+ // Validate fileName is provided when extraFiles are present
372
+ if (code) {
373
+ for (var _i = 0, _Object$entries = Object.entries(code); _i < _Object$entries.length; _i++) {
374
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
375
+ variantName = _Object$entries$_i[0],
376
+ variantCode = _Object$entries$_i[1];
377
+ if (_typeof(variantCode) === 'object' && variantCode != null && variantCode.extraFiles && Object.keys(variantCode.extraFiles).length > 0 && !variantCode.fileName && !variantCode.url) {
378
+ throw new Errors.ErrorCodeHighlighterServerMissingFileName(variantName);
379
+ }
380
+ }
381
+ }
382
+ var ContentLoading = props.ContentLoading;
383
+ if (!ContentLoading) {
384
+ if (props.highlightAt === 'stream') {
385
+ // if the user explicitly sets highlightAt to 'stream', we need a ContentLoading component
386
+ throw new Errors.ErrorCodeHighlighterServerMissingContentLoading();
387
+ }
388
+ return renderCodeHighlighter(_extends(_extends({}, props), {}, {
389
+ code: code
390
+ }));
391
+ }
392
+
393
+ // Check if any loader functions are available
394
+ var hasAnyLoaderFunction = !!(props.loadCodeMeta || props.loadVariantMeta || props.loadSource || props.sourceParser || props.sourceTransformers);
395
+
396
+ // If no loader functions are available, skip async loading and go directly to client
397
+ if (!hasAnyLoaderFunction) {
398
+ return renderCodeHighlighter(_extends(_extends({}, props), {}, {
399
+ code: code
400
+ }));
401
+ }
402
+ var initialKey = props.initialVariant || props.variant || props.defaultVariant || variants[0];
403
+ var initial = (_code = code) == null ? void 0 : _code[initialKey];
404
+ if (!initial && !((_props$components2 = props.components) != null && _props$components2[initialKey])) {
405
+ throw new Errors.ErrorCodeHighlighterServerMissingVariant(initialKey);
406
+ }
407
+
408
+ // TODO: use initial.filesOrder to determing which source to use
409
+ var highlightAt = props.highlightAt || DEFAULT_HIGHLIGHT_AT;
410
+ var _maybeInitialData = maybeInitialData(variants, initialKey, code || props.precompute, undefined,
411
+ // TODO: use initial.filesOrder if provided?
412
+ highlightAt === 'init', props.fallbackUsesExtraFiles, props.fallbackUsesAllVariants),
413
+ initialData = _maybeInitialData.initialData,
414
+ reason = _maybeInitialData.reason;
415
+ if (!initialData) {
416
+ if (DEBUG) {
417
+ // eslint-disable-next-line no-console
418
+ console.log('Initial data not found:', reason);
419
+ }
420
+ if (props.forceClient) {
421
+ if (highlightAt === 'init') {
422
+ throw new Errors.ErrorCodeHighlighterServerInvalidClientMode();
423
+ }
424
+ return renderCodeHighlighter(_extends(_extends({}, props), {}, {
425
+ code: code
426
+ }));
427
+ }
428
+ return /*#__PURE__*/_jsx(CodeInitialSourceLoader, _extends(_extends({}, props), {}, {
429
+ ContentLoading: ContentLoading,
430
+ initialVariant: initialKey
431
+ }));
432
+ }
433
+ return renderWithInitialSource(_extends(_extends({}, props), {}, {
434
+ code: initialData.code,
435
+ ContentLoading: ContentLoading,
436
+ initialVariant: initialKey,
437
+ initialFilename: initialData.initialFilename,
438
+ initialSource: initialData.initialSource,
439
+ initialExtraFiles: initialData.initialExtraFiles
440
+ }));
441
+ }
@@ -0,0 +1,2 @@
1
+ import { CodeHighlighterClientProps } from "./types.js";
2
+ export declare function CodeHighlighterClient(props: CodeHighlighterClientProps): import("react/jsx-runtime").JSX.Element;