@mui/internal-docs-infra 0.2.0-alpha.1 → 0.2.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 (45) hide show
  1. package/esm/CodeHighlighter/CodeHighlighter.js +54 -39
  2. package/esm/CodeHighlighter/CodeHighlighterClient.js +77 -33
  3. package/esm/CodeHighlighter/errors.js +1 -1
  4. package/esm/CodeHighlighter/loadFallbackCode.js +6 -2
  5. package/esm/CodeHighlighter/loadVariant.js +64 -22
  6. package/esm/CodeHighlighter/maybeInitialData.d.ts +2 -2
  7. package/esm/CodeHighlighter/maybeInitialData.js +2 -2
  8. package/esm/CodeHighlighter/transformSource.js +57 -17
  9. package/esm/CodeHighlighter/types.d.ts +20 -3
  10. package/esm/abstractCreateDemo/abstractCreateDemo.d.ts +4 -2
  11. package/esm/abstractCreateDemo/abstractCreateDemo.js +2 -1
  12. package/esm/pipeline/hastUtils/hastUtils.d.ts +6 -0
  13. package/esm/pipeline/hastUtils/hastUtils.js +20 -0
  14. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.d.ts +3 -1
  15. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.js +2 -1
  16. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.d.ts +6 -4
  17. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js +443 -258
  18. package/esm/pipeline/loadServerSource/loadServerSource.js +59 -19
  19. package/esm/pipeline/loaderUtils/index.d.ts +1 -1
  20. package/esm/pipeline/loaderUtils/index.js +1 -1
  21. package/esm/pipeline/loaderUtils/parseImportsAndComments.d.ts +91 -0
  22. package/esm/pipeline/loaderUtils/parseImportsAndComments.js +1329 -0
  23. package/esm/pipeline/loaderUtils/processRelativeImports.d.ts +8 -3
  24. package/esm/pipeline/loaderUtils/processRelativeImports.js +237 -118
  25. package/esm/pipeline/loaderUtils/resolveModulePath.d.ts +7 -3
  26. package/esm/pipeline/loaderUtils/resolveModulePath.js +3 -3
  27. package/esm/pipeline/loaderUtils/resolveModulePathWithFs.d.ts +4 -0
  28. package/esm/pipeline/loaderUtils/rewriteImports.d.ts +12 -5
  29. package/esm/pipeline/loaderUtils/rewriteImports.js +56 -26
  30. package/esm/pipeline/transformHtmlCodePrecomputed/index.d.ts +2 -0
  31. package/esm/pipeline/transformHtmlCodePrecomputed/index.js +4 -0
  32. package/esm/pipeline/transformHtmlCodePrecomputed/transformHtmlCodePrecomputed.d.ts +13 -0
  33. package/esm/pipeline/transformHtmlCodePrecomputed/transformHtmlCodePrecomputed.js +415 -0
  34. package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.js +304 -47
  35. package/esm/useCode/Pre.js +5 -0
  36. package/esm/useCode/useFileNavigation.js +4 -0
  37. package/esm/withDocsInfra/withDocsInfra.d.ts +12 -1
  38. package/esm/withDocsInfra/withDocsInfra.js +34 -6
  39. package/package.json +6 -4
  40. package/esm/pipeline/loaderUtils/parseImports.d.ts +0 -19
  41. package/esm/pipeline/loaderUtils/parseImports.js +0 -306
  42. package/esm/pipeline/transformHtmlCode/index.d.ts +0 -2
  43. package/esm/pipeline/transformHtmlCode/index.js +0 -4
  44. package/esm/pipeline/transformHtmlCode/transformHtmlCode.d.ts +0 -13
  45. package/esm/pipeline/transformHtmlCode/transformHtmlCode.js +0 -300
@@ -5,7 +5,7 @@ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
5
  // webpack does not like node: imports
6
6
  // eslint-disable-next-line n/prefer-node-protocol
7
7
  import { readFile } from 'fs/promises';
8
- import { parseImports } from "../loaderUtils/index.js";
8
+ import { parseImportsAndComments } from "../loaderUtils/index.js";
9
9
  import { resolveImportResultWithFs } from "../loaderUtils/resolveModulePathWithFs.js";
