@mintlify/previewing 4.0.822 → 4.0.823

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,2 +1,2 @@
1
1
  import type { FindAndRemoveImportsResponse } from '@mintlify/common';
2
- export declare const generateDependentSnippets: (changedFilename: string, newImportData: FindAndRemoveImportsResponse) => Promise<string[]>;
2
+ export declare const generateDependentSnippets: (changedFilename: string, newImportData: FindAndRemoveImportsResponse, snippetPatterns?: string[]) => Promise<string[]>;
@@ -4,7 +4,7 @@ import { join } from 'path';
4
4
  import { NEXT_PUBLIC_PATH } from '../../constants.js';
5
5
  import { getOriginalSnippets } from './getSnippets.js';
6
6
  import { resolveAllImports } from './resolveAllImports.js';
7
- const findAllDependents = async (initialFileWithSlash, allSnippets, processedDataCache) => {
7
+ const findAllDependents = async (initialFileWithSlash, allSnippets, processedDataCache, snippetPatterns) => {
8
8
  const affected = new Set([initialFileWithSlash]);
9
9
  const queue = [initialFileWithSlash];
10
10
  while (queue.length > 0) {
@@ -20,7 +20,7 @@ const findAllDependents = async (initialFileWithSlash, allSnippets, processedDat
20
20
  processedDataCache.set(potentialDependentFile, processedData);
21
21
  }
22
22
  const importsCurrentFile = Object.keys(processedData.importMap).some((importPath) => {
23
- const resolvedPath = resolveSnippetImportPath(importPath, potentialDependentFile);
23
+ const resolvedPath = resolveSnippetImportPath(importPath, potentialDependentFile, snippetPatterns);
24
24
  return resolvedPath === currentSourceFile;
25
25
  });
26
26
  if (importsCurrentFile) {
@@ -33,11 +33,11 @@ const findAllDependents = async (initialFileWithSlash, allSnippets, processedDat
33
33
  }
34
34
  return affected;
35
35
  };
36
- export const generateDependentSnippets = async (changedFilename, newImportData) => {
36
+ export const generateDependentSnippets = async (changedFilename, newImportData, snippetPatterns) => {
37
37
  const processedDataCache = new Map();
38
- const allOriginalSnippets = await getOriginalSnippets();
38
+ const allOriginalSnippets = await getOriginalSnippets(snippetPatterns);
39
39
  const updatedSnippetFileKey = optionallyAddLeadingSlash(changedFilename);
40
- const affectedSnippets = await findAllDependents(updatedSnippetFileKey, allOriginalSnippets, processedDataCache);
40
+ const affectedSnippets = await findAllDependents(updatedSnippetFileKey, allOriginalSnippets, processedDataCache, snippetPatterns);
41
41
  const snippetPromises = Array.from(affectedSnippets).map(async (filename) => {
42
42
  const cachedData = processedDataCache.get(filename);
43
43
  if (cachedData)
@@ -66,7 +66,7 @@ export const generateDependentSnippets = async (changedFilename, newImportData)
66
66
  const graph = {};
67
67
  snippets.forEach((item) => {
68
68
  graph[item.filename] = Object.keys(item.importMap)
69
- .map((dep) => resolveSnippetImportPath(dep, item.filename))
69
+ .map((dep) => resolveSnippetImportPath(dep, item.filename, snippetPatterns))
70
70
  .filter((resolvedDep) => resolvedDep != null);
71
71
  });
72
72
  const sortedSnippets = topologicalSort(graph).reverse();
@@ -78,7 +78,7 @@ export const generateDependentSnippets = async (changedFilename, newImportData)
78
78
  for (const currentSnippet of orderedSnippets) {
79
79
  let processedTree = currentSnippet.tree;
80
80
  if (currentSnippet.filename !== updatedSnippetFileKey && hasImports(currentSnippet)) {
81
- processedTree = await resolveAllImports(currentSnippet);
81
+ processedTree = await resolveAllImports(currentSnippet, snippetPatterns);
82
82
  }
83
83
  const targetFilename = optionallyRemoveLeadingSlash(currentSnippet.filename);
84
84
  const targetPath = join(NEXT_PUBLIC_PATH, targetFilename);
@@ -1 +1 @@
1
- export declare const generatePagesWithImports: (updatedSnippets: Set<string>) => Promise<void>;
1
+ export declare const generatePagesWithImports: (updatedSnippets: Set<string>, snippetPatterns?: string[]) => Promise<void>;
@@ -1,4 +1,4 @@
1
- import { findAndRemoveImports, optionallyRemoveLeadingSlash, resolveAllImports, resolveSnippetImportPath, stringifyTree, SNIPPET_EXTENSIONS, } from '@mintlify/common';
1
+ import { findAndRemoveImports, optionallyRemoveLeadingSlash, resolveAllImports, resolveSnippetImportPath, stringifyTree, SNIPPET_EXTENSIONS, isSnippetPath, } from '@mintlify/common';
2
2
  import { preparseMdxTree, getFileListSync } from '@mintlify/prebuild';
3
3
  import { promises as _promises } from 'fs';
4
4
  import { outputFile } from 'fs-extra';
@@ -6,11 +6,11 @@ import { join } from 'path';
6
6
  import { CMD_EXEC_PATH, NEXT_PROPS_PATH } from '../../constants.js';
7
7
  import { getProcessedSnippets } from './getSnippets.js';
8
8
  const { readFile } = _promises;
9
- export const generatePagesWithImports = async (updatedSnippets) => {
10
- const snippets = await getProcessedSnippets();
9
+ export const generatePagesWithImports = async (updatedSnippets, snippetPatterns) => {
10
+ const snippets = await getProcessedSnippets(snippetPatterns);
11
11
  const pageFilenames = getFileListSync(CMD_EXEC_PATH).filter((file) => SNIPPET_EXTENSIONS.some((ext) => file.endsWith(ext)) &&
12
12
  !file.startsWith('_snippets/') &&
13
- !file.startsWith('snippets/'));
13
+ !isSnippetPath(file, snippetPatterns));
14
14
  await Promise.all(pageFilenames.map(async (pageFilename) => {
15
15
  const sourcePath = join(CMD_EXEC_PATH, pageFilename);
16
16
  const contentStr = (await readFile(sourcePath)).toString();
@@ -18,13 +18,14 @@ export const generatePagesWithImports = async (updatedSnippets) => {
18
18
  const tree = await preparseMdxTree(contentStr, CMD_EXEC_PATH, sourcePath);
19
19
  const importsResponse = await findAndRemoveImports(tree);
20
20
  if (Object.keys(importsResponse.importMap).some((importPath) => {
21
- const resolvedPath = resolveSnippetImportPath(importPath, pageFilename);
21
+ const resolvedPath = resolveSnippetImportPath(importPath, pageFilename, snippetPatterns);
22
22
  return (resolvedPath != null &&
23
23
  updatedSnippets.has(optionallyRemoveLeadingSlash(resolvedPath)));
24
24
  })) {
25
25
  const content = await resolveAllImports({
26
26
  snippets,
27
27
  fileWithImports: { ...importsResponse, filename: pageFilename },
28
+ snippetPatterns,
28
29
  });
29
30
  const targetPath = join(NEXT_PROPS_PATH, pageFilename);
30
31
  await outputFile(targetPath, stringifyTree(content), {
@@ -1,3 +1,3 @@
1
1
  import { type FileType } from '@mintlify/common';
2
- export declare const getProcessedSnippets: () => Promise<FileType[]>;
3
- export declare const getOriginalSnippets: () => Promise<FileType[]>;
2
+ export declare const getProcessedSnippets: (snippetPatterns?: string[]) => Promise<FileType[]>;
3
+ export declare const getOriginalSnippets: (snippetPatterns?: string[]) => Promise<FileType[]>;
@@ -1,11 +1,11 @@
1
- import { optionallyAddLeadingSlash, SNIPPET_EXTENSIONS } from '@mintlify/common';
1
+ import { optionallyAddLeadingSlash, SNIPPET_EXTENSIONS, isSnippetPath, } from '@mintlify/common';
2
2
  import { getFileListSync, preparseMdxTree } from '@mintlify/prebuild';
3
3
  import { promises as _promises } from 'fs';
4
4
  import { join } from 'path';
5
5
  import { CMD_EXEC_PATH, NEXT_PUBLIC_PATH } from '../../constants.js';
6
6
  const { readFile } = _promises;
7
- const getSnippetBase = async (BASE_DIR) => {
8
- const snippetFilenames = getFileListSync(BASE_DIR).filter((file) => SNIPPET_EXTENSIONS.some((ext) => file.endsWith(ext)) && file.startsWith('snippets/'));
7
+ const getSnippetBase = async (BASE_DIR, snippetPatterns) => {
8
+ const snippetFilenames = getFileListSync(BASE_DIR).filter((file) => SNIPPET_EXTENSIONS.some((ext) => file.endsWith(ext)) && isSnippetPath(file, snippetPatterns));
9
9
  const promises = snippetFilenames.map(async (snippetFilename) => {
10
10
  try {
11
11
  const tree = await preparseMdxTree((await readFile(join(BASE_DIR, snippetFilename))).toString(), BASE_DIR, join(BASE_DIR, snippetFilename));
@@ -21,5 +21,5 @@ const getSnippetBase = async (BASE_DIR) => {
21
21
  });
22
22
  return (await Promise.all(promises)).filter(Boolean);
23
23
  };
24
- export const getProcessedSnippets = () => getSnippetBase(NEXT_PUBLIC_PATH);
25
- export const getOriginalSnippets = () => getSnippetBase(CMD_EXEC_PATH);
24
+ export const getProcessedSnippets = (snippetPatterns) => getSnippetBase(NEXT_PUBLIC_PATH, snippetPatterns);
25
+ export const getOriginalSnippets = (snippetPatterns) => getSnippetBase(CMD_EXEC_PATH, snippetPatterns);
@@ -19,6 +19,7 @@ import { updateCustomLanguages, updateGeneratedNav, updateOpenApiFiles, upsertOp
19
19
  import { isFileSizeValid, shouldRegenerateNavForPage } from './utils.js';
20
20
  const { readFile } = _promises;
21
21
  const frontmatterHashes = new Map();
22
+ let previousSnippetPatternsJson = null;
22
23
  const getMintIgnoreGlobs = () => {
23
24
  const mintIgnorePath = pathUtil.join(CMD_EXEC_PATH, '.mintignore');
24
25
  if (fse.existsSync(mintIgnorePath)) {
@@ -27,8 +28,23 @@ const getMintIgnoreGlobs = () => {
27
28
  }
28
29
  return [];
29
30
  };
31
+ const getSnippetPatterns = () => {
32
+ const docsConfigPath = pathUtil.join(CMD_EXEC_PATH, 'docs.json');
33
+ if (fse.existsSync(docsConfigPath)) {
34
+ try {
35
+ const configContent = fse.readFileSync(docsConfigPath, 'utf-8');
36
+ const config = JSON.parse(configContent);
37
+ return config.snippets;
38
+ }
39
+ catch {
40
+ return undefined;
41
+ }
42
+ }
43
+ return undefined;
44
+ };
30
45
  const listener = (callback, options = {}) => {
31
46
  const mintIgnoreGlobs = getMintIgnoreGlobs();
47
+ previousSnippetPatternsJson = JSON.stringify(getSnippetPatterns() ?? []);
32
48
  chokidar
33
49
  .watch(CMD_EXEC_PATH, {
34
50
  ignoreInitial: true,
@@ -76,7 +92,8 @@ const onUnlinkEvent = async (filename, options) => {
76
92
  return;
77
93
  }
78
94
  try {
79
- const potentialCategory = getFileCategory(filename);
95
+ const snippetPatterns = getSnippetPatterns();
96
+ const potentialCategory = getFileCategory(filename, snippetPatterns);
80
97
  const targetPath = getTargetPath(potentialCategory, filename);
81
98
  if (potentialCategory === 'page' ||
82
99
  potentialCategory === 'snippet' ||
@@ -163,7 +180,8 @@ const validateConfigFiles = async () => {
163
180
  */
164
181
  const onUpdateEvent = async (filename, callback, options = {}) => {
165
182
  const filePath = pathUtil.join(CMD_EXEC_PATH, filename);
166
- const potentialCategory = getFileCategory(filename);
183
+ const snippetPatterns = getSnippetPatterns();
184
+ const potentialCategory = getFileCategory(filename, snippetPatterns);
167
185
  const targetPath = getTargetPath(potentialCategory, filename);
168
186
  let regenerateNav = false;
169
187
  let category = potentialCategory === 'potentialYamlOpenApiSpec' ||
@@ -177,7 +195,7 @@ const onUpdateEvent = async (filename, callback, options = {}) => {
177
195
  const tree = await preparseMdxTree(contentStr, CMD_EXEC_PATH, filePath);
178
196
  const importsResponse = await findAndRemoveImports(tree);
179
197
  if (hasImports(importsResponse)) {
180
- contentStr = stringifyTree(await resolveAllImports({ ...importsResponse, filename }));
198
+ contentStr = stringifyTree(await resolveAllImports({ ...importsResponse, filename }, snippetPatterns));
181
199
  }
182
200
  // set suppressErrLog true here to avoid double logging errors already logged in preparseMdxTree
183
201
  const { pageContent } = await createPage(filename, contentStr, CMD_EXEC_PATH, [], [], true);
@@ -195,17 +213,29 @@ const onUpdateEvent = async (filename, callback, options = {}) => {
195
213
  const tree = await preparseMdxTree(contentStr, CMD_EXEC_PATH, filePath);
196
214
  const importsResponse = await findAndRemoveImports(tree);
197
215
  if (hasImports(importsResponse)) {
198
- contentStr = stringifyTree(await resolveAllImports({ ...importsResponse, filename }));
216
+ contentStr = stringifyTree(await resolveAllImports({ ...importsResponse, filename }, snippetPatterns));
199
217
  }
200
218
  await fse.outputFile(targetPath, contentStr, {
201
219
  flag: 'w',
202
220
  });
203
- const updatedSnippets = await generateDependentSnippets(filename, importsResponse);
204
- await generatePagesWithImports(new Set(updatedSnippets));
221
+ const updatedSnippets = await generateDependentSnippets(filename, importsResponse, snippetPatterns);
222
+ await generatePagesWithImports(new Set(updatedSnippets), snippetPatterns);
205
223
  break;
206
224
  }
207
225
  case 'mintConfig':
208
226
  case 'docsConfig': {
227
+ if (potentialCategory === 'docsConfig') {
228
+ const currentJson = JSON.stringify(snippetPatterns ?? []);
229
+ if (previousSnippetPatternsJson !== null && currentJson !== previousSnippetPatternsJson) {
230
+ previousSnippetPatternsJson = currentJson;
231
+ addChangeLog(_jsx(InfoLog, { message: "Snippet patterns changed. Rebuilding..." }));
232
+ await fse.emptyDir(NEXT_PUBLIC_PATH);
233
+ await fse.emptyDir(NEXT_PROPS_PATH);
234
+ await prebuild(CMD_EXEC_PATH, options);
235
+ break;
236
+ }
237
+ previousSnippetPatternsJson = currentJson;
238
+ }
209
239
  regenerateNav = true;
210
240
  try {
211
241
  const { mintConfig, openApiFiles, docsConfig } = await getDocsState();
@@ -1,3 +1,3 @@
1
1
  import type { FileWithImports } from '@mintlify/common';
2
2
  import type { Root } from 'mdast';
3
- export declare const resolveAllImports: (fileWithImports: FileWithImports) => Promise<Root>;
3
+ export declare const resolveAllImports: (fileWithImports: FileWithImports, snippetPatterns?: string[]) => Promise<Root>;
@@ -1,9 +1,10 @@
1
1
  import { resolveAllImports as baseResolveAllImports } from '@mintlify/common';
2
2
  import { getProcessedSnippets } from './getSnippets.js';
3
- export const resolveAllImports = async (fileWithImports) => {
4
- const snippets = await getProcessedSnippets();
3
+ export const resolveAllImports = async (fileWithImports, snippetPatterns) => {
4
+ const snippets = await getProcessedSnippets(snippetPatterns);
5
5
  return await baseResolveAllImports({
6
6
  snippets,
7
7
  fileWithImports,
8
+ snippetPatterns,
8
9
  });
9
10
  };