@mui/internal-markdown 1.0.18 → 1.0.19

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 (2) hide show
  1. package/loader.js +115 -20
  2. package/package.json +2 -2
package/loader.js CHANGED
@@ -148,11 +148,12 @@ module.exports = async function demoLoader() {
148
148
  * @param {*} moduleFilepath
149
149
  * @param {*} variant
150
150
  * @param {*} importModuleID
151
+ * @returns {string} The name of the imported module along with a resolved extension if not provided
151
152
  * @example detectRelativeImports('ComboBox.js', '', JS', './top100Films') => relativeModules.set('ComboBox.js', new Map([['./top100Films.js', ['JS']]]))
152
153
  */
153
154
  function detectRelativeImports(demoName, moduleFilepath, variant, importModuleID) {
155
+ let relativeModuleFilename = importModuleID;
154
156
  if (importModuleID.startsWith('.')) {
155
- let relativeModuleFilename = importModuleID;
156
157
  const demoMap = relativeModules.get(demoName);
157
158
  // If the moduleID does not end with an extension, or ends with an unsupported extension (e.g. ".styling") we need to resolve it
158
159
  // Fastest way to get a file extension, see: https://stackoverflow.com/a/12900504/
@@ -198,6 +199,27 @@ module.exports = async function demoLoader() {
198
199
  }
199
200
  }
200
201
  }
202
+ return relativeModuleFilename;
203
+ }
204
+
205
+ /**
206
+ * Inserts the moduleData into the relativeModules object
207
+ * @param string demoName
208
+ * @param {*} moduleData
209
+ * @param string variant
210
+ * @example updateRelativeModules(demoName, {module: 'constants.js', raw: ... }, 'JS') => demos[demoName].relativeModules[variant].push(moduleData)
211
+ */
212
+ function updateRelativeModules(demoName, moduleData, variant) {
213
+ if (demos[demoName].relativeModules[variant]) {
214
+ // Avoid duplicates
215
+ if (
216
+ !demos[demoName].relativeModules[variant].some((elem) => elem.module === moduleData.module)
217
+ ) {
218
+ demos[demoName].relativeModules[variant].push(moduleData);
219
+ }
220
+ } else {
221
+ demos[demoName].relativeModules[variant] = [moduleData];
222
+ }
201
223
  }
202
224
 
203
225
  await Promise.all(
@@ -438,32 +460,105 @@ module.exports = async function demoLoader() {
438
460
  demos[demoName].relativeModules = {};
439
461
  }
440
462
 
463
+ const addedModulesRelativeToModulePath = new Set();
441
464
  await Promise.all(
442
465
  Array.from(relativeModules.get(demoName)).map(async ([relativeModuleID, variants]) => {
443
- let raw = '';
444
- try {
445
- raw = await fs.readFile(path.join(path.dirname(moduleFilepath), relativeModuleID), {
446
- encoding: 'utf8',
447
- });
448
- } catch {
449
- throw new Error(
450
- `Could not find a module for the relative import "${relativeModuleID}" in the demo "${demoName}"`,
466
+ for (const variant of variants) {
467
+ let raw = '';
468
+ const relativeModuleFilePath = path.join(
469
+ path.dirname(moduleFilepath),
470
+ relativeModuleID,
451
471
  );
452
- }
453
472
 
454
- const moduleData = { module: relativeModuleID, raw };
455
- const modules = demos[demoName].relativeModules;
473
+ // the file has already been processed
474
+ if (addedModulesRelativeToModulePath.has(relativeModuleFilePath)) {
475
+ continue;
476
+ }
456
477
 
457
- variants.forEach((variant) => {
458
- if (modules[variant]) {
459
- // Avoid duplicates
460
- if (!modules[variant].some((elem) => elem.module === relativeModuleID)) {
461
- modules[variant].push(moduleData);
478
+ try {
479
+ // We are only iterating trough an array that looks
480
+ // like this: ['JS', 'TS'], so it is safe to await
481
+ // eslint-disable-next-line no-await-in-loop
482
+ raw = await fs.readFile(relativeModuleFilePath, {
483
+ encoding: 'utf8',
484
+ });
485
+
486
+ const importedProcessedModuleIDs = new Set();
487
+ const importedProcessedModulesIDsParents = new Map();
488
+ // Find the relative paths in the relative module
489
+ extractImports(raw).forEach((importModuleID) => {
490
+ // detect relative import
491
+ const importModuleIdWithExtension = detectRelativeImports(
492
+ relativeModuleID,
493
+ relativeModuleFilePath,
494
+ variant,
495
+ importModuleID,
496
+ );
497
+ if (importModuleID.startsWith('.')) {
498
+ importedProcessedModuleIDs.add(importModuleIdWithExtension);
499
+ importedProcessedModulesIDsParents.set(
500
+ importModuleIdWithExtension,
501
+ relativeModuleFilePath,
502
+ );
503
+ }
504
+ });
505
+
506
+ updateRelativeModules(demoName, { module: relativeModuleID, raw }, variant);
507
+ addedModulesRelativeToModulePath.add(relativeModuleFilePath);
508
+
509
+ // iterate recursively over the relative imports
510
+ while (importedProcessedModuleIDs.size > 0) {
511
+ for (const entry of importedProcessedModuleIDs) {
512
+ if (entry.startsWith('.')) {
513
+ const entryModuleFilePath = path.join(
514
+ path.dirname(importedProcessedModulesIDsParents.get(entry)),
515
+ entry,
516
+ );
517
+
518
+ // We are only iterating trough an array that looks
519
+ // like this: ['JS', 'TS'], so it is safe to await
520
+ // eslint-disable-next-line no-await-in-loop
521
+ const rawEntry = await fs.readFile(entryModuleFilePath, { encoding: 'utf8' });
522
+
523
+ extractImports(rawEntry).forEach((importModuleID) => {
524
+ // detect relative import
525
+ const importModuleIdWithExtension = detectRelativeImports(
526
+ relativeModuleID,
527
+ entryModuleFilePath,
528
+ variant,
529
+ importModuleID,
530
+ );
531
+ if (importModuleID.startsWith('.')) {
532
+ importedProcessedModuleIDs.add(importModuleIdWithExtension);
533
+ importedProcessedModulesIDsParents.set(
534
+ importModuleIdWithExtension,
535
+ entryModuleFilePath,
536
+ );
537
+ }
538
+ });
539
+
540
+ if (!addedModulesRelativeToModulePath.has(entryModuleFilePath)) {
541
+ const modulePathDirectory = moduleFilepath
542
+ .split('/')
543
+ .slice(0, -1)
544
+ .join('/');
545
+ const moduleData = {
546
+ module: `.${entryModuleFilePath.replace(modulePathDirectory, '')}`,
547
+ raw: rawEntry,
548
+ };
549
+ updateRelativeModules(demoName, moduleData, variant);
550
+ addedModulesRelativeToModulePath.add(entryModuleFilePath);
551
+ }
552
+ }
553
+ importedProcessedModuleIDs.delete(entry);
554
+ }
462
555
  }
463
- } else {
464
- modules[variant] = [moduleData];
556
+ } catch {
557
+ throw new Error(
558
+ `Could not find a module for the relative import "${relativeModuleID}" in the demo "${demoName}"`,
559
+ );
465
560
  }
466
- });
561
+ }
467
562
  }),
468
563
  );
469
564
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mui/internal-markdown",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "author": "MUI Team",
5
5
  "description": "MUI markdown parser. This is an internal package not meant for general use.",
6
6
  "main": "./index.js",
@@ -16,7 +16,7 @@
16
16
  "directory": "packages/markdown"
17
17
  },
18
18
  "dependencies": {
19
- "@babel/runtime": "^7.25.7",
19
+ "@babel/runtime": "^7.26.0",
20
20
  "lodash": "^4.17.21",
21
21
  "marked": "^14.1.3",
22
22
  "prismjs": "^1.29.0"