@mui/internal-docs-infra 0.3.1-canary.2 → 0.3.1-canary.4

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 (110) hide show
  1. package/esm/CodeControllerContext/CodeControllerContext.js +2 -2
  2. package/esm/CodeExternalsContext/CodeExternalsContext.js +1 -1
  3. package/esm/CodeHighlighter/CodeHighlighter.js +252 -327
  4. package/esm/CodeHighlighter/CodeHighlighterClient.js +447 -653
  5. package/esm/CodeHighlighter/CodeHighlighterContext.js +2 -2
  6. package/esm/CodeHighlighter/CodeHighlighterFallbackContext.js +2 -2
  7. package/esm/CodeHighlighter/codeToFallbackProps.js +21 -37
  8. package/esm/CodeHighlighter/errors.js +248 -400
  9. package/esm/CodeHighlighter/parseControlledCode.js +12 -20
  10. package/esm/CodeHighlighter/types.d.ts +7 -1
  11. package/esm/CodeProvider/CodeContext.js +3 -3
  12. package/esm/CodeProvider/CodeProvider.js +31 -40
  13. package/esm/abstractCreateDemo/abstractCreateDemo.js +13 -17
  14. package/esm/abstractCreateDemoClient/abstractCreateDemoClient.js +12 -12
  15. package/esm/cli/index.js +1 -1
  16. package/esm/cli/runValidate.js +160 -264
  17. package/esm/createDemoData/createDemoData.js +11 -12
  18. package/esm/createSitemap/createSitemap.js +2 -2
  19. package/esm/pipeline/getFileConventions/fileConventions.js +1 -1
  20. package/esm/pipeline/getFileConventions/getFileConventions.js +2 -15
  21. package/esm/pipeline/hastUtils/hastUtils.js +16 -17
  22. package/esm/pipeline/loadCodeVariant/addCodeVariantPaths.js +24 -24
  23. package/esm/pipeline/loadCodeVariant/applyCodeTransform.js +12 -22
  24. package/esm/pipeline/loadCodeVariant/calculateMainFilePath.js +30 -37
  25. package/esm/pipeline/loadCodeVariant/computeHastDeltas.js +107 -185
  26. package/esm/pipeline/loadCodeVariant/diffHast.js +18 -53
  27. package/esm/pipeline/loadCodeVariant/examineCodeVariant.js +24 -27
  28. package/esm/pipeline/loadCodeVariant/flattenCodeVariant.js +9 -10
  29. package/esm/pipeline/loadCodeVariant/hasAllCodeVariants.js +5 -5
  30. package/esm/pipeline/loadCodeVariant/loadCodeFallback.js +517 -727
  31. package/esm/pipeline/loadCodeVariant/loadCodeVariant.js +683 -1032
  32. package/esm/pipeline/loadCodeVariant/maybeCodeInitialData.js +14 -20
  33. package/esm/pipeline/loadCodeVariant/mergeCodeMetadata.js +53 -63
  34. package/esm/pipeline/loadCodeVariant/parseCode.js +40 -48
  35. package/esm/pipeline/loadCodeVariant/pathUtils.js +43 -64
  36. package/esm/pipeline/loadCodeVariant/transformSource.js +55 -125
  37. package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.js +160 -221
  38. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js +377 -479
  39. package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.js +171 -173
  40. package/esm/pipeline/loadPrecomputedCodeHighlighter/performanceLogger.js +14 -30
  41. package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.js +19 -21
  42. package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.js +37 -71
  43. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.js +3 -9
  44. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.js +54 -80
  45. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.js +71 -98
  46. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.js +5 -5
  47. package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.js +161 -211
  48. package/esm/pipeline/loadPrecomputedSitemap/loadPrecomputedSitemap.js +159 -207
  49. package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.js +42 -64
  50. package/esm/pipeline/loadServerCodeMeta/resolveModulePathWithFs.js +20 -96
  51. package/esm/pipeline/loadServerPageIndex/loadServerPageIndex.js +66 -85
  52. package/esm/pipeline/loadServerSitemap/loadServerSitemap.js +71 -118
  53. package/esm/pipeline/loadServerSource/loadServerSource.js +121 -148
  54. package/esm/pipeline/loaderUtils/externalsToPackages.js +7 -7
  55. package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.js +8 -12
  56. package/esm/pipeline/loaderUtils/fileUrlToPortablePath.js +5 -5
  57. package/esm/pipeline/loaderUtils/getFileNameFromUrl.js +19 -29
  58. package/esm/pipeline/loaderUtils/getLanguageFromExtension.d.ts +24 -0
  59. package/esm/pipeline/loaderUtils/getLanguageFromExtension.js +62 -0
  60. package/esm/pipeline/loaderUtils/index.d.ts +2 -1
  61. package/esm/pipeline/loaderUtils/index.js +2 -1
  62. package/esm/pipeline/loaderUtils/mergeExternals.js +15 -35
  63. package/esm/pipeline/loaderUtils/parseImportsAndComments.js +413 -433
  64. package/esm/pipeline/loaderUtils/processRelativeImports.js +153 -239
  65. package/esm/pipeline/loaderUtils/resolveModulePath.js +544 -1303
  66. package/esm/pipeline/loaderUtils/rewriteImports.js +73 -111
  67. package/esm/pipeline/parseSource/addLineGutters.js +33 -45
  68. package/esm/pipeline/parseSource/grammars.d.ts +12 -1
  69. package/esm/pipeline/parseSource/grammars.js +36 -4
  70. package/esm/pipeline/parseSource/index.d.ts +2 -1
  71. package/esm/pipeline/parseSource/index.js +2 -1
  72. package/esm/pipeline/parseSource/parseSource.js +23 -32
  73. package/esm/pipeline/syncPageIndex/createMarkdownNodes.js +32 -55
  74. package/esm/pipeline/syncPageIndex/mergeMetadataMarkdown.js +107 -160
  75. package/esm/pipeline/syncPageIndex/metadataToMarkdown.js +846 -1033
  76. package/esm/pipeline/syncPageIndex/syncPageIndex.js +291 -438
  77. package/esm/pipeline/transformHtmlCodePrecomputed/transformHtmlCodePrecomputed.js +266 -320
  78. package/esm/pipeline/transformMarkdownBlockquoteCallouts/transformMarkdownBlockquoteCallouts.js +10 -10
  79. package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.js +183 -267
  80. package/esm/pipeline/transformMarkdownDemoLinks/transformMarkdownDemoLinks.js +25 -27
  81. package/esm/pipeline/transformMarkdownMetadata/transformMarkdownMetadata.js +572 -717
  82. package/esm/pipeline/transformMarkdownRelativePaths/transformMarkdownRelativePaths.js +8 -8
  83. package/esm/pipeline/transformTypescriptToJavascript/removeTypes.js +84 -113
  84. package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.js +10 -26
  85. package/esm/useCode/Pre.d.ts +2 -0
  86. package/esm/useCode/Pre.js +58 -60
  87. package/esm/useCode/useCode.js +59 -61
  88. package/esm/useCode/useCodeUtils.js +54 -63
  89. package/esm/useCode/useCopyFunctionality.js +10 -9
  90. package/esm/useCode/useFileNavigation.js +171 -209
  91. package/esm/useCode/useSourceEditing.js +17 -14
  92. package/esm/useCode/useTransformManagement.js +23 -26
  93. package/esm/useCode/useUIState.js +12 -20
  94. package/esm/useCode/useVariantSelection.js +62 -79
  95. package/esm/useCopier/index.js +29 -56
  96. package/esm/useDemo/createCodeSandbox.js +12 -15
  97. package/esm/useDemo/createStackBlitz.js +14 -20
  98. package/esm/useDemo/exportVariant.js +200 -180
  99. package/esm/useDemo/exportVariantAsCra.js +22 -25
  100. package/esm/useDemo/useDemo.js +80 -84
  101. package/esm/useErrors/ErrorsContext.js +1 -1
  102. package/esm/useErrors/useErrors.js +3 -3
  103. package/esm/useLocalStorageState/useLocalStorageState.js +23 -39
  104. package/esm/usePreference/PreferencesProvider.js +1 -1
  105. package/esm/usePreference/usePreference.js +9 -11
  106. package/esm/useSearch/useSearch.js +290 -387
  107. package/esm/useUrlHashState/useUrlHashState.js +11 -14
  108. package/esm/withDocsInfra/withDeploymentConfig.js +26 -21
  109. package/esm/withDocsInfra/withDocsInfra.js +99 -101
  110. package/package.json +7 -4
