@mintlify/common 1.0.650 → 1.0.652

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.
@@ -1,3 +1,6 @@
1
+ export type FileCategoryOptions = {
2
+ importedFiles?: Set<string>;
3
+ };
1
4
  export type PotentialFileCategory = 'page' | 'snippet' | 'mintConfig' | 'potentialYamlOpenApiSpec' | 'potentialJsonOpenApiSpec' | 'staticFile' | 'snippet-v2' | 'css' | 'js' | 'docsConfig' | 'generatedStaticFile' | 'mintIgnore' | null;
2
5
  export type FileCategory = Omit<PotentialFileCategory, 'potentialYamlOpenApiSpec' | 'potentialJsonOpenApiSpec'> | 'openApi' | null;
3
6
  export declare const generatedStaticFiles: readonly ["llms.txt", "robots.txt", "sitemap.xml", "llms-full.txt"];
@@ -17,4 +20,4 @@ export declare function isGatedFormat(params: {
17
20
  filePath?: undefined;
18
21
  ext: string;
19
22
  }): boolean;
20
- export declare const getFileCategory: (filePath: string) => PotentialFileCategory;
23
+ export declare const getFileCategory: (filePath: string, options?: FileCategoryOptions) => PotentialFileCategory;
@@ -1,5 +1,7 @@
1
1
  import { parse } from 'path';
2
2
  import { SNIPPET_EXTENSIONS } from './mdx/snippets/constants.js';
3
+ import { isImportedAsSnippet } from './mdx/snippets/isImportedAsSnippet.js';
4
+ import { optionallyRemoveLeadingSlash } from './optionallyRemoveLeadingSlash.js';
3
5
  const excludedMdFiles = ['readme', 'license', 'contributing', 'contribute'];
