@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
@@ -1,306 +0,0 @@
1
- import _regeneratorValues from "@babel/runtime/helpers/esm/regeneratorValues";
2
- import _regenerator from "@babel/runtime/helpers/esm/regenerator";
3
- import _extends from "@babel/runtime/helpers/esm/extends";
4
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
5
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
6
- // webpack does not like node: imports
7
- // eslint-disable-next-line n/prefer-node-protocol
8
- import path from 'path';
9
- export function parseImports(_x, _x2) {
10
- return _parseImports.apply(this, arguments);
11
- }
12
- function _parseImports() {
13
- _parseImports = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(code, _filePath) {
14
- var result, externals, importRegex, sideEffectImportRegex, match, _loop, _ret, sideEffectMatch, _sideEffectMatch, _sideEffectMatch2, modulePathRaw, modulePath, isRelative, resolvedPath;
15
- return _regenerator().w(function (_context2) {
16
- while (1) switch (_context2.n) {
17
- case 0:
18
- result = {};
19
- externals = {}; // Regex to extract regular import statements with 'from' clause
20
- importRegex = /import(?:\s+type)?\s*([^'"]*)\s*from\s*['"]([^'"]+)['"][;]?/g; // Regex to extract side-effect imports (without 'from' clause)
21
- sideEffectImportRegex = /import\s*['"]([^'"]+)['"][;]?/g;
22
- // eslint-disable-next-line no-cond-assign
23
- _loop = /*#__PURE__*/_regenerator().m(function _loop() {
24
- var _match, _match2, fullMatch, importSpecifierRaw, modulePathRaw, includeTypeDefs, importSpecifier, modulePath, isRelative, _resolvedPath, _resolvedPath2, defaultImportMatch, namespaceImportMatch, namedImportsMatch, defaultImport, namespaceImport, namedImportsStr, existing, _existing, cleanedImportsStr, namedImports, _defaultImportMatch, _namespaceImportMatch, _namedImportsMatch, _defaultImport, _namespaceImport, _namedImportsStr, _existing3, _existing4, _cleanedImportsStr, _namedImports;
25
- return _regenerator().w(function (_context) {
26
- while (1) switch (_context.n) {
27
- case 0:
28
- _match = match, _match2 = _slicedToArray(_match, 3), fullMatch = _match2[0], importSpecifierRaw = _match2[1], modulePathRaw = _match2[2]; // Determine if this is a type import by checking if the full match contains "import type"
29
- includeTypeDefs = fullMatch.includes('import type');
30
- importSpecifier = importSpecifierRaw == null ? void 0 : importSpecifierRaw.trim();
31
- modulePath = modulePathRaw == null ? void 0 : modulePathRaw.trim();
32
- if (modulePath) {
33
- _context.n = 1;
34
- break;
35
- }
36
- return _context.a(2, 0);
37
- case 1:
38
- // Check if this is a relative import
39
- isRelative = modulePath.startsWith('./') || modulePath.startsWith('../'); // Handle side-effect imports (no import specifier)
40
- if (importSpecifier) {
41
- _context.n = 2;
42
- break;
43
- }
44
- if (isRelative) {
45
- // Resolve the relative import to an absolute path
46
- _resolvedPath = path.resolve(path.dirname(_filePath), modulePath);
47
- if (!result[modulePath]) {
48
- result[modulePath] = {
49
- path: _resolvedPath,
50
- names: [] // Empty names array for side-effect imports
51
- };
52
- }
53
- } else if (!externals[modulePath]) {
54
- // External side-effect import
55
- externals[modulePath] = {
56
- names: []
57
- };
58
- }
59
- return _context.a(2, 0);
60
- case 2:
61
- if (isRelative) {
62
- // Resolve the relative import to an absolute path
63
- _resolvedPath2 = path.resolve(path.dirname(_filePath), modulePath);
64
- if (!result[modulePath]) {
65
- result[modulePath] = _extends({
66
- path: _resolvedPath2,
67
- names: []
68
- }, includeTypeDefs && {
69
- includeTypeDefs: true
70
- });
71
- } else if (includeTypeDefs && !result[modulePath].includeTypeDefs) {
72
- result[modulePath].includeTypeDefs = true;
73
- }
74
-
75
- // Parse the import specifier to determine the type of import
76
- defaultImportMatch = importSpecifier.match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)\s*(?:,|$)/);
77
- namespaceImportMatch = importSpecifier.match(/\*\s+as\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/);
78
- namedImportsMatch = importSpecifier.match(/\{([^}]+)\}/);
79
- defaultImport = defaultImportMatch == null ? void 0 : defaultImportMatch[1];
80
- namespaceImport = namespaceImportMatch == null ? void 0 : namespaceImportMatch[1];
81
- namedImportsStr = namedImportsMatch == null ? void 0 : namedImportsMatch[1];
82
- if (defaultImport) {
83
- // Check if we already have this default import
84
- existing = result[modulePath].names.find(function (n) {
85
- return n.name === defaultImport && n.type === 'default';
86
- });
87
- if (!existing) {
88
- result[modulePath].names.push(_extends({
89
- name: defaultImport,
90
- type: 'default'
91
- }, includeTypeDefs && {
92
- isType: true
93
- }));
94
- }
95
- }
96
- if (namespaceImport) {
97
- // Check if we already have this namespace import
98
- _existing = result[modulePath].names.find(function (n) {
99
- return n.name === namespaceImport && n.type === 'namespace';
100
- });
101
- if (!_existing) {
102
- result[modulePath].names.push(_extends({
103
- name: namespaceImport,
104
- type: 'namespace'
105
- }, includeTypeDefs && {
106
- isType: true
107
- }));
108
- }
109
- }
110
- if (namedImportsStr) {
111
- // Handle named imports like { ComponentName, Component2 as Alias, type TypeName }
112
- // Clean up the string by removing comments and extra whitespace
113
- cleanedImportsStr = namedImportsStr.replace(/\/\*[\s\S]*?\*\//g, '') // Remove /* comment */ blocks
114
- .replace(/\/\/.*$/gm, '') // Remove // line comments
115
- .trim();
116
- if (cleanedImportsStr) {
117
- namedImports = cleanedImportsStr.split(',').map(function (s) {
118
- return s.trim();
119
- }).filter(function (s) {
120
- return s.length > 0;
121
- });
122
- namedImports.forEach(function (namedImport) {
123
- // Check if this specific import is a type import
124
- var isTypeImport = namedImport.trim().startsWith('type ');
125
-
126
- // Clean up the import name (remove 'type' keyword and handle aliases)
127
- var cleanImport = namedImport.replace(/^type\s+/, ''); // Remove leading 'type'
128
-
129
- // Parse original name and alias
130
- var aliasMatch = cleanImport.match(/(.+?)\s+as\s+(.+)/);
131
- var originalName = aliasMatch ? aliasMatch[1].trim() : cleanImport.trim();
132
- var alias = aliasMatch ? aliasMatch[2].trim() : undefined;
133
-
134
- // Only add if we have a valid name
135
- if (originalName && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(originalName)) {
136
- // Check if we already have this named import
137
- var _existing2 = result[modulePath].names.find(function (n) {
138
- return n.name === originalName && n.type === 'named' && n.alias === alias;
139
- });
140
- if (!_existing2) {
141
- result[modulePath].names.push(_extends(_extends({
142
- name: originalName
143
- }, alias && {
144
- alias: alias
145
- }), {}, {
146
- type: 'named'
147
- }, (includeTypeDefs || isTypeImport) && {
148
- isType: true
149
- }));
150
- }
151
- }
152
- });
153
- }
154
- }
155
- } else {
156
- // This is an external import
157
- if (!externals[modulePath]) {
158
- externals[modulePath] = {
159
- names: []
160
- };
161
- }
162
-
163
- // Parse the import specifier to determine the type of import
164
- _defaultImportMatch = importSpecifier.match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)\s*(?:,|$)/);
165
- _namespaceImportMatch = importSpecifier.match(/\*\s+as\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/);
166
- _namedImportsMatch = importSpecifier.match(/\{([^}]+)\}/);
167
- _defaultImport = _defaultImportMatch == null ? void 0 : _defaultImportMatch[1];
168
- _namespaceImport = _namespaceImportMatch == null ? void 0 : _namespaceImportMatch[1];
169
- _namedImportsStr = _namedImportsMatch == null ? void 0 : _namedImportsMatch[1];
170
- if (_defaultImport) {
171
- // Check if we already have this default import
172
- _existing3 = externals[modulePath].names.find(function (n) {
173
- return n.name === _defaultImport && n.type === 'default';
174
- });
175
- if (!_existing3) {
176
- externals[modulePath].names.push(_extends({
177
- name: _defaultImport,
178
- type: 'default'
179
- }, includeTypeDefs && {
180
- isType: true
181
- }));
182
- }
183
- }
184
- if (_namespaceImport) {
185
- // Check if we already have this namespace import
186
- _existing4 = externals[modulePath].names.find(function (n) {
187
- return n.name === _namespaceImport && n.type === 'namespace';
188
- });
189
- if (!_existing4) {
190
- externals[modulePath].names.push(_extends({
191
- name: _namespaceImport,
192
- type: 'namespace'
193
- }, includeTypeDefs && {
194
- isType: true
195
- }));
196
- }
197
- }
198
- if (_namedImportsStr) {
199
- // Handle named imports like { ComponentName, Component2 as Alias, type TypeName }
200
- // Clean up the string by removing comments and extra whitespace
201
- _cleanedImportsStr = _namedImportsStr.replace(/\/\*[\s\S]*?\*\//g, '') // Remove /* comment */ blocks
202
- .replace(/\/\/.*$/gm, '') // Remove // line comments
203
- .trim();
204
- if (_cleanedImportsStr) {
205
- _namedImports = _cleanedImportsStr.split(',').map(function (s) {
206
- return s.trim();
207
- }).filter(function (s) {
208
- return s.length > 0;
209
- });
210
- _namedImports.forEach(function (namedImport) {
211
- // Check if this specific import is a type import
212
- var isTypeImport = namedImport.trim().startsWith('type ');
213
-
214
- // Clean up the import name (remove 'type' keyword and handle aliases)
215
- var cleanImport = namedImport.replace(/^type\s+/, ''); // Remove leading 'type'
216
-
217
- // Parse original name and alias - for externals we track both
218
- var aliasMatch = cleanImport.match(/(.+?)\s+as\s+(.+)/);
219
- var originalName = aliasMatch ? aliasMatch[1].trim() : cleanImport.trim();
220
- var alias = aliasMatch ? aliasMatch[2].trim() : undefined;
221
-
222
- // Only add if we have a valid name
223
- if (originalName && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(originalName)) {
224
- // Check if we already have this named import
225
- var _existing5 = externals[modulePath].names.find(function (n) {
226
- return n.name === originalName && n.type === 'named' && n.alias === alias;
227
- });
228
- if (!_existing5) {
229
- externals[modulePath].names.push(_extends(_extends({
230
- name: originalName
231
- }, alias && {
232
- alias: alias
233
- }), {}, {
234
- type: 'named'
235
- }, (includeTypeDefs || isTypeImport) && {
236
- isType: true
237
- }));
238
- }
239
- }
240
- });
241
- }
242
- }
243
- }
244
- case 3:
245
- return _context.a(2);
246
- }
247
- }, _loop);
248
- });
249
- case 1:
250
- if (!((match = importRegex.exec(code)) !== null)) {
251
- _context2.n = 4;
252
- break;
253
- }
254
- return _context2.d(_regeneratorValues(_loop()), 2);
255
- case 2:
256
- _ret = _context2.v;
257
- if (!(_ret === 0)) {
258
- _context2.n = 3;
259
- break;
260
- }
261
- return _context2.a(3, 1);
262
- case 3:
263
- _context2.n = 1;
264
- break;
265
- case 4:
266
- if (!((sideEffectMatch = sideEffectImportRegex.exec(code)) !== null)) {
267
- _context2.n = 6;
268
- break;
269
- }
270
- _sideEffectMatch = sideEffectMatch, _sideEffectMatch2 = _slicedToArray(_sideEffectMatch, 2), modulePathRaw = _sideEffectMatch2[1];
271
- modulePath = modulePathRaw == null ? void 0 : modulePathRaw.trim();
272
- if (modulePath) {
273
- _context2.n = 5;
274
- break;
275
- }
276
- return _context2.a(3, 4);
277
- case 5:
278
- // Check if this is a relative import
279
- isRelative = modulePath.startsWith('./') || modulePath.startsWith('../');
280
- if (isRelative) {
281
- // Resolve the relative import to an absolute path
282
- resolvedPath = path.resolve(path.dirname(_filePath), modulePath);
283
- if (!result[modulePath]) {
284
- result[modulePath] = {
285
- path: resolvedPath,
286
- names: [] // Empty names array for side-effect imports
287
- };
288
- }
289
- } else if (!externals[modulePath]) {
290
- // External side-effect import
291
- externals[modulePath] = {
292
- names: []
293
- };
294
- }
295
- _context2.n = 4;
296
- break;
297
- case 6:
298
- return _context2.a(2, {
299
- relative: result,
300
- externals: externals
301
- });
302
- }
303
- }, _callee);
304
- }));
305
- return _parseImports.apply(this, arguments);
306
- }
@@ -1,2 +0,0 @@
1
- import { transformHtmlCode } from "./transformHtmlCode.js";
2
- export default transformHtmlCode;
@@ -1,4 +0,0 @@
1
- // This is the export format expected by a remark plugin.
2
-
3
- import { transformHtmlCode } from "./transformHtmlCode.js";
4
- export default transformHtmlCode;
@@ -1,13 +0,0 @@
1
- import type { Plugin } from 'unified';
2
- /**
3
- * Rehype plugin that transforms pre > code elements to use loadVariant
4
- *
5
- * This plugin:
6
- * 1. Finds pre elements in the HTML AST
7
- * 2. Collects all code children within each pre element
8
- * 3. Creates variants from multiple code elements or single Default variant
9
- * 4. Uses loadVariant to process each variant
10
- * 5. Stores the combined precompute data on the pre element
11
- * 6. Clears all code element contents
12
- */
13
- export declare const transformHtmlCode: Plugin;
@@ -1,300 +0,0 @@
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 { visit } from 'unist-util-visit';
7
- import { loadVariant } from "../../CodeHighlighter/loadVariant.js";
8
- import { createParseSource } from "../parseSource/index.js";
9
- import { TypescriptToJavascriptTransformer } from "../transformTypescriptToJavascript/index.js";
10
- /**
11
- * Maps common language class names to file extensions
12
- * Only includes languages that have corresponding grammars in parseSource/grammars.ts
13
- */
14
- var LANGUAGE_TO_EXTENSION = {
15
- // JavaScript
16
- javascript: 'js',
17
- js: 'js',
18
- // TypeScript
19
- typescript: 'ts',
20
- ts: 'ts',
21
- // TSX/JSX
22
- tsx: 'tsx',
23
- jsx: 'jsx',
24
- // Maps to .jsx but uses tsx grammar
25
-
26
- // JSON
27
- json: 'json',
28
- // Markdown
29
- markdown: 'md',
30
- md: 'md',
31
- // MDX
32
- mdx: 'mdx',
33
- // HTML
34
- html: 'html',
35
- // CSS
36
- css: 'css',
37
- // Shell
38
- shell: 'sh',
39
- bash: 'sh',
40
- sh: 'sh',
41
- // YAML
42
- yaml: 'yaml',
43
- yml: 'yaml'
44
- };
45
-
46
- /**
47
- * Extracts the language from className attribute
48
- */
49
- function extractLanguageFromClassName(className) {
50
- if (!className) {
51
- return null;
52
- }
53
-
54
- // Handle array of class names (HAST format)
55
- var classString = Array.isArray(className) ? className.join(' ') : className;
56
- var match = classString.match(/(?:^|\s)language-(\w+)(?:\s|$)/);
57
- return match ? match[1] : null;
58
- }
59
-
60
- /**
61
- * Gets the filename from data-filename attribute or derives it from language
62
- * Returns undefined if no explicit filename and no recognizable language
63
- */
64
- function getFileName(codeElement) {
65
- var _codeElement$properti, _codeElement$properti2;
66
- // Check for explicit data-filename attribute
67
- var dataFilename = (_codeElement$properti = codeElement.properties) == null ? void 0 : _codeElement$properti.dataFilename;
68
- if (dataFilename && typeof dataFilename === 'string') {
69
- return dataFilename;
70
- }
71
-
72
- // Extract language from className
73
- var className = (_codeElement$properti2 = codeElement.properties) == null ? void 0 : _codeElement$properti2.className;
74
- var language = extractLanguageFromClassName(className);
75
- if (language && LANGUAGE_TO_EXTENSION[language]) {
76
- return "index.".concat(LANGUAGE_TO_EXTENSION[language]);
77
- }
78
-
79
- // Return undefined instead of a fallback - let the system handle gracefully
80
- return undefined;
81
- }
82
-
83
- /**
84
- * Extracts text content from HAST nodes
85
- */
86
- function extractTextContent(node) {
87
- if (node.type === 'text') {
88
- return node.value;
89
- }
90
- if (node.type === 'element' && node.children) {
91
- return node.children.map(function (child) {
92
- return extractTextContent(child);
93
- }).join('');
94
- }
95
- return '';
96
- }
97
-
98
- /**
99
- * Creates variants from multiple code elements within a pre element
100
- */
101
- function createVariantsFromCodeElements(codeElements) {
102
- var variants = {};
103
- if (codeElements.length === 1) {
104
- var _codeElement$properti3;
105
- // Single code element - use "Default" as variant name
106
- var codeElement = codeElements[0];
107
- var sourceCode = extractTextContent(codeElement);
108
- var fileName = getFileName(codeElement);
109
- var variant = {
110
- source: sourceCode,
111
- skipTransforms: !((_codeElement$properti3 = codeElement.properties) != null && _codeElement$properti3.dataTransform)
112
- };
113
-
114
- // Only add fileName if we have one
115
- if (fileName) {
116
- variant.fileName = fileName;
117
- }
118
- variants.Default = variant;
119
- } else {
120
- // Multiple code elements - create appropriate variant names
121
- var languages = codeElements.map(function (element) {
122
- var _element$properties;
123
- var className = (_element$properties = element.properties) == null ? void 0 : _element$properties.className;
124
- return extractLanguageFromClassName(className);
125
- });
126
-
127
- // Check if all languages are the same (or all null)
128
- var uniqueLanguages = Array.from(new Set(languages.filter(Boolean)));
129
- var shouldUseLanguageNames = uniqueLanguages.length > 1;
130
- codeElements.forEach(function (codeElement, index) {
131
- var _codeElement$properti4, _codeElement$properti5;
132
- var sourceCode = extractTextContent(codeElement);
133
- var fileName = getFileName(codeElement);
134
-
135
- // Check for explicit variant name
136
- var dataVariant = (_codeElement$properti4 = codeElement.properties) == null ? void 0 : _codeElement$properti4.dataVariant;
137
- var variantName;
138
- if (dataVariant && typeof dataVariant === 'string') {
139
- variantName = dataVariant;
140
- } else if (shouldUseLanguageNames && languages[index]) {
141
- // Use language name if languages differ
142
- variantName = languages[index].charAt(0).toUpperCase() + languages[index].slice(1);
143
- } else {
144
- // Use numbered variants if languages are the same or unknown
145
- variantName = "Variant ".concat(index + 1);
146
- }
147
- var variant = {
148
- source: sourceCode,
149
- skipTransforms: !((_codeElement$properti5 = codeElement.properties) != null && _codeElement$properti5.dataTransform)
150
- };
151
-
152
- // Only add fileName if we have one
153
- if (fileName) {
154
- variant.fileName = fileName;
155
- }
156
- variants[variantName] = variant;
157
- });
158
- }
159
- return variants;
160
- }
161
-
162
- /**
163
- * Rehype plugin that transforms pre > code elements to use loadVariant
164
- *
165
- * This plugin:
166
- * 1. Finds pre elements in the HTML AST
167
- * 2. Collects all code children within each pre element
168
- * 3. Creates variants from multiple code elements or single Default variant
169
- * 4. Uses loadVariant to process each variant
170
- * 5. Stores the combined precompute data on the pre element
171
- * 6. Clears all code element contents
172
- */
173
- export var transformHtmlCode = function transformHtmlCode() {
174
- return /*#__PURE__*/function () {
175
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(tree) {
176
- var transformPromises, sourceParser, sourceTransformers;
177
- return _regenerator().w(function (_context3) {
178
- while (1) switch (_context3.n) {
179
- case 0:
180
- transformPromises = []; // Get the source parser and transformers
181
- sourceParser = createParseSource();
182
- sourceTransformers = [TypescriptToJavascriptTransformer];
183
- visit(tree, 'element', function (node) {
184
- // Look for pre elements
185
- if (node.tagName === 'pre' && node.children && node.children.length > 0) {
186
- // Find all code elements within this pre
187
- var codeElements = node.children.filter(function (child) {
188
- return child.type === 'element' && child.tagName === 'code';
189
- });
190
- if (codeElements.length > 0) {
191
- var transformPromise = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
192
- var variants, processedCode, variantPromises, variantResults, _iterator, _step, result, _t;
193
- return _regenerator().w(function (_context2) {
194
- while (1) switch (_context2.p = _context2.n) {
195
- case 0:
196
- _context2.p = 0;
197
- // Create variants from all code elements
198
- variants = createVariantsFromCodeElements(codeElements); // Process each variant with loadVariant
199
- processedCode = {};
200
- variantPromises = Object.entries(variants).map(/*#__PURE__*/function () {
201
- var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref3) {
202
- var _ref5, variantName, variantData, result;
203
- return _regenerator().w(function (_context) {
204
- while (1) switch (_context.n) {
205
- case 0:
206
- _ref5 = _slicedToArray(_ref3, 2), variantName = _ref5[0], variantData = _ref5[1];
207
- if (!(variantData && _typeof(variantData) === 'object')) {
208
- _context.n = 2;
209
- break;
210
- }
211
- _context.n = 1;
212
- return loadVariant(undefined,
213
- // url - not needed for inline code
214
- variantName, variantData, {
215
- sourceParser: sourceParser,
216
- loadSource: undefined,
217
- // loadSource - not needed since we have the data
218
- loadVariantMeta: undefined,
219
- // loadVariantMeta - not needed since we have the data
220
- sourceTransformers: sourceTransformers,
221
- disableTransforms: variantData.skipTransforms || false
222
- });
223
- case 1:
224
- result = _context.v;
225
- return _context.a(2, {
226
- variantName: variantName,
227
- processedVariant: result.code
228
- });
229
- case 2:
230
- return _context.a(2, null);
231
- }
232
- }, _callee);
233
- }));
234
- return function (_x2) {
235
- return _ref4.apply(this, arguments);
236
- };
237
- }());
238
- _context2.n = 1;
239
- return Promise.all(variantPromises);
240
- case 1:
241
- variantResults = _context2.v;
242
- _iterator = _createForOfIteratorHelper(variantResults);
243
- try {
244
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
245
- result = _step.value;
246
- if (result) {
247
- processedCode[result.variantName] = result.processedVariant;
248
- }
249
- }
250
-
251
- // Clear all code element contents and replace with error message
252
- } catch (err) {
253
- _iterator.e(err);
254
- } finally {
255
- _iterator.f();
256
- }
257
- codeElements.forEach(function (codeElement) {
258
- codeElement.children = [];
259
- });
260
-
261
- // Replace pre element children with error message for CodeHighlighter
262
- node.children = [{
263
- type: 'text',
264
- value: 'Error: expected pre tag to be handled by CodeHighlighter'
265
- }];
266
-
267
- // Set the precompute data on the pre element directly on properties for immediate HTML serialization
268
- if (!node.properties) {
269
- node.properties = {};
270
- }
271
- node.properties.dataPrecompute = JSON.stringify(processedCode);
272
- _context2.n = 3;
273
- break;
274
- case 2:
275
- _context2.p = 2;
276
- _t = _context2.v;
277
- console.warn('Failed to transform code block:', _t);
278
- case 3:
279
- return _context2.a(2);
280
- }
281
- }, _callee2, null, [[0, 2]]);
282
- }))();
283
- transformPromises.push(transformPromise);
284
- }
285
- }
286
- });
287
-
288
- // Wait for all transformations to complete
289
- _context3.n = 1;
290
- return Promise.all(transformPromises);
291
- case 1:
292
- return _context3.a(2);
293
- }
294
- }, _callee3);
295
- }));
296
- return function (_x) {
297
- return _ref.apply(this, arguments);
298
- };
299
- }();
300
- };