@mintlify/common 1.0.630 → 1.0.632

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.
@@ -3,6 +3,7 @@ export * from './hasImports.js';
3
3
  export * from './nodeIncludesExport.js';
4
4
  export * from './resolveImport/index.js';
5
5
  export * from './resolveAllImports.js';
6
+ export * from './resolveSnippetImportPath.js';
6
7
  export * from './removeExports.js';
7
8
  export * from './getExportMap.js';
8
9
  export * from './findAndRemoveExports.js';
@@ -3,6 +3,7 @@ export * from './hasImports.js';
3
3
  export * from './nodeIncludesExport.js';
4
4
  export * from './resolveImport/index.js';
5
5
  export * from './resolveAllImports.js';
6
+ export * from './resolveSnippetImportPath.js';
6
7
  export * from './removeExports.js';
7
8
  export * from './getExportMap.js';
8
9
  export * from './findAndRemoveExports.js';
@@ -9,18 +9,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { getExportMapFromTree } from './getExportMap.js';
11
11
  import { resolveImport } from './resolveImport/index.js';
12
+ import { resolveSnippetImportPath } from './resolveSnippetImportPath.js';
12
13
  export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, function* () {
13
14
  const { snippets, fileWithImports } = params;
14
15
  let ast = fileWithImports.tree;
15
16
  const exportMap = getExportMapFromTree(ast);
16
17
  for (const source of Object.keys(fileWithImports.importMap)) {
17
- if (!source.startsWith('/snippets/')) {
18
- console.log(`Invalid import path ${source} in ${fileWithImports.filename}. Import source must start with "/snippets/".`);
18
+ const resolvedPath = resolveSnippetImportPath(source, fileWithImports.filename);
19
+ if (resolvedPath == null) {
20
+ console.log(`Invalid import path ${source} in ${fileWithImports.filename}. Import must resolve to a file in /snippets/.`);
19
21
  continue;
20
22
  }
21
- const importedSnippet = snippets.find((snippet) => snippet.filename === source);
23
+ const importedSnippet = snippets.find((snippet) => snippet.filename === resolvedPath);
22
24
  if (importedSnippet == undefined) {
23
- console.log(`Could not find file ${source} - imported from ${fileWithImports.filename}`);
25
+ console.log(`Could not find file ${resolvedPath} - imported from ${fileWithImports.filename}`);
24
26
  continue;
25
27
  }
26
28
  const specifiers = fileWithImports.importMap[source];
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Resolves an import path to an absolute path with leading slash.
3
+ * Only allows imports that resolve to files in /snippets/.
4
+ *
5
+ * @param source The import source path (e.g., '/snippets/foo.mdx' or '../snippets/bar.mdx')
6
+ * @param importingFilePath The path of the file containing the import
7
+ * @returns The resolved absolute path with leading slash, or null if invalid or not in snippets folder
8
+ */
9
+ export declare const resolveSnippetImportPath: (source: string, importingFilePath: string) => string | null;
@@ -0,0 +1,32 @@
1
+ import { posix } from 'path';
2
+ const isRelativeImport = (source) => {
3
+ return source.startsWith('./') || source.startsWith('../');
4
+ };
5
+ const isInSnippetsFolder = (resolvedPath) => {
6
+ return resolvedPath.startsWith('/snippets/');
7
+ };
8
+ /**
9
+ * Resolves an import path to an absolute path with leading slash.
10
+ * Only allows imports that resolve to files in /snippets/.
11
+ *
12
+ * @param source The import source path (e.g., '/snippets/foo.mdx' or '../snippets/bar.mdx')
13
+ * @param importingFilePath The path of the file containing the import
14
+ * @returns The resolved absolute path with leading slash, or null if invalid or not in snippets folder
15
+ */
16
+ export const resolveSnippetImportPath = (source, importingFilePath) => {
17
+ let resolvedPath;
18
+ if (source.startsWith('/')) {
19
+ resolvedPath = posix.normalize(source);
20
+ }
21
+ else if (isRelativeImport(source)) {
22
+ const importingDir = posix.dirname(importingFilePath.startsWith('/') ? importingFilePath : '/' + importingFilePath);
23
+ resolvedPath = posix.resolve(importingDir, source);
24
+ }
25
+ else {
26
+ return null;
27
+ }
28
+ if (!isInSnippetsFolder(resolvedPath)) {
29
+ return null;
30
+ }
31
+ return resolvedPath;
32
+ };