10
10
  import { processRelativeImports } from "../loaderUtils/processRelativeImports.js";
11
11
  import { isJavaScriptModule } from "../loaderUtils/resolveModulePath.js";
@@ -33,7 +33,7 @@ export function createLoadServerSource() {
33
33
  storeAt = _options$storeAt === void 0 ? 'flat' : _options$storeAt;
34
34
  return /*#__PURE__*/function () {
35
35
  var _loadSource = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(url) {
36
- var filePath, source, isJavascriptModuleFile, _yield$parseImports, importResult, externals, transformedExternals, _i, _Object$entries, _Object$entries$_i, modulePath, externalImport, relativeImportsCompatible, _i2, _Object$entries2, _Object$entries2$_i, importPath, _Object$entries2$_i$, path, names, includeTypeDefs, resolvedPathsMap, _processRelativeImpor, processedSource, extraFiles, extraDependencies;
36
+ var filePath, source, isJavascriptModuleFile, isCssFile, _yield$parseImportsAn, importResult, externals, transformedExternals, _i, _Object$entries, _Object$entries$_i, modulePath, externalImport, processedSource, extraFiles, extraDependencies, importsCompatible, _i2, _Object$entries2, _Object$entries2$_i, importPath, _Object$entries2$_i$, path, names, positions, result, relativeImportsCompatible, _i3, _Object$entries3, _Object$entries3$_i, _importPath, _Object$entries3$_i$, _path, _names, includeTypeDefs, _positions, resolvedPathsMap, _result;
37
37
  return _regenerator().w(function (_context) {
38
38
  while (1) switch (_context.n) {
39
39
  case 0:
@@ -53,7 +53,8 @@ export function createLoadServerSource() {
53
53
  case 2:
54
54
  // Check if this is a static asset file (non-JS/TS modules)
55
55
  isJavascriptModuleFile = isJavaScriptModule(filePath);
56
- if (isJavascriptModuleFile) {
56
+ isCssFile = filePath.toLowerCase().endsWith('.css');
57
+ if (!(!isJavascriptModuleFile && !isCssFile)) {
57
58
  _context.n = 3;
58
59
  break;
59
60
  }
@@ -62,12 +63,12 @@ export function createLoadServerSource() {
62
63
  });
63
64
  case 3:
64
65
  _context.n = 4;
65
- return parseImports(source, filePath);
66
+ return parseImportsAndComments(source, filePath);
66
67
  case 4:
67
- _yield$parseImports = _context.v;
68
- importResult = _yield$parseImports.relative;
69
- externals = _yield$parseImports.externals;
70
- // Transform externals from parseImports format to simplified format
68
+ _yield$parseImportsAn = _context.v;
69
+ importResult = _yield$parseImportsAn.relative;
70
+ externals = _yield$parseImportsAn.externals;
71
+ // Transform externals from parseImportsAndComments format to simplified format
71
72
  transformedExternals = {};
72
73
  for (_i = 0, _Object$entries = Object.entries(externals); _i < _Object$entries.length; _i++) {
73
74
  _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), modulePath = _Object$entries$_i[0], externalImport = _Object$entries$_i[1];
@@ -88,35 +89,74 @@ export function createLoadServerSource() {
88
89
  externals: Object.keys(transformedExternals).length > 0 ? transformedExternals : undefined
89
90
  });
90
91
  case 5:
91
- // Convert to format expected by resolveImportResultWithFs and processImports
92
- relativeImportsCompatible = {};
92
+ // Convert import result to the format expected by processImports, preserving position data
93
+ importsCompatible = {};
93
94
  for (_i2 = 0, _Object$entries2 = Object.entries(importResult); _i2 < _Object$entries2.length; _i2++) {
94
- _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2), importPath = _Object$entries2$_i[0], _Object$entries2$_i$ = _Object$entries2$_i[1], path = _Object$entries2$_i$.path, names = _Object$entries2$_i$.names, includeTypeDefs = _Object$entries2$_i$.includeTypeDefs;
95
- relativeImportsCompatible[importPath] = _extends({
95
+ _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2), importPath = _Object$entries2$_i[0], _Object$entries2$_i$ = _Object$entries2$_i[1], path = _Object$entries2$_i$.path, names = _Object$entries2$_i$.names, positions = _Object$entries2$_i$.positions;
96
+ importsCompatible[importPath] = {
96
97
  path: path,
97
98
  names: names.map(function (_ref) {
98
99
  var name = _ref.name,
99
100
  alias = _ref.alias;
100
101
  return alias || name;
101
- })
102
+ }),
103
+ positions: positions
104
+ };
105
+ }
106
+ if (!isCssFile) {
107
+ _context.n = 6;
108
+ break;
109
+ }
110
+ // For CSS files, we don't need complex path resolution
111
+ // The parseImportsAndComments function already resolved paths for CSS
112
+ result = processRelativeImports(source, importsCompatible, storeAt);
113
+ processedSource = result.processedSource;
114
+ extraFiles = result.extraFiles;
115
+
116
+ // Build dependencies list for recursive loading (CSS files use direct paths)
117
+ extraDependencies = Object.values(importResult).map(function (_ref2) {
118
+ var path = _ref2.path;
119
+ return path;
120
+ });
121
+ _context.n = 8;
122
+ break;
123
+ case 6:
124
+ // For JavaScript/TypeScript files, resolve paths first
125
+ relativeImportsCompatible = {};
126
+ for (_i3 = 0, _Object$entries3 = Object.entries(importResult); _i3 < _Object$entries3.length; _i3++) {
127
+ _Object$entries3$_i = _slicedToArray(_Object$entries3[_i3], 2), _importPath = _Object$entries3$_i[0], _Object$entries3$_i$ = _Object$entries3$_i[1], _path = _Object$entries3$_i$.path, _names = _Object$entries3$_i$.names, includeTypeDefs = _Object$entries3$_i$.includeTypeDefs, _positions = _Object$entries3$_i$.positions;
128
+ relativeImportsCompatible[_importPath] = _extends({
129
+ path: _path,
130
+ names: _names.map(function (_ref3) {
131
+ var name = _ref3.name,
132
+ alias = _ref3.alias;
133
+ return alias || name;
134
+ }),
135
+ // Use alias if available
136
+ positions: _positions
102
137
  }, includeTypeDefs && {
103
138
  includeTypeDefs: includeTypeDefs
104
139
  });
105
140
  }
106
141
 
107
142
  // Resolve import paths, handling JS/TS modules and static assets appropriately
108
- _context.n = 6;
143
+ _context.n = 7;
109
144
  return resolveImportResultWithFs(relativeImportsCompatible);
110
- case 6:
145
+ case 7:
111
146
  resolvedPathsMap = _context.v;
112
- // Process imports using the consolidated helper function
113
- _processRelativeImpor = processRelativeImports(source, relativeImportsCompatible, resolvedPathsMap, storeAt), processedSource = _processRelativeImpor.processedSource, extraFiles = _processRelativeImpor.extraFiles; // Build dependencies list for recursive loading
114
- extraDependencies = Object.values(importResult).map(function (_ref2) {
115
- var path = _ref2.path;
147
+ // Process imports using the unified helper function
148
+ _result = processRelativeImports(source, importsCompatible, storeAt, true, resolvedPathsMap);
149
+ processedSource = _result.processedSource;
150
+ extraFiles = _result.extraFiles;
151
+
152
+ // Build dependencies list for recursive loading
153
+ extraDependencies = Object.values(importResult).map(function (_ref4) {
154
+ var path = _ref4.path;
116
155
  return resolvedPathsMap.get(path);
117
156
  }).filter(function (path) {
118
157
  return path !== undefined;
119
158
  });
159
+ case 8:
120
160
  return _context.a(2, {
121
161
  source: processedSource,
122
162
  extraFiles: Object.keys(extraFiles).length > 0 ? extraFiles : undefined,
@@ -1,4 +1,4 @@
1
- export * from "./parseImports.js";
1
+ export * from "./parseImportsAndComments.js";
2
2
  export * from "./resolveModulePath.js";
3
3
  export * from "./rewriteImports.js";
4
4
  export * from "./processRelativeImports.js";
@@ -1,4 +1,4 @@
1
- export * from "./parseImports.js";
1
+ export * from "./parseImportsAndComments.js";
2
2
  export * from "./resolveModulePath.js";
3
3
  export * from "./rewriteImports.js";
4
4
  export * from "./processRelativeImports.js";
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Represents a single import name with its properties.
3
+ */
4
+ export interface ImportName {
5
+ /** The imported name or identifier */
6
+ name: string;
7
+ /** The alias used when importing (e.g., 'as newName') */
8
+ alias?: string;
9
+ /** The type of import: default, named, or namespace (*) */
10
+ type: 'default' | 'named' | 'namespace';
11
+ /** Whether this is a TypeScript type-only import */
12
+ isType?: boolean;
13
+ }
14
+ /**
15
+ * Represents the position of an import path in the source code.
16
+ */
17
+ export interface ImportPathPosition {
18
+ /** The start index of the import path (including quotes) */
19
+ start: number;
20
+ /** The end index of the import path (including quotes) */
21
+ end: number;
22
+ }
23
+ /**
24
+ * Represents an import from a relative path (starts with ./ or ../).
25
+ */
26
+ export interface RelativeImport {
27
+ /** The resolved absolute path to the imported file */
28
+ path: string;
29
+ /** Array of imported names from this module */
30
+ names: ImportName[];
31
+ /** Whether TypeScript type definitions should be included for this import */
32
+ includeTypeDefs?: true;
33
+ /** Array of positions where this import path appears in the source code */
34
+ positions: ImportPathPosition[];
35
+ }
36
+ /**
37
+ * Represents an import from an external package (node_modules).
38
+ */
39
+ export interface ExternalImport {
40
+ /** Array of imported names from this external package */
41
+ names: ImportName[];
42
+ /** Array of positions where this import path appears in the source code */
43
+ positions: ImportPathPosition[];
44
+ }
45
+ /**
46
+ * The result of parsing import statements from source code.
47
+ */
48
+ export interface ImportsAndComments {
49
+ /** Map of relative import paths to their import details */
50
+ relative: Record<string, RelativeImport>;
51
+ /** Map of external package names to their import details */
52
+ externals: Record<string, ExternalImport>;
53
+ /** The processed code with comments removed (if comment processing was requested) */
54
+ code?: string;
55
+ /** Map of line numbers to arrays of comment content (if comment processing was requested) */
56
+ comments?: Record<number, string[]>;
57
+ }
58
+ /**
59
+ * Parse import statements from JavaScript/TypeScript/CSS code.
60
+ *
61
+ * This function analyzes source code to extract all import statements, categorizing them
62
+ * as either relative imports (local files) or external imports (packages). It supports
63
+ * JavaScript, TypeScript, CSS, and MDX files.
64
+ *
65
+ * Comment processing (stripping/collecting) is performed during import parsing
66
+ * for efficiency. Since we must already parse the entire file character-by-character
67
+ * to correctly identify imports while avoiding false positives in strings, comments,
68
+ * and template literals, it's most efficient to handle comment processing in this
69
+ * same pass rather than requiring separate parsing steps.
70
+ *
71
+ * @param code - The source code to parse
72
+ * @param filePath - The file path, used to determine file type and resolve relative imports
73
+ * @param options - Optional configuration for comment processing
74
+ * @param options.removeCommentsWithPrefix - Array of prefixes; comments starting with these will be stripped from output
75
+ * @param options.notableCommentsPrefix - Array of prefixes; comments starting with these will be collected regardless of stripping
76
+ * @returns Promise resolving to parsed import data, optionally including processed code and collected comments
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const result = await parseImportsAndComments(
81
+ * 'import React from "react";\nimport { Button } from "./Button";',
82
+ * '/src/App.tsx'
83
+ * );
84
+ * // result.externals['react'] contains the React import
85
+ * // result.relative['./Button'] contains the Button import
86
+ * ```
87
+ */
88
+ export declare function parseImportsAndComments(code: string, filePath: string, options?: {
89
+ removeCommentsWithPrefix?: string[];
90
+ notableCommentsPrefix?: string[];
91
+ }): Promise<ImportsAndComments>;