@mintlify/previewing 4.0.513 → 4.0.515

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.
@@ -0,0 +1,2 @@
1
+ import type { FindAndRemoveImportsResponse } from '@mintlify/common';
2
+ export declare const generateDependentSnippets: (changedFilename: string, newImportData: FindAndRemoveImportsResponse) => Promise<string[]>;
@@ -0,0 +1,86 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { findAndRemoveImports, stringifyTree, topologicalSort, hasImports, optionallyAddLeadingSlash, optionallyRemoveLeadingSlash, } from '@mintlify/common';
11
+ import { outputFile } from 'fs-extra';
12
+ import { join } from 'path';
13
+ import { NEXT_PUBLIC_PATH } from '../../constants.js';
14
+ import { getOriginalSnippets } from './getSnippets.js';
15
+ import { resolveAllImports } from './resolveAllImports.js';
16
+ const findAllDependents = (initialFileWithSlash, allSnippets, processedDataCache) => __awaiter(void 0, void 0, void 0, function* () {
17
+ const affected = new Set([initialFileWithSlash]);
18
+ const queue = [initialFileWithSlash];
19
+ while (queue.length > 0) {
20
+ const currentSourceFile = queue.shift();
21
+ for (const snippet of allSnippets) {
22
+ const potentialDependentFile = optionallyAddLeadingSlash(snippet.filename);
23
+ if (potentialDependentFile === currentSourceFile)
24
+ continue;
25
+ let processedData = processedDataCache.get(potentialDependentFile);
26
+ if (processedData == null) {
27
+ const clonedTree = structuredClone(snippet.tree);
28
+ processedData = yield findAndRemoveImports(clonedTree);
29
+ processedDataCache.set(potentialDependentFile, processedData);
30
+ }
31
+ if (processedData.importMap[currentSourceFile]) {
32
+ if (!affected.has(potentialDependentFile)) {
33
+ affected.add(potentialDependentFile);
34
+ queue.push(potentialDependentFile);
35
+ }
36
+ }
37
+ }
38
+ }
39
+ return affected;
40
+ });
41
+ export const generateDependentSnippets = (changedFilename, newImportData) => __awaiter(void 0, void 0, void 0, function* () {
42
+ const processedDataCache = new Map();
43
+ const allOriginalSnippets = yield getOriginalSnippets();
44
+ const updatedSnippetFileKey = optionallyAddLeadingSlash(changedFilename);
45
+ const affectedSnippets = yield findAllDependents(updatedSnippetFileKey, allOriginalSnippets, processedDataCache);
46
+ const snippetPromises = Array.from(affectedSnippets).map((filename) => __awaiter(void 0, void 0, void 0, function* () {
47
+ const cachedData = processedDataCache.get(filename);
48
+ if (cachedData)
49
+ return Object.assign({ filename }, cachedData);
50
+ const originalSnippet = allOriginalSnippets.find((s) => optionallyAddLeadingSlash(s.filename) === filename);
51
+ if (!originalSnippet)
52
+ return null;
53
+ const processed = yield findAndRemoveImports(structuredClone(originalSnippet.tree));
54
+ processedDataCache.set(filename, processed);
55
+ return Object.assign({ filename }, processed);
56
+ }));
57
+ const snippets = (yield Promise.all(snippetPromises)).filter((item) => item != null);
58
+ const idx = snippets.findIndex((item) => item.filename === updatedSnippetFileKey);
59
+ if (idx !== -1) {
60
+ snippets[idx] = Object.assign(Object.assign({}, newImportData), { filename: updatedSnippetFileKey });
61
+ }
62
+ else {
63
+ snippets.push(Object.assign(Object.assign({}, newImportData), { filename: updatedSnippetFileKey }));
64
+ }
65
+ const graph = {};
66
+ snippets.forEach((item) => {
67
+ graph[item.filename] = Object.keys(item.importMap).map((dep) => optionallyAddLeadingSlash(dep));
68
+ });
69
+ const sortedSnippets = topologicalSort(graph).reverse();
70
+ const snippetsMap = new Map(snippets.map((item) => [item.filename, item]));
71
+ const orderedSnippets = sortedSnippets
72
+ .map((filename) => snippetsMap.get(filename))
73
+ .filter((item) => item != null);
74
+ const processedSnippets = [];
75
+ for (const currentSnippet of orderedSnippets) {
76
+ let processedTree = currentSnippet.tree;
77
+ if (currentSnippet.filename !== updatedSnippetFileKey && hasImports(currentSnippet)) {
78
+ processedTree = yield resolveAllImports(currentSnippet);
79
+ }
80
+ const targetFilename = optionallyRemoveLeadingSlash(currentSnippet.filename);
81
+ const targetPath = join(NEXT_PUBLIC_PATH, targetFilename);
82
+ yield outputFile(targetPath, stringifyTree(processedTree), { flag: 'w' });
83
+ processedSnippets.push(targetFilename);
84
+ }
85
+ return processedSnippets;
86
+ });
@@ -1 +1 @@
1
- export declare const generatePagesWithImports: () => Promise<void>;
1
+ export declare const generatePagesWithImports: (updatedSnippets: Set<string>) => Promise<void>;
@@ -7,16 +7,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { hasImports, findAndRemoveImports, resolveAllImports, stringifyTree, } from '@mintlify/common';
10
+ import { findAndRemoveImports, optionallyRemoveLeadingSlash, resolveAllImports, stringifyTree, } from '@mintlify/common';
11
11
  import { preparseMdxTree, getFileListSync } from '@mintlify/prebuild';