4
6
  export const generatedStaticFiles = [
5
7
  'llms.txt',
@@ -73,9 +75,9 @@ export function isGatedFormat({ filePath, ext }) {
73
75
  ext = ext.startsWith('.') ? ext : `.${ext}`;
74
76
  return gatedStaticAssetExtensions.includes(ext.toLowerCase());
75
77
  }
76
- export const getFileCategory = (filePath) => {
77
- filePath = filePath.toLowerCase();
78
- const parsed = parse(filePath);
78
+ export const getFileCategory = (filePath, options) => {
79
+ const normalizedFilePath = optionallyRemoveLeadingSlash(filePath.toLowerCase());
80
+ const parsed = parse(normalizedFilePath);
79
81
  if (parsed.base === 'mint.json') {
80
82
  return 'mintConfig';
81
83
  }
@@ -90,16 +92,19 @@ export const getFileCategory = (filePath) => {
90
92
  if (generatedStaticFiles.includes(parsed.base)) {
91
93
  return 'generatedStaticFile';
92
94
  }
93
- if ((filePath.startsWith('_snippets/') || filePath.startsWith('snippets/')) &&
94
- (SNIPPET_EXTENSIONS.some((ext) => extension === ext) || extension === '.md')) {
95
- if (filePath.startsWith('_snippets/')) {
95
+ if ((normalizedFilePath.startsWith('_snippets/') || normalizedFilePath.startsWith('snippets/')) &&
96
+ SNIPPET_EXTENSIONS.some((ext) => extension === ext)) {
97
+ if (normalizedFilePath.startsWith('_snippets/')) {
96
98
  return 'snippet';
97
99
  }
98
- else if (filePath.startsWith('snippets/')) {
100
+ else if (normalizedFilePath.startsWith('snippets/')) {
99
101
  return 'snippet-v2';
100
102
  }
101
103
  }
102
104
  else if (extension === '.mdx') {
105
+ if ((options === null || options === void 0 ? void 0 : options.importedFiles) != null && isImportedAsSnippet(filePath, options.importedFiles)) {
106
+ return 'snippet-v2';
107
+ }
103
108
  return 'page';
104
109
  }
105
110
  else if (extension === '.md') {
@@ -107,8 +112,14 @@ export const getFileCategory = (filePath) => {
107
112
  if (excludedMdFiles.includes(fileName)) {
108
113
  return null;
109
114
  }
115
+ if ((options === null || options === void 0 ? void 0 : options.importedFiles) != null && isImportedAsSnippet(filePath, options.importedFiles)) {
116
+ return 'snippet-v2';
117
+ }
110
118
  return 'page';
111
119
  }
120
+ else if (extension === '.jsx') {
121
+ return 'snippet-v2';
122
+ }
112
123
  else if (extension === '.yaml' || extension === '.yml') {
113
124
  return 'potentialYamlOpenApiSpec';
114
125
  }
@@ -0,0 +1,9 @@
1
+ import type { Root } from 'mdast';
2
+ export interface FileWithTree {
3
+ path: string;
4
+ tree: Root;
5
+ }
6
+ export declare const buildImportMap: (files: FileWithTree[]) => {
7
+ importedFiles: Set<string>;
8
+ fileImportsMap: Map<string, Set<string>>;
9
+ };
@@ -0,0 +1,19 @@
1
+ import { extractImportSources } from './extractImportSources.js';
2
+ import { resolveImportPath } from './resolveImportPath.js';
3
+ export const buildImportMap = (files) => {
4
+ const importedFiles = new Set();
5
+ const fileImportsMap = new Map();
6
+ for (const file of files) {
7
+ const resolvedImports = new Set();
8
+ for (const source of extractImportSources(file.tree)) {
9
+ const resolved = resolveImportPath(source, file.path);
10
+ if (resolved) {
11
+ const normalized = resolved.toLowerCase();
12
+ importedFiles.add(normalized);
13
+ resolvedImports.add(normalized);
14
+ }
15
+ }
16
+ fileImportsMap.set(file.path.toLowerCase(), resolvedImports);
17
+ }
18
+ return { importedFiles, fileImportsMap };
19
+ };
@@ -1 +1 @@
1
- export const SNIPPET_EXTENSIONS = ['.mdx', '.jsx'];
1
+ export const SNIPPET_EXTENSIONS = ['.mdx', '.jsx', '.md'];
@@ -0,0 +1,2 @@
1
+ import type { Root } from 'mdast';
2
+ export declare const extractImportSources: (tree: Root) => string[];
@@ -0,0 +1,15 @@
1
+ import { visit } from 'unist-util-visit';
2
+ import { estreeIsProgram, isImportDeclaration, isMdxJsEsm } from '../utils.js';
3
+ export const extractImportSources = (tree) => {
4
+ const sources = [];
5
+ visit(tree, (node) => {
6
+ if (isMdxJsEsm(node) && estreeIsProgram(node)) {
7
+ for (const bodyChild of node.data.estree.body) {
8
+ if (isImportDeclaration(bodyChild) && typeof bodyChild.source.value === 'string') {
9
+ sources.push(bodyChild.source.value);
10
+ }
11
+ }
12
+ }
13
+ });
14
+ return sources;
15
+ };
@@ -1,10 +1,14 @@
1
+ export * from './buildImportMap.js';
2
+ export * from './constants.js';
3
+ export * from './extractImportSources.js';
4
+ export * from './isSnippetExtension.js';
5
+ export * from './findAndRemoveExports.js';
1
6
  export * from './findAndRemoveImports.js';
7
+ export * from './getExportMap.js';
2
8
  export * from './hasImports.js';
9
+ export * from './isImportedAsSnippet.js';
3
10
  export * from './nodeIncludesExport.js';
4
- export * from './resolveImport/index.js';
5
- export * from './resolveAllImports.js';
6
- export * from './resolveSnippetImportPath.js';
7
11
  export * from './removeExports.js';
8
- export * from './getExportMap.js';
9
- export * from './findAndRemoveExports.js';
10
- export * from './constants.js';
12
+ export * from './resolveAllImports.js';
13
+ export * from './resolveImport/index.js';
14
+ export * from './resolveImportPath.js';
@@ -1,10 +1,14 @@
1
+ export * from './buildImportMap.js';
2
+ export * from './constants.js';
3
+ export * from './extractImportSources.js';
4
+ export * from './isSnippetExtension.js';
5
+ export * from './findAndRemoveExports.js';
1
6
  export * from './findAndRemoveImports.js';
7
+ export * from './getExportMap.js';
2
8
  export * from './hasImports.js';
9
+ export * from './isImportedAsSnippet.js';
3
10
  export * from './nodeIncludesExport.js';
4
- export * from './resolveImport/index.js';
5
- export * from './resolveAllImports.js';
6
- export * from './resolveSnippetImportPath.js';
7
11
  export * from './removeExports.js';
8
- export * from './getExportMap.js';
9
- export * from './findAndRemoveExports.js';
10
- export * from './constants.js';
12
+ export * from './resolveAllImports.js';
13
+ export * from './resolveImport/index.js';
14
+ export * from './resolveImportPath.js';
@@ -0,0 +1 @@
1
+ export declare const isImportedAsSnippet: (filename: string, importedFiles: Set<string>) => boolean;
@@ -0,0 +1,7 @@
1
+ import { optionallyAddLeadingSlash } from '../../fs/optionallySlash.js';
2
+ export const isImportedAsSnippet = (filename, importedFiles) => {
3
+ const normalizedFilename = optionallyAddLeadingSlash(filename).toLowerCase();
4
+ return (importedFiles.has(normalizedFilename) &&
5
+ !normalizedFilename.startsWith('/snippets/') &&
6
+ !normalizedFilename.startsWith('/_snippets/'));
7
+ };
@@ -0,0 +1 @@
1
+ export declare const isSnippetExtension: (extension: string) => boolean;
@@ -0,0 +1,5 @@
1
+ import { SNIPPET_EXTENSIONS } from './constants.js';
2
+ export const isSnippetExtension = (extension) => {
3
+ const normalizedExt = extension.startsWith('.') ? extension : `.${extension}`;
4
+ return SNIPPET_EXTENSIONS.includes(normalizedExt);
5
+ };
@@ -9,15 +9,15 @@ 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
+ import { resolveImportPath } from './resolveImportPath.js';
13
13
  export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, function* () {
14
14
  const { snippets, fileWithImports } = params;
15
15
  let ast = fileWithImports.tree;
16
16
  const exportMap = getExportMapFromTree(ast);
17
17
  for (const source of Object.keys(fileWithImports.importMap)) {
18
- const resolvedPath = resolveSnippetImportPath(source, fileWithImports.filename);
18
+ const resolvedPath = resolveImportPath(source, fileWithImports.filename);
19
19
  if (resolvedPath == null) {
20
- console.log(`Invalid import path ${source} in ${fileWithImports.filename}. Import must resolve to a file in /snippets/.`);
20
+ console.log(`Invalid import path ${source} in ${fileWithImports.filename}. Only local imports are supported.`);
21
21
  continue;
22
22
  }
23
23
  const importedSnippet = snippets.find((snippet) => snippet.filename === resolvedPath);
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Resolves an import path to an absolute path with leading slash.
3
+ *
4
+ * @param source The import source path (e.g., '/components/foo.mdx' or '../bar.mdx')
5
+ * @param importingFilePath The path of the file containing the import
6
+ * @returns The resolved absolute path with leading slash, or null if it's an external/package import
7
+ */
8
+ export declare const resolveImportPath: (source: string, importingFilePath: string) => string | null;
@@ -2,31 +2,27 @@ import { posix } from 'path';
2
2
  const isRelativeImport = (source) => {
3
3
  return source.startsWith('./') || source.startsWith('../');
4
4
  };
5
- const isInSnippetsFolder = (resolvedPath) => {
6
- return resolvedPath.startsWith('/snippets/');
5
+ const isLocalImport = (source) => {
6
+ return source.startsWith('/') || isRelativeImport(source);
7
7
  };
8
8
  /**
9
9
  * Resolves an import path to an absolute path with leading slash.
10
- * Only allows imports that resolve to files in /snippets/.
11
10
  *
12
- * @param source The import source path (e.g., '/snippets/foo.mdx' or '../snippets/bar.mdx')
11
+ * @param source The import source path (e.g., '/components/foo.mdx' or '../bar.mdx')
13
12
  * @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
13
+ * @returns The resolved absolute path with leading slash, or null if it's an external/package import
15
14
  */
16
- export const resolveSnippetImportPath = (source, importingFilePath) => {
15
+ export const resolveImportPath = (source, importingFilePath) => {
16
+ if (!isLocalImport(source)) {
17
+ return null;
18
+ }
17
19
  let resolvedPath;
18
20
  if (source.startsWith('/')) {
19
21
  resolvedPath = posix.normalize(source);
20
22
  }
21
- else if (isRelativeImport(source)) {
23
+ else {
22
24
  const importingDir = posix.dirname(importingFilePath.startsWith('/') ? importingFilePath : '/' + importingFilePath);
23
25
  resolvedPath = posix.resolve(importingDir, source);
24
26
  }
25
- else {
26
- return null;
27
- }
28
- if (!isInSnippetsFolder(resolvedPath)) {
29
- return null;
30
- }
31
27
  return resolvedPath;
32
28
  };