@@ -1,4 +1,3 @@
1
- import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
1
  /**
3
2
  * Interface for rewrite replacement operations.
4
3
  */
@@ -14,78 +13,66 @@ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIt
14
13
  * @returns The source code with import statements rewritten to const declarations
15
14
  */
16
15
  export function rewriteImportsToNull(source, importPathsToRewrite, importResult) {
17
- var replacements = [];
16
+ const replacements = [];
18
17
 
19
18
  // Find all import statements to rewrite
20
- importPathsToRewrite.forEach(function (importPath) {
21
- var importData = importResult[importPath];
19
+ importPathsToRewrite.forEach(importPath => {
20
+ const importData = importResult[importPath];
22
21
  if (importData && importData.positions.length > 0) {
23
22
  // For each position (there should typically be one per import statement)
24
- var _iterator = _createForOfIteratorHelper(importData.positions),
25
- _step;
26
- try {
27
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
28
- var position = _step.value;
29
- // Parse backwards from the quote to find 'import' keyword
30
- var importStart = position.start;
31
- var targetWord = 'import';
32
- while (importStart >= targetWord.length) {
33
- var slice = source.slice(importStart - targetWord.length, importStart);
34
- var prevChar = importStart > targetWord.length ? source[importStart - targetWord.length - 1] : '';
35
- if (slice === targetWord && (!prevChar || /\s/.test(prevChar))) {
36
- importStart -= targetWord.length;
37
- break;
38
- }
39
- importStart -= 1;
23
+ for (const position of importData.positions) {
24
+ // Parse backwards from the quote to find 'import' keyword
25
+ let importStart = position.start;
26
+ const targetWord = 'import';
27
+ while (importStart >= targetWord.length) {
28
+ const slice = source.slice(importStart - targetWord.length, importStart);
29
+ const prevChar = importStart > targetWord.length ? source[importStart - targetWord.length - 1] : '';
30
+ if (slice === targetWord && (!prevChar || /\s/.test(prevChar))) {
31
+ importStart -= targetWord.length;
32
+ break;
40
33
  }
34
+ importStart -= 1;
35
+ }
41
36
 
42
- // Parse forwards from after the closing quote to find semicolon/newline
43
- // position.end points to the character AFTER the closing quote
44
- var importEnd = position.end;
45
-
46
- // Check for optional semicolon
47
- if (importEnd < source.length && source[importEnd] === ';') {
48
- importEnd += 1;
49
- }
37
+ // Parse forwards from after the closing quote to find semicolon/newline
38
+ // position.end points to the character AFTER the closing quote
39
+ let importEnd = position.end;
50
40
 
51
- // Check for newline after the statement
52
- var hasTrailingNewline = false;
53
- if (importEnd < source.length && source[importEnd] === '\n') {
54
- hasTrailingNewline = true;
55
- importEnd += 1;
56
- }
41
+ // Check for optional semicolon
42
+ if (importEnd < source.length && source[importEnd] === ';') {
43
+ importEnd += 1;
44
+ }
57
45
 
58
- // Generate const declarations from import names
59
- var constDeclarations = importData.names.map(function (nameInfo) {
60
- var varName = nameInfo.alias || nameInfo.name;
61
- return "const ".concat(varName, " = null;");
62
- }).join('\n');
63
-
64
- // Add newline if original import had one
65
- var newText = constDeclarations + (hasTrailingNewline ? '\n' : '');
66
- replacements.push({
67
- start: importStart,
68
- end: importEnd,
69
- newText: newText
70
- });
46
+ // Check for newline after the statement
47
+ let hasTrailingNewline = false;
48
+ if (importEnd < source.length && source[importEnd] === '\n') {
49
+ hasTrailingNewline = true;
50
+ importEnd += 1;
71
51
  }
72
- } catch (err) {
73
- _iterator.e(err);
74
- } finally {
75
- _iterator.f();
52
+
53
+ // Generate const declarations from import names
54
+ const constDeclarations = importData.names.map(nameInfo => {
55
+ const varName = nameInfo.alias || nameInfo.name;
56
+ return `const ${varName} = null;`;
57
+ }).join('\n');
58
+
59
+ // Add newline if original import had one
60
+ const newText = constDeclarations + (hasTrailingNewline ? '\n' : '');
61
+ replacements.push({
62
+ start: importStart,
63
+ end: importEnd,
64
+ newText
65
+ });
76
66
  }
77
67
  }
78
68
  });
79
69
 
80
70
  // Sort replacements by position (descending) to avoid position shifts
81
- replacements.sort(function (a, b) {
82
- return b.start - a.start;
83
- });
71
+ replacements.sort((a, b) => b.start - a.start);
84
72
 
85
73
  // Apply replacements from right to left (using same logic as rewriteImports)
86
- var result = source;
87
- for (var _i = 0, _replacements = replacements; _i < _replacements.length; _i++) {
88
- var replacement = _replacements[_i];
74
+ let result = source;
75
+ for (const replacement of replacements) {
89
76
  result = result.slice(0, replacement.start) + replacement.newText + result.slice(replacement.end);
90
77
  }
91
78
  return result;
@@ -101,36 +88,24 @@ export function rewriteImportsToNull(source, importPathsToRewrite, importResult)
101
88
  * @returns The source code with import statements removed
102
89
  */
103
90
  export function removeImports(source, importPathsToRemove, importResult) {
104
- var linesToRemove = new Set();
91
+ const linesToRemove = new Set();
105
92
 
106
93
  // Find all line numbers that contain imports to remove
107
- importPathsToRemove.forEach(function (importPath) {
108
- var _importResult$importP;
109
- var positions = (_importResult$importP = importResult[importPath]) == null ? void 0 : _importResult$importP.positions;
94
+ importPathsToRemove.forEach(importPath => {
95
+ const positions = importResult[importPath]?.positions;
110
96
  if (positions && positions.length > 0) {
111
- var _iterator2 = _createForOfIteratorHelper(positions),
112
- _step2;
113
- try {
114
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
115
- var position = _step2.value;
116
- // Find which line this position is on
117
- var beforePosition = source.slice(0, position.start);
118
- var lineNumber = (beforePosition.match(/\n/g) || []).length;
119
- linesToRemove.add(lineNumber);
120
- }
121
- } catch (err) {
122
- _iterator2.e(err);
123
- } finally {
124
- _iterator2.f();
97
+ for (const position of positions) {
98
+ // Find which line this position is on
99
+ const beforePosition = source.slice(0, position.start);
100
+ const lineNumber = (beforePosition.match(/\n/g) || []).length;
101
+ linesToRemove.add(lineNumber);
125
102
  }
126
103
  }
127
104
  });
128
105
 
129
106
  // Split source into lines and filter out lines to remove
130
- var lines = source.split('\n');
131
- var filteredLines = lines.filter(function (_, index) {
132
- return !linesToRemove.has(index);
133
- });
107
+ const lines = source.split('\n');
108
+ const filteredLines = lines.filter((_, index) => !linesToRemove.has(index));
134
109
  return filteredLines.join('\n');
135
110
  }
136
111
 
@@ -145,51 +120,38 @@ export function removeImports(source, importPathsToRemove, importResult) {
145
120
  * @returns The source code with rewritten import paths
146
121
  */
147
122
  export function rewriteImports(source, importPathMapping, importResult) {
148
- var replacements = [];
123
+ const replacements = [];
149
124
 
150
125
  // Use precise position-based replacement
151
- importPathMapping.forEach(function (newPath, originalPath) {
152
- var _importResult$origina;
153
- var positions = (_importResult$origina = importResult[originalPath]) == null ? void 0 : _importResult$origina.positions;
126
+ importPathMapping.forEach((newPath, originalPath) => {
127
+ const positions = importResult[originalPath]?.positions;
154
128
  if (positions && positions.length > 0) {
155
129
  // Process all positions where this import path appears
156
- var _iterator3 = _createForOfIteratorHelper(positions),
157
- _step3;
158
- try {
159
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
160
- var position = _step3.value;
161
- // Validate position bounds
162
- if (position.start >= 0 && position.end <= source.length && position.start < position.end) {
163
- // The positions include the quotes, so we need to preserve them
164
- var originalText = source.slice(position.start, position.end);
165
- if (originalText.length > 0) {
166
- var quote = originalText.charAt(0); // Get the original quote character
167
- var newText = "".concat(quote).concat(newPath).concat(quote);
168
- replacements.push({
169
- start: position.start,
170
- end: position.end,
171
- newText: newText
172
- });
173
- }
130
+ for (const position of positions) {
131
+ // Validate position bounds
132
+ if (position.start >= 0 && position.end <= source.length && position.start < position.end) {
133
+ // The positions include the quotes, so we need to preserve them
134
+ const originalText = source.slice(position.start, position.end);
135
+ if (originalText.length > 0) {
136
+ const quote = originalText.charAt(0); // Get the original quote character
137
+ const newText = `${quote}${newPath}${quote}`;
138
+ replacements.push({
139
+ start: position.start,
140
+ end: position.end,
141
+ newText
142
+ });
174
143
  }
175
144
  }
176
- } catch (err) {
177
- _iterator3.e(err);
178
- } finally {
179
- _iterator3.f();
180
145
  }
181
146
  }
182
147
  });
183
148
 
184
149
  // Sort replacements by position (descending) to avoid position shifts
185
- replacements.sort(function (a, b) {
186
- return b.start - a.start;
187
- });
150
+ replacements.sort((a, b) => b.start - a.start);
188
151
 
189
152
  // Apply replacements from right to left
190
- var result = source;
191
- for (var _i2 = 0, _replacements2 = replacements; _i2 < _replacements2.length; _i2++) {
192
- var replacement = _replacements2[_i2];
153
+ let result = source;
154
+ for (const replacement of replacements) {
193
155
  result = result.slice(0, replacement.start) + replacement.newText + result.slice(replacement.end);
194
156
  }
195
157
  return result;
@@ -1,4 +1,3 @@
1
- import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
1
  // Example copied from https://github.com/wooorm/starry-night#example-adding-line-numbers
3
2
 
4
3
  /**
@@ -8,17 +7,17 @@ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIt
8
7
  * @returns The number of lines in the tree
9
8
  */
10
9
  export function countLines(tree) {
11
- var search = /\r?\n|\r/g;
12
- var index = -1;
13
- var start = 0;
14
- var startTextRemainder = '';
15
- var lineNumber = 0;
10
+ const search = /\r?\n|\r/g;
11
+ let index = -1;
12
+ let start = 0;
13
+ let startTextRemainder = '';
14
+ let lineNumber = 0;
16
15
  while (index + 1 < tree.children.length) {
17
16
  index += 1;
18
- var child = tree.children[index];
17
+ const child = tree.children[index];
19
18
  if (child.type === 'text') {
20
- var textStart = 0;
21
- var match = search.exec(child.value);
19
+ let textStart = 0;
20
+ let match = search.exec(child.value);
22
21
  while (match) {
23
22
  lineNumber += 1;
24
23
  start = index + 1;
@@ -39,31 +38,30 @@ export function countLines(tree) {
39
38
  }
40
39
  return lineNumber;
41
40
  }
42
- export function starryNightGutter(tree, sourceLines) {
43
- var frameSize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 120;
41
+ export function starryNightGutter(tree, sourceLines, frameSize = 120) {
44
42
  /** @type {Array<RootContent>} */
45
- var replacement = [];
46
- var search = /\r?\n|\r/g;
47
- var index = -1;
48
- var start = 0;
49
- var startTextRemainder = '';
50
- var lineNumber = 0;
51
- var frameLines = [];
52
- var frameStartLine = 1; // Track the starting line number for the current frame
43
+ const replacement = [];
44
+ const search = /\r?\n|\r/g;
45
+ let index = -1;
46
+ let start = 0;
47
+ let startTextRemainder = '';
48
+ let lineNumber = 0;
49
+ let frameLines = [];
50
+ let frameStartLine = 1; // Track the starting line number for the current frame
53
51
 
54
52
  while (index + 1 < tree.children.length) {
55
53
  index += 1;
56
- var child = tree.children[index];
54
+ const child = tree.children[index];
57
55
  if (child.type === 'text') {
58
- var textStart = 0;
59
- var match = search.exec(child.value);
56
+ let textStart = 0;
57
+ let match = search.exec(child.value);
60
58
  while (match) {
61
59
  // Nodes in this line.
62
- var _line = tree.children.slice(start, index);
60
+ const line = tree.children.slice(start, index);
63
61
 
64
62
  // Prepend text from a partial matched earlier text.
65
63
  if (startTextRemainder) {
66
- _line.unshift({
64
+ line.unshift({
67
65
  type: 'text',
68
66
  value: startTextRemainder
69
67
  });
@@ -72,7 +70,7 @@ export function starryNightGutter(tree, sourceLines) {
72
70
 
73
71
  // Append text from this text.
74
72
  if (match.index > textStart) {
75
- _line.push({
73
+ line.push({
76
74
  type: 'text',
77
75
  value: child.value.slice(textStart, match.index)
78
76
  });
@@ -80,7 +78,7 @@ export function starryNightGutter(tree, sourceLines) {
80
78
 
81
79
  // Add a line, and the eol.
82
80
  lineNumber += 1;
83
- frameLines.push(createLine(_line, lineNumber), {
81
+ frameLines.push(createLine(line, lineNumber), {
84
82
  type: 'text',
85
83
  value: match[0]
86
84
  });
@@ -102,7 +100,7 @@ export function starryNightGutter(tree, sourceLines) {
102
100
  }
103
101
  }
104
102
  }
105
- var line = tree.children.slice(start);
103
+ const line = tree.children.slice(start);
106
104
  // Prepend text from a partial matched earlier text.
107
105
  if (startTextRemainder) {
108
106
  line.unshift({
@@ -123,22 +121,12 @@ export function starryNightGutter(tree, sourceLines) {
123
121
 
124
122
  // If there are multiple frames and sourceLines provided, add dataAsString to each frame
125
123
  if (replacement.length > 1 && sourceLines) {
126
- var _iterator = _createForOfIteratorHelper(replacement),
127
- _step;
128
- try {
129
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
130
- var _frame$properties;
131
- var frame = _step.value;
132
- if (frame.type === 'element' && frame.tagName === 'span' && ((_frame$properties = frame.properties) == null ? void 0 : _frame$properties.className) === 'frame' && typeof frame.properties.dataFrameStartLine === 'number' && typeof frame.properties.dataFrameEndLine === 'number') {
133
- var startLine = frame.properties.dataFrameStartLine - 1; // Convert to 0-based index
134
- var endLine = frame.properties.dataFrameEndLine; // This is already inclusive
135
- frame.properties.dataAsString = sourceLines.slice(startLine, endLine).join('\n');
136
- }
124
+ for (const frame of replacement) {
125
+ if (frame.type === 'element' && frame.tagName === 'span' && frame.properties?.className === 'frame' && typeof frame.properties.dataFrameStartLine === 'number' && typeof frame.properties.dataFrameEndLine === 'number') {
126
+ const startLine = frame.properties.dataFrameStartLine - 1; // Convert to 0-based index
127
+ const endLine = frame.properties.dataFrameEndLine; // This is already inclusive
128
+ frame.properties.dataAsString = sourceLines.slice(startLine, endLine).join('\n');
137
129
  }
138
- } catch (err) {
139
- _iterator.e(err);
140
- } finally {
141
- _iterator.f();
142
130
  }
143
131
  }
144
132
 
@@ -159,11 +147,11 @@ function createLine(children, line) {
159
147
  className: 'line',
160
148
  dataLn: line
161
149
  },
162
- children: children
150
+ children
163
151
  };
164
152
  }
165
153
  function createFrame(frameChildren, sourceLines, startLine, endLine) {
166
- var properties = {
154
+ const properties = {
167
155
  className: 'frame'
168
156
  };
169
157
 
@@ -175,7 +163,7 @@ function createFrame(frameChildren, sourceLines, startLine, endLine) {
175
163
  return {
176
164
  type: 'element',
177
165
  tagName: 'span',
178
- properties: properties,
166
+ properties,
179
167
  children: frameChildren
180
168
  };
181
169
  }
@@ -1,2 +1,13 @@
1
1
  export declare const grammars: import("@wooorm/starry-night").Grammar[];
2
- export declare const extensionMap: Record<string, string>;
2
+ export declare const extensionMap: Record<string, string>;
3
+ /**
4
+ * Maps simplified language names back to grammar scope names.
5
+ * Used when `language` prop is provided instead of fileName.
6
+ */
7
+ export declare const languageToGrammarMap: Record<string, string>;
8
+ /**
9
+ * Gets the grammar scope from a language name.
10
+ * @param language - The language name (e.g., 'tsx', 'css', 'typescript')
11
+ * @returns The grammar scope or undefined if not recognized
12
+ */
13
+ export declare function getGrammarFromLanguage(language: string): string | undefined;
@@ -8,14 +8,13 @@ import textHtmlBasic from '@wooorm/starry-night/text.html.basic';
8
8
  import sourceCss from '@wooorm/starry-night/source.css';
9
9
  import sourceShell from '@wooorm/starry-night/source.shell';
10
10
  import sourceYaml from '@wooorm/starry-night/source.yaml';
11
- export var grammars = [sourceJs, sourceTs, sourceTsx, sourceJson, textMd, sourceMdx,
11
+ export const grammars = [sourceJs, sourceTs, sourceTsx, sourceJson, textMd, sourceMdx,
12
12
  // needs sourceTsx
13
13
  textHtmlBasic, sourceCss, sourceShell, sourceYaml];
14
- export var extensionMap = {
14
+ export const extensionMap = {
15
15
  '.js': 'source.js',
16
16
  '.ts': 'source.ts',
17
17
  '.jsx': 'source.tsx',
18
- // TODO: is there a JSX grammar?
19
18
  '.tsx': 'source.tsx',
20
19
  '.json': 'source.json',
21
20
  '.md': 'text.md',
@@ -24,4 +23,37 @@ export var extensionMap = {
24
23
  '.css': 'source.css',
25
24
  '.sh': 'source.shell',
26
25
  '.yaml': 'source.yaml'
27
- };
26
+ };
27
+
28
+ /**
29
+ * Maps simplified language names back to grammar scope names.
30
+ * Used when `language` prop is provided instead of fileName.
31
+ */
32
+ export const languageToGrammarMap = {
33
+ js: 'source.js',
34
+ javascript: 'source.js',
35
+ ts: 'source.ts',
36
+ typescript: 'source.ts',
37
+ jsx: 'source.tsx',
38
+ tsx: 'source.tsx',
39
+ json: 'source.json',
40
+ md: 'text.md',
41
+ markdown: 'text.md',
42
+ mdx: 'source.mdx',
43
+ html: 'text.html.basic',
44
+ css: 'source.css',
45
+ sh: 'source.shell',
46
+ shell: 'source.shell',
47
+ bash: 'source.shell',
48
+ yaml: 'source.yaml',
49
+ yml: 'source.yaml'
50
+ };
51
+
52
+ /**
53
+ * Gets the grammar scope from a language name.
54
+ * @param language - The language name (e.g., 'tsx', 'css', 'typescript')
55
+ * @returns The grammar scope or undefined if not recognized
56
+ */
57
+ export function getGrammarFromLanguage(language) {
58
+ return languageToGrammarMap[language.toLowerCase()];
59
+ }
@@ -1 +1,2 @@
1
- export * from "./parseSource.js";
1
+ export * from "./parseSource.js";
2
+ export { getGrammarFromLanguage, languageToGrammarMap, extensionMap } from "./grammars.js";
@@ -1 +1,2 @@
1
- export * from "./parseSource.js";
1
+ export * from "./parseSource.js";
2
+ export { getGrammarFromLanguage, languageToGrammarMap, extensionMap } from "./grammars.js";
@@ -1,16 +1,23 @@
1
- import _regenerator from "@babel/runtime/helpers/esm/regenerator";
2
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
1
  import { createStarryNight } from '@wooorm/starry-night';
4
- import { grammars, extensionMap } from "./grammars.js";
2
+ import { grammars, extensionMap, getGrammarFromLanguage } from "./grammars.js";
5
3
  import { starryNightGutter } from "./addLineGutters.js";
6
- var STARRY_NIGHT_KEY = '__docs_infra_starry_night_instance__';
7
- export var parseSource = function parseSource(source, fileName) {
8
- var starryNight = globalThis[STARRY_NIGHT_KEY];
4
+ const STARRY_NIGHT_KEY = '__docs_infra_starry_night_instance__';
5
+ export const parseSource = (source, fileName, language) => {
6
+ const starryNight = globalThis[STARRY_NIGHT_KEY];
9
7
  if (!starryNight) {
10
8
  throw new Error('Starry Night not initialized. Use createParseSource to create an initialized parseSource function.');
11
9
  }
12
- var fileType = fileName.slice(fileName.lastIndexOf('.'));
13
- if (!extensionMap[fileType]) {
10
+
11
+ // Determine the grammar scope: prefer explicit language, then fall back to file extension
12
+ let grammarScope;
13
+ if (language) {
14
+ grammarScope = getGrammarFromLanguage(language);
15
+ }
16
+ if (!grammarScope && fileName) {
17
+ const fileType = fileName.slice(fileName.lastIndexOf('.'));
18
+ grammarScope = extensionMap[fileType];
19
+ }
20
+ if (!grammarScope) {
14
21
  // Return a basic HAST root node with the source text for unsupported file types
15
22
  // TODO: should we split and add line gutters?
16
23
  return {
@@ -21,31 +28,15 @@ export var parseSource = function parseSource(source, fileName) {
21
28
  }]
22
29
  };
23
30
  }
24
- var highlighted = starryNight.highlight(source, extensionMap[fileType]);
25
- var sourceLines = source.split(/\r?\n|\r/);
31
+ const highlighted = starryNight.highlight(source, grammarScope);
32
+ const sourceLines = source.split(/\r?\n|\r/);
26
33
  starryNightGutter(highlighted, sourceLines); // mutates the tree to add line gutters
27
34
 
28
35
  return highlighted;
29
36
  };
30
- export var createParseSource = /*#__PURE__*/function () {
31
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
32
- return _regenerator().w(function (_context) {
33
- while (1) switch (_context.n) {
34
- case 0:
35
- if (globalThis[STARRY_NIGHT_KEY]) {
36
- _context.n = 2;
37
- break;
38
- }
39
- _context.n = 1;
40
- return createStarryNight(grammars);
41
- case 1:
42
- globalThis[STARRY_NIGHT_KEY] = _context.v;
43
- case 2:
44
- return _context.a(2, parseSource);
45
- }
46
- }, _callee);
47
- }));
48
- return function createParseSource() {
49
- return _ref.apply(this, arguments);
50
- };
51
- }();
37
+ export const createParseSource = async () => {
38
+ if (!globalThis[STARRY_NIGHT_KEY]) {
39
+ globalThis[STARRY_NIGHT_KEY] = await createStarryNight(grammars);
40
+ }
41
+ return parseSource;
42
+ };