12
12
  import { promises as _promises } from 'fs';
13
13
  import { outputFile } from 'fs-extra';
14
14
  import { join } from 'path';
15
15
  import { CMD_EXEC_PATH, NEXT_PROPS_PATH } from '../../constants.js';
16
- import { getSnippets } from './getSnippets.js';
16
+ import { getProcessedSnippets } from './getSnippets.js';
17
17
  const { readFile } = _promises;
18
- export const generatePagesWithImports = () => __awaiter(void 0, void 0, void 0, function* () {
19
- const snippets = yield getSnippets();
18
+ export const generatePagesWithImports = (updatedSnippets) => __awaiter(void 0, void 0, void 0, function* () {
19
+ const snippets = yield getProcessedSnippets();
20
20
  const pageFilenames = getFileListSync(CMD_EXEC_PATH).filter((file) => file.endsWith('.mdx') && !file.startsWith('_snippets/') && !file.startsWith('snippets/'));
21
21
  yield Promise.all(pageFilenames.map((pageFilename) => __awaiter(void 0, void 0, void 0, function* () {
22
22
  const sourcePath = join(CMD_EXEC_PATH, pageFilename);
@@ -24,7 +24,7 @@ export const generatePagesWithImports = () => __awaiter(void 0, void 0, void 0,
24
24
  try {
25
25
  const tree = yield preparseMdxTree(contentStr, CMD_EXEC_PATH, sourcePath);
26
26
  const importsResponse = yield findAndRemoveImports(tree);
27
- if (hasImports(importsResponse)) {
27
+ if (Object.keys(importsResponse.importMap).some((importPath) => updatedSnippets.has(optionallyRemoveLeadingSlash(importPath)))) {
28
28
  const content = yield resolveAllImports({
29
29
  snippets,
30
30
  fileWithImports: Object.assign(Object.assign({}, importsResponse), { filename: pageFilename }),
@@ -1,2 +1,3 @@
1
- import type { FileType } from '@mintlify/common';
2
- export declare const getSnippets: () => Promise<FileType[]>;
1
+ import { type FileType } from '@mintlify/common';
2
+ export declare const getProcessedSnippets: () => Promise<FileType[]>;
3
+ export declare const getOriginalSnippets: () => Promise<FileType[]>;
@@ -7,18 +7,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import { optionallyAddLeadingSlash } from '@mintlify/common';
10
11
  import { getFileListSync, preparseMdxTree } from '@mintlify/prebuild';
11
12
  import { promises as _promises } from 'fs';
12
13
  import { join } from 'path';
13
- import { NEXT_PUBLIC_PATH } from '../../constants.js';
14
+ import { CMD_EXEC_PATH, NEXT_PUBLIC_PATH } from '../../constants.js';
14
15
  const { readFile } = _promises;
15
- export const getSnippets = () => __awaiter(void 0, void 0, void 0, function* () {
16
- const snippetFilenames = getFileListSync(NEXT_PUBLIC_PATH).filter((file) => file.endsWith('.mdx') && file.startsWith('snippets/'));
16
+ const getSnippetBase = (BASE_DIR) => __awaiter(void 0, void 0, void 0, function* () {
17
+ const snippetFilenames = getFileListSync(BASE_DIR).filter((file) => file.endsWith('.mdx') && file.startsWith('snippets/'));
17
18
  const promises = snippetFilenames.map((snippetFilename) => __awaiter(void 0, void 0, void 0, function* () {
18
19
  try {
19
- const tree = yield preparseMdxTree((yield readFile(join(NEXT_PUBLIC_PATH, snippetFilename))).toString(), NEXT_PUBLIC_PATH, join(NEXT_PUBLIC_PATH, snippetFilename));
20
+ const tree = yield preparseMdxTree((yield readFile(join(BASE_DIR, snippetFilename))).toString(), BASE_DIR, join(BASE_DIR, snippetFilename));
20
21
  return {
21
- filename: '/' + snippetFilename,
22
+ filename: optionallyAddLeadingSlash(snippetFilename),
22
23
  tree,
23
24
  };
24
25
  }
@@ -29,3 +30,5 @@ export const getSnippets = () => __awaiter(void 0, void 0, void 0, function* ()
29
30
  }));
30
31
  return (yield Promise.all(promises)).filter(Boolean);
31
32
  });
33
+ export const getProcessedSnippets = () => getSnippetBase(NEXT_PUBLIC_PATH);
34
+ export const getOriginalSnippets = () => getSnippetBase(CMD_EXEC_PATH);
@@ -17,6 +17,7 @@ import fs from 'fs/promises';
17
17
  import yaml from 'js-yaml';
18
18
  import pathUtil from 'path';
19
19
  import { CMD_EXEC_PATH, NEXT_PROPS_PATH, NEXT_PUBLIC_PATH, CLIENT_PATH } from '../../constants.js';
20
+ import { generateDependentSnippets } from './generateDependentSnippets.js';
20
21
  import { generatePagesWithImports } from './generatePagesWithImports.js';
21
22
  import { getDocsState } from './getDocsState.js';
22
23
  import { resolveAllImports } from './resolveAllImports.js';
@@ -228,7 +229,8 @@ const onUpdateEvent = (filename, callback) => __awaiter(void 0, void 0, void 0,
228
229
  yield fse.outputFile(targetPath, contentStr, {
229
230
  flag: 'w',
230
231
  });
231
- yield generatePagesWithImports();
232
+ const updatedSnippets = yield generateDependentSnippets(filename, importsResponse);
233
+ yield generatePagesWithImports(new Set(updatedSnippets));
232
234
  break;
233
235
  }
234
236
  case 'mintConfig':
@@ -8,9 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { resolveAllImports as baseResolveAllImports } from '@mintlify/common';
11
- import { getSnippets } from './getSnippets.js';
11
+ import { getProcessedSnippets } from './getSnippets.js';
12
12
  export const resolveAllImports = (fileWithImports) => __awaiter(void 0, void 0, void 0, function* () {
13
- const snippets = yield getSnippets();
13
+ const snippets = yield getProcessedSnippets();
14
14
  return yield baseResolveAllImports({
15
15
  snippets,
16
16
  fileWithImports,