@mui/internal-docs-infra 0.1.1-canary.9 → 0.2.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +3 -11
- package/esm/CodeControllerContext/CodeControllerContext.d.ts +62 -0
- package/esm/CodeControllerContext/CodeControllerContext.js +33 -0
- package/esm/CodeControllerContext/index.d.ts +1 -0
- package/esm/CodeControllerContext/index.js +1 -0
- package/esm/CodeExternalsContext/CodeExternalsContext.d.ts +8 -0
- package/esm/CodeExternalsContext/CodeExternalsContext.js +8 -0
- package/esm/CodeExternalsContext/index.d.ts +1 -0
- package/esm/CodeExternalsContext/index.js +1 -0
- package/esm/CodeHighlighter/CodeHighlighter.d.ts +2 -0
- package/esm/CodeHighlighter/CodeHighlighter.js +441 -0
- package/esm/CodeHighlighter/CodeHighlighterClient.d.ts +2 -0
- package/esm/CodeHighlighter/CodeHighlighterClient.js +984 -0
- package/esm/CodeHighlighter/CodeHighlighterContext.d.ts +16 -0
- package/esm/CodeHighlighter/CodeHighlighterContext.js +15 -0
- package/esm/CodeHighlighter/CodeHighlighterFallbackContext.d.ts +7 -0
- package/esm/CodeHighlighter/CodeHighlighterFallbackContext.js +14 -0
- package/esm/CodeHighlighter/addPathsToVariant.d.ts +14 -0
- package/esm/CodeHighlighter/addPathsToVariant.js +68 -0
- package/esm/CodeHighlighter/applyTransform.d.ts +19 -0
- package/esm/CodeHighlighter/applyTransform.js +75 -0
- package/esm/CodeHighlighter/calculateMainFilePath.d.ts +1 -0
- package/esm/CodeHighlighter/calculateMainFilePath.js +108 -0
- package/esm/CodeHighlighter/codeToFallbackProps.d.ts +2 -0
- package/esm/CodeHighlighter/codeToFallbackProps.js +73 -0
- package/esm/CodeHighlighter/errors.d.ts +141 -0
- package/esm/CodeHighlighter/errors.js +441 -0
- package/esm/CodeHighlighter/examineVariant.d.ts +25 -0
- package/esm/CodeHighlighter/examineVariant.js +73 -0
- package/esm/CodeHighlighter/hasAllVariants.d.ts +27 -0
- package/esm/CodeHighlighter/hasAllVariants.js +63 -0
- package/esm/CodeHighlighter/index.d.ts +1 -0
- package/esm/CodeHighlighter/index.js +1 -0
- package/esm/CodeHighlighter/loadFallbackCode.d.ts +10 -0
- package/esm/CodeHighlighter/loadFallbackCode.js +679 -0
- package/esm/CodeHighlighter/loadVariant.d.ts +12 -0
- package/esm/CodeHighlighter/loadVariant.js +1044 -0
- package/esm/CodeHighlighter/maybeInitialData.d.ts +108 -0
- package/esm/CodeHighlighter/maybeInitialData.js +192 -0
- package/esm/CodeHighlighter/mergeMetadata.d.ts +40 -0
- package/esm/CodeHighlighter/mergeMetadata.js +165 -0
- package/esm/CodeHighlighter/parseCode.d.ts +6 -0
- package/esm/CodeHighlighter/parseCode.js +134 -0
- package/esm/CodeHighlighter/parseControlledCode.d.ts +6 -0
- package/esm/CodeHighlighter/parseControlledCode.js +87 -0
- package/esm/CodeHighlighter/pathUtils.d.ts +120 -0
- package/esm/CodeHighlighter/pathUtils.js +258 -0
- package/esm/CodeHighlighter/transformCode.d.ts +21 -0
- package/esm/CodeHighlighter/transformCode.js +251 -0
- package/esm/CodeHighlighter/transformParsedSource.d.ts +3 -0
- package/esm/CodeHighlighter/transformParsedSource.js +60 -0
- package/esm/CodeHighlighter/transformSource.d.ts +2 -0
- package/esm/CodeHighlighter/transformSource.js +103 -0
- package/esm/CodeHighlighter/types.d.ts +276 -0
- package/esm/CodeHighlighter/types.js +1 -0
- package/esm/CodeProvider/CodeContext.d.ts +31 -0
- package/esm/CodeProvider/CodeContext.js +12 -0
- package/esm/CodeProvider/CodeProvider.d.ts +13 -0
- package/esm/CodeProvider/CodeProvider.js +83 -0
- package/esm/CodeProvider/index.d.ts +1 -0
- package/esm/CodeProvider/index.js +1 -0
- package/esm/abstractCreateDemo/abstractCreateDemo.d.ts +45 -0
- package/esm/abstractCreateDemo/abstractCreateDemo.js +100 -0
- package/esm/abstractCreateDemo/index.d.ts +1 -0
- package/esm/abstractCreateDemo/index.js +1 -0
- package/esm/abstractCreateDemoClient/abstractCreateDemoClient.d.ts +32 -0
- package/esm/abstractCreateDemoClient/abstractCreateDemoClient.js +52 -0
- package/esm/abstractCreateDemoClient/index.d.ts +1 -0
- package/esm/abstractCreateDemoClient/index.js +1 -0
- package/esm/createDemoData/createDemoData.d.ts +44 -0
- package/esm/createDemoData/createDemoData.js +74 -0
- package/esm/createDemoData/index.d.ts +1 -0
- package/esm/createDemoData/index.js +1 -0
- package/esm/createDemoData/types.d.ts +25 -0
- package/esm/createDemoData/types.js +1 -0
- package/esm/pipeline/hastUtils/hastUtils.d.ts +11 -0
- package/esm/pipeline/hastUtils/hastUtils.js +67 -0
- package/esm/pipeline/hastUtils/index.d.ts +1 -0
- package/esm/pipeline/hastUtils/index.js +1 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/index.d.ts +2 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/index.js +4 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.d.ts +14 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.js +178 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.d.ts +47 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js +849 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.d.ts +85 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.js +715 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.d.ts +14 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.js +73 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.d.ts +11 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.js +203 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.d.ts +6 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.js +23 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.d.ts +6 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.js +148 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.d.ts +9 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.js +198 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/index.d.ts +2 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/index.js +4 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.d.ts +5 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.js +21 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.d.ts +15 -0
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.js +233 -0
- package/esm/pipeline/loadServerCodeMeta/index.d.ts +1 -0
- package/esm/pipeline/loadServerCodeMeta/index.js +1 -0
- package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.d.ts +24 -0
- package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.js +90 -0
- package/esm/pipeline/loadServerSource/index.d.ts +1 -0
- package/esm/pipeline/loadServerSource/index.js +1 -0
- package/esm/pipeline/loadServerSource/loadServerSource.d.ts +25 -0
- package/esm/pipeline/loadServerSource/loadServerSource.js +134 -0
- package/esm/pipeline/loaderUtils/externalsToPackages.d.ts +1 -0
- package/esm/pipeline/loaderUtils/externalsToPackages.js +46 -0
- package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.d.ts +34 -0
- package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.js +161 -0
- package/esm/pipeline/loaderUtils/getFileNameFromUrl.d.ts +12 -0
- package/esm/pipeline/loaderUtils/getFileNameFromUrl.js +66 -0
- package/esm/pipeline/loaderUtils/index.d.ts +7 -0
- package/esm/pipeline/loaderUtils/index.js +7 -0
- package/esm/pipeline/loaderUtils/mergeExternals.d.ts +32 -0
- package/esm/pipeline/loaderUtils/mergeExternals.js +72 -0
- package/esm/pipeline/loaderUtils/parseImports.d.ts +19 -0
- package/esm/pipeline/loaderUtils/parseImports.js +306 -0
- package/esm/pipeline/loaderUtils/processRelativeImports.d.ts +19 -0
- package/esm/pipeline/loaderUtils/processRelativeImports.js +352 -0
- package/esm/pipeline/loaderUtils/resolveModulePath.d.ts +87 -0
- package/esm/pipeline/loaderUtils/resolveModulePath.js +1435 -0
- package/esm/pipeline/loaderUtils/resolveModulePathWithFs.d.ts +47 -0
- package/esm/pipeline/loaderUtils/resolveModulePathWithFs.js +150 -0
- package/esm/pipeline/loaderUtils/rewriteImports.d.ts +9 -0
- package/esm/pipeline/loaderUtils/rewriteImports.js +35 -0
- package/esm/pipeline/parseSource/addLineGutters.d.ts +9 -0
- package/esm/pipeline/parseSource/addLineGutters.js +181 -0
- package/esm/pipeline/parseSource/grammars.d.ts +2 -0
- package/esm/pipeline/parseSource/grammars.js +27 -0
- package/esm/pipeline/parseSource/index.d.ts +1 -0
- package/esm/pipeline/parseSource/index.js +1 -0
- package/esm/pipeline/parseSource/parseSource.d.ts +3 -0
- package/esm/pipeline/parseSource/parseSource.js +51 -0
- package/esm/pipeline/transformHtmlCode/index.d.ts +2 -0
- package/esm/pipeline/transformHtmlCode/index.js +4 -0
- package/esm/pipeline/transformHtmlCode/transformHtmlCode.d.ts +13 -0
- package/esm/pipeline/transformHtmlCode/transformHtmlCode.js +300 -0
- package/esm/pipeline/transformMarkdownCode/index.d.ts +2 -0
- package/esm/pipeline/transformMarkdownCode/index.js +4 -0
- package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.d.ts +2 -0
- package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.js +514 -0
- package/esm/pipeline/transformTypescriptToJavascript/index.d.ts +1 -0
- package/esm/pipeline/transformTypescriptToJavascript/index.js +1 -0
- package/esm/pipeline/transformTypescriptToJavascript/removeTypes.d.ts +13 -0
- package/esm/pipeline/transformTypescriptToJavascript/removeTypes.js +131 -0
- package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.d.ts +3 -0
- package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.js +31 -0
- package/esm/useCode/Pre.d.ts +15 -0
- package/esm/useCode/Pre.js +164 -0
- package/esm/useCode/index.d.ts +1 -0
- package/esm/useCode/index.js +1 -0
- package/esm/useCode/useCode.d.ts +41 -0
- package/esm/useCode/useCode.js +128 -0
- package/esm/useCode/useCodeUtils.d.ts +44 -0
- package/esm/useCode/useCodeUtils.js +245 -0
- package/esm/useCode/useCopyFunctionality.d.ts +18 -0
- package/esm/useCode/useCopyFunctionality.js +28 -0
- package/esm/useCode/useFileNavigation.d.ts +41 -0
- package/esm/useCode/useFileNavigation.js +453 -0
- package/esm/useCode/useSourceEditing.d.ts +21 -0
- package/esm/useCode/useSourceEditing.js +32 -0
- package/esm/useCode/useTransformManagement.d.ts +30 -0
- package/esm/useCode/useTransformManagement.js +72 -0
- package/esm/useCode/useUIState.d.ts +16 -0
- package/esm/useCode/useUIState.js +21 -0
- package/esm/useCode/useVariantSelection.d.ts +23 -0
- package/esm/useCode/useVariantSelection.js +75 -0
- package/esm/useCopier/index.d.ts +1 -1
- package/esm/useCopier/index.js +5 -5
- package/esm/useDemo/createCodeSandbox.d.ts +15 -0
- package/esm/useDemo/createCodeSandbox.js +42 -0
- package/esm/useDemo/createStackBlitz.d.ts +22 -0
- package/esm/useDemo/createStackBlitz.js +38 -0
- package/esm/useDemo/exportVariant.d.ts +184 -0
- package/esm/useDemo/exportVariant.js +422 -0
- package/esm/useDemo/exportVariantAsCra.d.ts +15 -0
- package/esm/useDemo/exportVariantAsCra.js +57 -0
- package/esm/useDemo/flattenVariant.d.ts +19 -0
- package/esm/useDemo/flattenVariant.js +49 -0
- package/esm/useDemo/index.d.ts +6 -51
- package/esm/useDemo/index.js +6 -104
- package/esm/useDemo/useDemo.d.ts +81 -0
- package/esm/useDemo/useDemo.js +193 -0
- package/esm/useErrors/ErrorsContext.d.ts +6 -0
- package/esm/useErrors/ErrorsContext.js +8 -0
- package/esm/useErrors/index.d.ts +1 -0
- package/esm/useErrors/index.js +1 -0
- package/esm/useErrors/useErrors.d.ts +5 -0
- package/esm/useErrors/useErrors.js +7 -0
- package/esm/useLocalStorageState/index.d.ts +2 -0
- package/esm/useLocalStorageState/index.js +2 -0
- package/esm/useLocalStorageState/useLocalStorageState.d.ts +14 -0
- package/esm/useLocalStorageState/useLocalStorageState.js +128 -0
- package/esm/usePreference/PreferencesProvider.d.ts +6 -0
- package/esm/usePreference/PreferencesProvider.js +8 -0
- package/esm/usePreference/index.d.ts +2 -0
- package/esm/usePreference/index.js +2 -0
- package/esm/usePreference/usePreference.d.ts +2 -0
- package/esm/usePreference/usePreference.js +25 -0
- package/esm/useUrlHashState/index.d.ts +1 -0
- package/esm/useUrlHashState/index.js +1 -0
- package/esm/useUrlHashState/useUrlHashState.d.ts +5 -0
- package/esm/useUrlHashState/useUrlHashState.js +68 -0
- package/esm/withDocsInfra/index.d.ts +1 -0
- package/esm/withDocsInfra/index.js +1 -0
- package/esm/withDocsInfra/withDocsInfra.d.ts +82 -0
- package/esm/withDocsInfra/withDocsInfra.js +147 -0
- package/package.json +178 -11
- package/esm/useCopier/index.d.ts.map +0 -1
- package/esm/useDemo/index.d.ts.map +0 -1
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Code, VariantExtraFiles, VariantSource } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Type guard function that determines if we have sufficient data to render a code highlighter
|
|
4
|
+
* component immediately, or if we need to start loading data first.
|
|
5
|
+
*
|
|
6
|
+
* This function acts as a validation layer to ensure we have the minimal required data
|
|
7
|
+
* to render either a fallback state or the actual code content, helping to prevent
|
|
8
|
+
* rendering errors and provide better user experience.
|
|
9
|
+
*
|
|
10
|
+
* ## Usage Contexts
|
|
11
|
+
*
|
|
12
|
+
* This function is used in two main scenarios:
|
|
13
|
+
*
|
|
14
|
+
* 1. **Server-side rendering (CodeHighlighter)**: Determines if we can render with initial
|
|
15
|
+
* source content immediately, or if we need to load fallback data via `CodeInitialSourceLoader`
|
|
16
|
+
*
|
|
17
|
+
* 2. **Client-side hydration (CodeHighlighterClient)**: Within `useInitialData` hook to determine
|
|
18
|
+
* if we should trigger loading effects or if we can render with available data
|
|
19
|
+
*
|
|
20
|
+
* ## Decision Flow
|
|
21
|
+
*
|
|
22
|
+
* The function checks data availability in this order:
|
|
23
|
+
* 1. Code object exists and contains the requested variant
|
|
24
|
+
* 2. All required variants are available (if `needsAllVariants` is true)
|
|
25
|
+
* 3. Requested file exists (main file or in extraFiles)
|
|
26
|
+
* 4. All extra files are loaded (if `needsAllFiles` is true)
|
|
27
|
+
* 5. Source content is properly highlighted (if `needsHighlight` is true)
|
|
28
|
+
*
|
|
29
|
+
* ## Synchronous vs Asynchronous Behavior
|
|
30
|
+
*
|
|
31
|
+
* This function operates **synchronously** and only validates existing data - it never triggers
|
|
32
|
+
* any loading operations. This design is crucial for performance and rendering strategies:
|
|
33
|
+
*
|
|
34
|
+
* - **Synchronous validation** allows immediate decisions about rendering paths without async overhead
|
|
35
|
+
* - **Enables build-time optimization**: When code is precomputed (e.g., via build-time processing),
|
|
36
|
+
* this function can immediately return `initialData`, avoiding async components entirely
|
|
37
|
+
* - **Separates concerns**: Data validation is separate from data loading, making the codebase
|
|
38
|
+
* more predictable and easier to reason about
|
|
39
|
+
*
|
|
40
|
+
* When `initialData: false` is returned, the calling component is responsible for initiating
|
|
41
|
+
* asynchronous loading operations (e.g., `loadFallbackCode`, `CodeInitialSourceLoader`).
|
|
42
|
+
*
|
|
43
|
+
* @param variants - Array of all available variant names for this code block (e.g., ['javascript', 'typescript'])
|
|
44
|
+
* @param variant - The specific variant we want to display (must exist in variants array)
|
|
45
|
+
* @param code - The code object containing all variant data (may be undefined if not loaded)
|
|
46
|
+
* @param fileName - Optional specific file to display. Resolution logic:
|
|
47
|
+
* - When it matches `variantCode.fileName`, uses the main variant source
|
|
48
|
+
* - When it doesn't match, looks for the file in `variantCode.extraFiles`
|
|
49
|
+
* - When undefined, defaults to the main file of the variant
|
|
50
|
+
* @param needsHighlight - Whether the code needs to be syntax highlighted (source must be highlighted object, not string)
|
|
51
|
+
* @param needsAllFiles - Whether all extra files must be loaded before rendering (checks that all extraFiles have source content)
|
|
52
|
+
* @param needsAllVariants - Whether all variants must be available before rendering (validates using hasAllVariants)
|
|
53
|
+
*
|
|
54
|
+
* @returns Object with either:
|
|
55
|
+
* - `initialData: false` with a `reason` string explaining why data is insufficient for rendering
|
|
56
|
+
* - `initialData: object` containing the validated data ready for immediate rendering, including:
|
|
57
|
+
* - `code`: The full code object
|
|
58
|
+
* - `initialFilename`: The resolved filename (may be undefined if variant has no fileName)
|
|
59
|
+
* - `initialSource`: The source content for the requested file
|
|
60
|
+
* - `initialExtraFiles`: Extra files associated with the variant (if any)
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // Server-side: Check if we can render with initial source or need to load fallback
|
|
65
|
+
* const { initialData, reason } = maybeInitialData(
|
|
66
|
+
* variants,
|
|
67
|
+
* initialKey,
|
|
68
|
+
* code || props.precompute,
|
|
69
|
+
* undefined,
|
|
70
|
+
* highlightAt === 'init',
|
|
71
|
+
* props.fallbackUsesExtraFiles,
|
|
72
|
+
* props.fallbackUsesAllVariants,
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* if (!initialData) {
|
|
76
|
+
* // Need to load fallback data
|
|
77
|
+
* return <CodeInitialSourceLoader {...props} />;
|
|
78
|
+
* }
|
|
79
|
+
*
|
|
80
|
+
* // Client-side: Check if we need to trigger loading effects
|
|
81
|
+
* const { initialData, reason } = React.useMemo(() =>
|
|
82
|
+
* maybeInitialData(
|
|
83
|
+
* variants,
|
|
84
|
+
* variantName,
|
|
85
|
+
* code,
|
|
86
|
+
* fileName,
|
|
87
|
+
* highlightAt === 'init',
|
|
88
|
+
* fallbackUsesExtraFiles,
|
|
89
|
+
* fallbackUsesAllVariants,
|
|
90
|
+
* ), [dependencies]);
|
|
91
|
+
*
|
|
92
|
+
* React.useEffect(() => {
|
|
93
|
+
* if (initialData || isControlled) {
|
|
94
|
+
* return; // No loading needed
|
|
95
|
+
* }
|
|
96
|
+
* // Trigger loadFallbackCode...
|
|
97
|
+
* }, [initialData, reason, ...]);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare function maybeInitialData(variants: string[], variant: string, code?: Code, fileName?: string, needsHighlight?: boolean, needsAllFiles?: boolean, needsAllVariants?: boolean): {
|
|
101
|
+
initialData: false | {
|
|
102
|
+
code: Code;
|
|
103
|
+
initialFilename: string | undefined;
|
|
104
|
+
initialSource: VariantSource;
|
|
105
|
+
initialExtraFiles?: VariantExtraFiles;
|
|
106
|
+
};
|
|
107
|
+
reason?: string;
|
|
108
|
+
};
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
2
|
+
import { hasAllVariants } from "./hasAllVariants.js";
|
|
3
|
+
/**
|
|
4
|
+
* Type guard function that determines if we have sufficient data to render a code highlighter
|
|
5
|
+
* component immediately, or if we need to start loading data first.
|
|
6
|
+
*
|
|
7
|
+
* This function acts as a validation layer to ensure we have the minimal required data
|
|
8
|
+
* to render either a fallback state or the actual code content, helping to prevent
|
|
9
|
+
* rendering errors and provide better user experience.
|
|
10
|
+
*
|
|
11
|
+
* ## Usage Contexts
|
|
12
|
+
*
|
|
13
|
+
* This function is used in two main scenarios:
|
|
14
|
+
*
|
|
15
|
+
* 1. **Server-side rendering (CodeHighlighter)**: Determines if we can render with initial
|
|
16
|
+
* source content immediately, or if we need to load fallback data via `CodeInitialSourceLoader`
|
|
17
|
+
*
|
|
18
|
+
* 2. **Client-side hydration (CodeHighlighterClient)**: Within `useInitialData` hook to determine
|
|
19
|
+
* if we should trigger loading effects or if we can render with available data
|
|
20
|
+
*
|
|
21
|
+
* ## Decision Flow
|
|
22
|
+
*
|
|
23
|
+
* The function checks data availability in this order:
|
|
24
|
+
* 1. Code object exists and contains the requested variant
|
|
25
|
+
* 2. All required variants are available (if `needsAllVariants` is true)
|
|
26
|
+
* 3. Requested file exists (main file or in extraFiles)
|
|
27
|
+
* 4. All extra files are loaded (if `needsAllFiles` is true)
|
|
28
|
+
* 5. Source content is properly highlighted (if `needsHighlight` is true)
|
|
29
|
+
*
|
|
30
|
+
* ## Synchronous vs Asynchronous Behavior
|
|
31
|
+
*
|
|
32
|
+
* This function operates **synchronously** and only validates existing data - it never triggers
|
|
33
|
+
* any loading operations. This design is crucial for performance and rendering strategies:
|
|
34
|
+
*
|
|
35
|
+
* - **Synchronous validation** allows immediate decisions about rendering paths without async overhead
|
|
36
|
+
* - **Enables build-time optimization**: When code is precomputed (e.g., via build-time processing),
|
|
37
|
+
* this function can immediately return `initialData`, avoiding async components entirely
|
|
38
|
+
* - **Separates concerns**: Data validation is separate from data loading, making the codebase
|
|
39
|
+
* more predictable and easier to reason about
|
|
40
|
+
*
|
|
41
|
+
* When `initialData: false` is returned, the calling component is responsible for initiating
|
|
42
|
+
* asynchronous loading operations (e.g., `loadFallbackCode`, `CodeInitialSourceLoader`).
|
|
43
|
+
*
|
|
44
|
+
* @param variants - Array of all available variant names for this code block (e.g., ['javascript', 'typescript'])
|
|
45
|
+
* @param variant - The specific variant we want to display (must exist in variants array)
|
|
46
|
+
* @param code - The code object containing all variant data (may be undefined if not loaded)
|
|
47
|
+
* @param fileName - Optional specific file to display. Resolution logic:
|
|
48
|
+
* - When it matches `variantCode.fileName`, uses the main variant source
|
|
49
|
+
* - When it doesn't match, looks for the file in `variantCode.extraFiles`
|
|
50
|
+
* - When undefined, defaults to the main file of the variant
|
|
51
|
+
* @param needsHighlight - Whether the code needs to be syntax highlighted (source must be highlighted object, not string)
|
|
52
|
+
* @param needsAllFiles - Whether all extra files must be loaded before rendering (checks that all extraFiles have source content)
|
|
53
|
+
* @param needsAllVariants - Whether all variants must be available before rendering (validates using hasAllVariants)
|
|
54
|
+
*
|
|
55
|
+
* @returns Object with either:
|
|
56
|
+
* - `initialData: false` with a `reason` string explaining why data is insufficient for rendering
|
|
57
|
+
* - `initialData: object` containing the validated data ready for immediate rendering, including:
|
|
58
|
+
* - `code`: The full code object
|
|
59
|
+
* - `initialFilename`: The resolved filename (may be undefined if variant has no fileName)
|
|
60
|
+
* - `initialSource`: The source content for the requested file
|
|
61
|
+
* - `initialExtraFiles`: Extra files associated with the variant (if any)
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* // Server-side: Check if we can render with initial source or need to load fallback
|
|
66
|
+
* const { initialData, reason } = maybeInitialData(
|
|
67
|
+
* variants,
|
|
68
|
+
* initialKey,
|
|
69
|
+
* code || props.precompute,
|
|
70
|
+
* undefined,
|
|
71
|
+
* highlightAt === 'init',
|
|
72
|
+
* props.fallbackUsesExtraFiles,
|
|
73
|
+
* props.fallbackUsesAllVariants,
|
|
74
|
+
* );
|
|
75
|
+
*
|
|
76
|
+
* if (!initialData) {
|
|
77
|
+
* // Need to load fallback data
|
|
78
|
+
* return <CodeInitialSourceLoader {...props} />;
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* // Client-side: Check if we need to trigger loading effects
|
|
82
|
+
* const { initialData, reason } = React.useMemo(() =>
|
|
83
|
+
* maybeInitialData(
|
|
84
|
+
* variants,
|
|
85
|
+
* variantName,
|
|
86
|
+
* code,
|
|
87
|
+
* fileName,
|
|
88
|
+
* highlightAt === 'init',
|
|
89
|
+
* fallbackUsesExtraFiles,
|
|
90
|
+
* fallbackUsesAllVariants,
|
|
91
|
+
* ), [dependencies]);
|
|
92
|
+
*
|
|
93
|
+
* React.useEffect(() => {
|
|
94
|
+
* if (initialData || isControlled) {
|
|
95
|
+
* return; // No loading needed
|
|
96
|
+
* }
|
|
97
|
+
* // Trigger loadFallbackCode...
|
|
98
|
+
* }, [initialData, reason, ...]);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function maybeInitialData(variants, variant, code, fileName) {
|
|
102
|
+
var needsHighlight = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
103
|
+
var needsAllFiles = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
|
|
104
|
+
var needsAllVariants = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : false;
|
|
105
|
+
if (!code) {
|
|
106
|
+
return {
|
|
107
|
+
initialData: false,
|
|
108
|
+
reason: 'No code provided'
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (needsAllVariants && !hasAllVariants(variants, code, needsHighlight)) {
|
|
112
|
+
return {
|
|
113
|
+
initialData: false,
|
|
114
|
+
reason: 'Not all variants are available'
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
var variantCode = code[variant];
|
|
118
|
+
if (!variantCode || typeof variantCode === 'string') {
|
|
119
|
+
return {
|
|
120
|
+
initialData: false,
|
|
121
|
+
reason: 'Variant code is not loaded yet'
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (needsAllFiles) {
|
|
125
|
+
if (!variantCode) {
|
|
126
|
+
return {
|
|
127
|
+
initialData: false,
|
|
128
|
+
reason: 'Variant code not found'
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (!variantCode.source) {
|
|
132
|
+
return {
|
|
133
|
+
initialData: false,
|
|
134
|
+
reason: 'Variant source not found'
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
if (variantCode.extraFiles && !Object.keys(variantCode.extraFiles).every(function (file) {
|
|
138
|
+
var _variantCode$extraFil;
|
|
139
|
+
var fileData = (_variantCode$extraFil = variantCode.extraFiles) == null ? void 0 : _variantCode$extraFil[file];
|
|
140
|
+
return _typeof(fileData) === 'object' && (fileData == null ? void 0 : fileData.source) !== undefined;
|
|
141
|
+
})) {
|
|
142
|
+
return {
|
|
143
|
+
initialData: false,
|
|
144
|
+
reason: 'Not all extra files are available'
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// TODO, filename might need to be determined from filesOrder if provided?
|
|
150
|
+
var initialFilename = fileName || variantCode.fileName;
|
|
151
|
+
var fileSource;
|
|
152
|
+
if (fileName && fileName !== variantCode.fileName) {
|
|
153
|
+
var _variantCode$extraFil2;
|
|
154
|
+
var fileData = variantCode == null || (_variantCode$extraFil2 = variantCode.extraFiles) == null ? void 0 : _variantCode$extraFil2[fileName];
|
|
155
|
+
if (!fileData) {
|
|
156
|
+
return {
|
|
157
|
+
initialData: false,
|
|
158
|
+
reason: "File not found in code"
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (typeof fileData === 'string') {
|
|
162
|
+
// It's a URL, not actual source content
|
|
163
|
+
return {
|
|
164
|
+
initialData: false,
|
|
165
|
+
reason: "File is not loaded yet"
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
fileSource = fileData.source;
|
|
169
|
+
} else {
|
|
170
|
+
fileSource = variantCode.source;
|
|
171
|
+
}
|
|
172
|
+
if (!fileSource) {
|
|
173
|
+
return {
|
|
174
|
+
initialData: false,
|
|
175
|
+
reason: "File source not found"
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
if (needsHighlight && typeof fileSource === 'string') {
|
|
179
|
+
return {
|
|
180
|
+
initialData: false,
|
|
181
|
+
reason: 'File needs highlighting'
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
initialData: {
|
|
186
|
+
code: code,
|
|
187
|
+
initialFilename: initialFilename,
|
|
188
|
+
initialSource: fileSource,
|
|
189
|
+
initialExtraFiles: variantCode == null ? void 0 : variantCode.extraFiles
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metadata merging utility for positioning metadata files relative to source files
|
|
3
|
+
*/
|
|
4
|
+
import type { VariantCode, VariantExtraFiles } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Options for merging metadata files
|
|
7
|
+
*/
|
|
8
|
+
interface MergeMetadataOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Optional prefix indicating where source files will be placed.
|
|
11
|
+
* When provided, metadata files will be positioned relative to this prefix
|
|
12
|
+
* PLUS the existing maxSourceBackNavigation from the variant structure.
|
|
13
|
+
*
|
|
14
|
+
* Examples (assuming maxSourceBackNavigation = 2):
|
|
15
|
+
* - 'src/' -> metadata goes to '../../../' (maxSourceBackNavigation + 1 for src/)
|
|
16
|
+
* - 'src/app/' -> metadata goes to '../../../../' (maxSourceBackNavigation + 2 for src/app/)
|
|
17
|
+
* - undefined -> uses only maxSourceBackNavigation (for client merging)
|
|
18
|
+
*/
|
|
19
|
+
metadataPrefix?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Merge metadata files into a variant with proper positioning
|
|
23
|
+
*
|
|
24
|
+
* @param variant - The variant containing source files and potentially mixed metadata/non-metadata files
|
|
25
|
+
* @param metadataFiles - Additional metadata files to merge in
|
|
26
|
+
* @param options - Options for metadata positioning
|
|
27
|
+
* @returns A variant with all metadata files properly positioned
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Extract metadata files from a variant, scoping them according to metadataPrefix
|
|
31
|
+
*
|
|
32
|
+
* @param variant - The variant containing mixed source and metadata files
|
|
33
|
+
* @returns An object with the cleaned variant and extracted metadata
|
|
34
|
+
*/
|
|
35
|
+
export declare function extractMetadata(variant: VariantCode): {
|
|
36
|
+
variant: VariantCode;
|
|
37
|
+
metadata: VariantExtraFiles;
|
|
38
|
+
};
|
|
39
|
+
export declare function mergeMetadata(variant: VariantCode, metadataFiles?: VariantExtraFiles, options?: MergeMetadataOptions): VariantCode;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
4
|
+
var _excluded = ["metadata"];
|
|
5
|
+
/**
|
|
6
|
+
* Metadata merging utility for positioning metadata files relative to source files
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { calculateMaxSourceBackNavigation, removeBackNavigationPrefix, buildPath, calculateMetadataBackNavigation, splitPath } from "./pathUtils.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Options for merging metadata files
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Merge metadata files into a variant with proper positioning
|
|
17
|
+
*
|
|
18
|
+
* @param variant - The variant containing source files and potentially mixed metadata/non-metadata files
|
|
19
|
+
* @param metadataFiles - Additional metadata files to merge in
|
|
20
|
+
* @param options - Options for metadata positioning
|
|
21
|
+
* @returns A variant with all metadata files properly positioned
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Extract metadata files from a variant, scoping them according to metadataPrefix
|
|
25
|
+
*
|
|
26
|
+
* @param variant - The variant containing mixed source and metadata files
|
|
27
|
+
* @returns An object with the cleaned variant and extracted metadata
|
|
28
|
+
*/
|
|
29
|
+
export function extractMetadata(variant) {
|
|
30
|
+
var metadataPrefix = variant.metadataPrefix;
|
|
31
|
+
var extractedMetadata = {};
|
|
32
|
+
var nonMetadataFiles = {};
|
|
33
|
+
if (!variant.extraFiles) {
|
|
34
|
+
return {
|
|
35
|
+
variant: _extends(_extends({}, variant), {}, {
|
|
36
|
+
extraFiles: {}
|
|
37
|
+
}),
|
|
38
|
+
metadata: {}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Calculate how much back navigation to remove using pathUtils
|
|
43
|
+
var backLevelsToRemove = calculateMaxSourceBackNavigation(variant.extraFiles || {});
|
|
44
|
+
if (metadataPrefix) {
|
|
45
|
+
// Add metadataPrefix levels if present
|
|
46
|
+
backLevelsToRemove += splitPath(metadataPrefix).length;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Process all extraFiles
|
|
50
|
+
for (var _i = 0, _Object$entries = Object.entries(variant.extraFiles); _i < _Object$entries.length; _i++) {
|
|
51
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
52
|
+
relativePath = _Object$entries$_i[0],
|
|
53
|
+
fileContent = _Object$entries$_i[1];
|
|
54
|
+
var file = typeof fileContent === 'string' ? {
|
|
55
|
+
source: fileContent
|
|
56
|
+
} : fileContent;
|
|
57
|
+
if (file.metadata) {
|
|
58
|
+
var scopedPath = removeBackNavigationPrefix(relativePath, backLevelsToRemove);
|
|
59
|
+
|
|
60
|
+
// Remove metadata flag when extracting
|
|
61
|
+
var metadataFlag = file.metadata,
|
|
62
|
+
cleanFile = _objectWithoutPropertiesLoose(file, _excluded);
|
|
63
|
+
extractedMetadata[scopedPath] = cleanFile;
|
|
64
|
+
} else {
|
|
65
|
+
nonMetadataFiles[relativePath] = file;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
variant: _extends(_extends({}, variant), {}, {
|
|
70
|
+
extraFiles: nonMetadataFiles
|
|
71
|
+
}),
|
|
72
|
+
metadata: extractedMetadata
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export function mergeMetadata(variant) {
|
|
76
|
+
var _options$metadataPref;
|
|
77
|
+
var metadataFiles = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
78
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
79
|
+
// Determine which metadataPrefix to use
|
|
80
|
+
var targetMetadataPrefix = (_options$metadataPref = options.metadataPrefix) != null ? _options$metadataPref : variant.metadataPrefix;
|
|
81
|
+
|
|
82
|
+
// Check if we need to re-extract metadata due to metadataPrefix change
|
|
83
|
+
var needsReextraction = options.metadataPrefix !== undefined && options.metadataPrefix !== variant.metadataPrefix;
|
|
84
|
+
|
|
85
|
+
// If metadataPrefix is changing, extract existing metadata and reposition everything
|
|
86
|
+
var workingVariant = variant;
|
|
87
|
+
var existingMetadata = {};
|
|
88
|
+
if (needsReextraction && variant.extraFiles) {
|
|
89
|
+
// Extract existing metadata using the old metadataPrefix
|
|
90
|
+
var extracted = extractMetadata(variant);
|
|
91
|
+
workingVariant = extracted.variant;
|
|
92
|
+
existingMetadata = extracted.metadata;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Calculate the positioning level for metadata files
|
|
96
|
+
var metadataBackNavigation = calculateMetadataBackNavigation(workingVariant.extraFiles, targetMetadataPrefix);
|
|
97
|
+
|
|
98
|
+
// Collect all metadata files that need positioning
|
|
99
|
+
var allMetadataFiles = {};
|
|
100
|
+
var nonMetadataFiles = {};
|
|
101
|
+
var positionedMetadataFiles = {};
|
|
102
|
+
|
|
103
|
+
// Process existing extraFiles from working variant
|
|
104
|
+
if (workingVariant.extraFiles) {
|
|
105
|
+
for (var _i2 = 0, _Object$entries2 = Object.entries(workingVariant.extraFiles); _i2 < _Object$entries2.length; _i2++) {
|
|
106
|
+
var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
|
|
107
|
+
relativePath = _Object$entries2$_i[0],
|
|
108
|
+
fileContent = _Object$entries2$_i[1];
|
|
109
|
+
var file = typeof fileContent === 'string' ? {
|
|
110
|
+
source: fileContent
|
|
111
|
+
} : fileContent;
|
|
112
|
+
if (file.metadata) {
|
|
113
|
+
// If we're not changing metadataPrefix, keep existing metadata files where they are
|
|
114
|
+
if (!needsReextraction) {
|
|
115
|
+
positionedMetadataFiles[relativePath] = file;
|
|
116
|
+
} else {
|
|
117
|
+
// Only collect for repositioning if metadataPrefix is changing
|
|
118
|
+
allMetadataFiles[relativePath] = file;
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
nonMetadataFiles[relativePath] = file;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Add extracted metadata (if any) for repositioning
|
|
127
|
+
for (var _i3 = 0, _Object$entries3 = Object.entries(existingMetadata); _i3 < _Object$entries3.length; _i3++) {
|
|
128
|
+
var _Object$entries3$_i = _slicedToArray(_Object$entries3[_i3], 2),
|
|
129
|
+
filePath = _Object$entries3$_i[0],
|
|
130
|
+
_file = _Object$entries3$_i[1];
|
|
131
|
+
allMetadataFiles[filePath] = _extends(_extends({}, typeof _file === 'string' ? {
|
|
132
|
+
source: _file
|
|
133
|
+
} : _file), {}, {
|
|
134
|
+
metadata: true
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Add additional metadata files for positioning
|
|
139
|
+
for (var _i4 = 0, _Object$entries4 = Object.entries(metadataFiles); _i4 < _Object$entries4.length; _i4++) {
|
|
140
|
+
var _Object$entries4$_i = _slicedToArray(_Object$entries4[_i4], 2),
|
|
141
|
+
_filePath = _Object$entries4$_i[0],
|
|
142
|
+
_file2 = _Object$entries4$_i[1];
|
|
143
|
+
allMetadataFiles[_filePath] = _extends(_extends({}, typeof _file2 === 'string' ? {
|
|
144
|
+
source: _file2
|
|
145
|
+
} : _file2), {}, {
|
|
146
|
+
metadata: true
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Position new metadata files at the calculated level using buildPath
|
|
151
|
+
for (var _i5 = 0, _Object$entries5 = Object.entries(allMetadataFiles); _i5 < _Object$entries5.length; _i5++) {
|
|
152
|
+
var _Object$entries5$_i = _slicedToArray(_Object$entries5[_i5], 2),
|
|
153
|
+
originalPath = _Object$entries5$_i[0],
|
|
154
|
+
_file3 = _Object$entries5$_i[1];
|
|
155
|
+
var metadataPath = buildPath(metadataBackNavigation, originalPath);
|
|
156
|
+
positionedMetadataFiles[metadataPath] = _file3;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Combine all files
|
|
160
|
+
var finalExtraFiles = _extends(_extends({}, nonMetadataFiles), positionedMetadataFiles);
|
|
161
|
+
return _extends(_extends({}, workingVariant), {}, {
|
|
162
|
+
extraFiles: finalExtraFiles,
|
|
163
|
+
metadataPrefix: targetMetadataPrefix
|
|
164
|
+
});
|
|
165
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Code, ParseSource } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Pure function to parse code variants and their extraFiles.
|
|
4
|
+
* Converts string sources to HAST nodes and handles hastJson parsing.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseCode(code: Code, parseSource: ParseSource): Code;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
3
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
4
|
+
/**
|
|
5
|
+
* Pure function to parse code variants and their extraFiles.
|
|
6
|
+
* Converts string sources to HAST nodes and handles hastJson parsing.
|
|
7
|
+
*/
|
|
8
|
+
export function parseCode(code, parseSource) {
|
|
9
|
+
var parsed = {};
|
|
10
|
+
for (var _i = 0, _Object$entries = Object.entries(code); _i < _Object$entries.length; _i++) {
|
|
11
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
12
|
+
variant = _Object$entries$_i[0],
|
|
13
|
+
variantCode = _Object$entries$_i[1];
|
|
14
|
+
if (typeof variantCode === 'string') {
|
|
15
|
+
// Already parsed/highlighted
|
|
16
|
+
parsed[variant] = variantCode;
|
|
17
|
+
} else if (variantCode && _typeof(variantCode) === 'object') {
|
|
18
|
+
// Parse if source is available and not already parsed, and we have a filename to determine the file type
|
|
19
|
+
if (variantCode.source && typeof variantCode.source === 'string' && variantCode.fileName) {
|
|
20
|
+
try {
|
|
21
|
+
var hastRoot = parseSource(variantCode.source, variantCode.fileName);
|
|
22
|
+
|
|
23
|
+
// Also parse extraFiles if they contain sources that need parsing
|
|
24
|
+
var parsedExtraFiles = variantCode.extraFiles ? Object.fromEntries(Object.entries(variantCode.extraFiles).map(function (_ref) {
|
|
25
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
26
|
+
fileName = _ref2[0],
|
|
27
|
+
fileContent = _ref2[1];
|
|
28
|
+
if (typeof fileContent === 'string') {
|
|
29
|
+
return [fileName, fileContent]; // Keep string as-is
|
|
30
|
+
}
|
|
31
|
+
if (fileContent && _typeof(fileContent) === 'object' && fileContent.source) {
|
|
32
|
+
if (typeof fileContent.source === 'string') {
|
|
33
|
+
// Parse string source in extraFile
|
|
34
|
+
try {
|
|
35
|
+
var parsedHastRoot = parseSource(fileContent.source, fileName);
|
|
36
|
+
return [fileName, _extends(_extends({}, fileContent), {}, {
|
|
37
|
+
source: parsedHastRoot
|
|
38
|
+
})];
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return [fileName, fileContent]; // Keep original if parsing fails
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return [fileName, fileContent]; // Keep as-is for other cases
|
|
45
|
+
})) : undefined;
|
|
46
|
+
parsed[variant] = _extends(_extends({}, variantCode), {}, {
|
|
47
|
+
source: hastRoot,
|
|
48
|
+
extraFiles: parsedExtraFiles
|
|
49
|
+
});
|
|
50
|
+
} catch (error) {
|
|
51
|
+
// Keep original if parsing fails
|
|
52
|
+
parsed[variant] = variantCode;
|
|
53
|
+
}
|
|
54
|
+
} else if (variantCode.source && typeof variantCode.source === 'string' && !variantCode.fileName) {
|
|
55
|
+
// Return a basic HAST root node with the source text for unsupported file types
|
|
56
|
+
// This indicates that the source has at least passed through the parsing pipeline
|
|
57
|
+
parsed[variant] = _extends(_extends({}, variantCode), {}, {
|
|
58
|
+
source: {
|
|
59
|
+
type: 'root',
|
|
60
|
+
children: [{
|
|
61
|
+
type: 'text',
|
|
62
|
+
value: variantCode.source
|
|
63
|
+
}]
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
} else if (variantCode.source && _typeof(variantCode.source) === 'object' && 'hastJson' in variantCode.source) {
|
|
67
|
+
try {
|
|
68
|
+
// Parse hastJson to HAST nodes
|
|
69
|
+
var _hastRoot = JSON.parse(variantCode.source.hastJson);
|
|
70
|
+
|
|
71
|
+
// Also parse extraFiles if they contain sources that need parsing
|
|
72
|
+
var _parsedExtraFiles = variantCode.extraFiles ? Object.fromEntries(Object.entries(variantCode.extraFiles).map(function (_ref3) {
|
|
73
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
74
|
+
fileName = _ref4[0],
|
|
75
|
+
fileContent = _ref4[1];
|
|
76
|
+
if (typeof fileContent === 'string') {
|
|
77
|
+
return [fileName, fileContent]; // Keep string as-is
|
|
78
|
+
}
|
|
79
|
+
if (fileContent && _typeof(fileContent) === 'object' && fileContent.source) {
|
|
80
|
+
if (typeof fileContent.source === 'string') {
|
|
81
|
+
// Parse string source in extraFile
|
|
82
|
+
try {
|
|
83
|
+
var parsedHastRoot = parseSource(fileContent.source, fileName);
|
|
84
|
+
return [fileName, _extends(_extends({}, fileContent), {}, {
|
|
85
|
+
source: parsedHastRoot
|
|
86
|
+
})];
|
|
87
|
+
} catch (error) {
|
|
88
|
+
return [fileName, fileContent]; // Keep original if parsing fails
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return [fileName, fileContent]; // Keep as-is for other cases
|
|
93
|
+
})) : undefined;
|
|
94
|
+
parsed[variant] = _extends(_extends({}, variantCode), {}, {
|
|
95
|
+
source: _hastRoot,
|
|
96
|
+
extraFiles: _parsedExtraFiles
|
|
97
|
+
});
|
|
98
|
+
} catch (error) {
|
|
99
|
+
// Keep original if parsing fails
|
|
100
|
+
console.error("Failed to parse hastJson for variant ".concat(variant, ":"), error);
|
|
101
|
+
parsed[variant] = variantCode;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
// Already parsed or no source to parse - but still check extraFiles
|
|
105
|
+
var _parsedExtraFiles2 = variantCode.extraFiles ? Object.fromEntries(Object.entries(variantCode.extraFiles).map(function (_ref5) {
|
|
106
|
+
var _ref6 = _slicedToArray(_ref5, 2),
|
|
107
|
+
fileName = _ref6[0],
|
|
108
|
+
fileContent = _ref6[1];
|
|
109
|
+
if (typeof fileContent === 'string') {
|
|
110
|
+
return [fileName, fileContent]; // Keep string as-is
|
|
111
|
+
}
|
|
112
|
+
if (fileContent && _typeof(fileContent) === 'object' && fileContent.source) {
|
|
113
|
+
if (typeof fileContent.source === 'string') {
|
|
114
|
+
// Parse string source in extraFile
|
|
115
|
+
try {
|
|
116
|
+
var parsedHastRoot = parseSource(fileContent.source, fileName);
|
|
117
|
+
return [fileName, _extends(_extends({}, fileContent), {}, {
|
|
118
|
+
source: parsedHastRoot
|
|
119
|
+
})];
|
|
120
|
+
} catch (error) {
|
|
121
|
+
return [fileName, fileContent]; // Keep original if parsing fails
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return [fileName, fileContent]; // Keep as-is for other cases
|
|
126
|
+
})) : undefined;
|
|
127
|
+
parsed[variant] = _extends(_extends({}, variantCode), {}, {
|
|
128
|
+
extraFiles: _parsedExtraFiles2
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return parsed;
|
|
134
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Code, ControlledCode, ParseSource } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Pure function to parse controlled code and convert it to regular Code format.
|
|
4
|
+
* Handles the conversion from ControlledCode (string|null sources) to Code (HAST nodes).
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseControlledCode(controlledCode: ControlledCode, parseSource: ParseSource): Code;
|