@mintlify/common 1.0.1225 → 1.0.1226
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.
- package/dist/mdx/islands/index.d.ts +2 -0
- package/dist/mdx/islands/index.js +2 -0
- package/dist/mdx/islands/package-json.d.ts +5 -0
- package/dist/mdx/islands/package-json.js +15 -0
- package/dist/mdx/islands/package-sources.d.ts +59 -0
- package/dist/mdx/islands/package-sources.js +155 -0
- package/dist/mdx/server-only/index.d.ts +1 -0
- package/dist/mdx/server-only/index.js +1 -0
- package/dist/mdx/server-only/package-snippet-tree-map.d.ts +3 -0
- package/dist/mdx/server-only/package-snippet-tree-map.js +6 -0
- package/dist/mdx/snippets/index.d.ts +1 -0
- package/dist/mdx/snippets/index.js +1 -0
- package/dist/mdx/snippets/resolveSnippetImports.d.ts +9 -0
- package/dist/mdx/snippets/resolveSnippetImports.js +29 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
2
|
+
export const parsePackageJsonDependencies = (content) => {
|
|
3
|
+
let parsed;
|
|
4
|
+
try {
|
|
5
|
+
parsed = JSON.parse(content);
|
|
6
|
+
}
|
|
7
|
+
catch (error) {
|
|
8
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
9
|
+
return { packageNames: new Set(), error: `is not valid JSON: ${message}` };
|
|
10
|
+
}
|
|
11
|
+
if (!isRecord(parsed))
|
|
12
|
+
return { packageNames: new Set(), error: 'is not a JSON object' };
|
|
13
|
+
const { dependencies } = parsed;
|
|
14
|
+
return { packageNames: new Set(isRecord(dependencies) ? Object.keys(dependencies) : []) };
|
|
15
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Root } from 'mdast';
|
|
2
|
+
import type { IslandSpec } from './types.js';
|
|
3
|
+
export type PackageSource = {
|
|
4
|
+
path: string;
|
|
5
|
+
content: string;
|
|
6
|
+
};
|
|
7
|
+
export type PackageImportSource = {
|
|
8
|
+
source: string;
|
|
9
|
+
line?: number;
|
|
10
|
+
};
|
|
11
|
+
export type ParsedPackageSource = PackageSource & {
|
|
12
|
+
tree: Root;
|
|
13
|
+
imports: PackageImportSource[];
|
|
14
|
+
};
|
|
15
|
+
export type PackageSourceProblem = {
|
|
16
|
+
file: string;
|
|
17
|
+
line?: number;
|
|
18
|
+
message: string;
|
|
19
|
+
};
|
|
20
|
+
export type PackageSourceCategory = 'page' | 'snippet' | 'legacySnippet' | 'other';
|
|
21
|
+
export type ClassifyPackageSourcesOptions = {
|
|
22
|
+
packageNames: ReadonlySet<string>;
|
|
23
|
+
variables?: Record<string, string>;
|
|
24
|
+
normalizeImportSource?: (source: string, importer: string) => string;
|
|
25
|
+
};
|
|
26
|
+
export type ClassifiedPackageSources = {
|
|
27
|
+
pages: ParsedPackageSource[];
|
|
28
|
+
snippets: ParsedPackageSource[];
|
|
29
|
+
legacySnippets: ParsedPackageSource[];
|
|
30
|
+
unparsed: (PackageSource & {
|
|
31
|
+
category: PackageSourceCategory;
|
|
32
|
+
})[];
|
|
33
|
+
errors: PackageSourceProblem[];
|
|
34
|
+
};
|
|
35
|
+
export declare const sortPackageProblems: (problems: PackageSourceProblem[]) => PackageSourceProblem[];
|
|
36
|
+
export declare const classifyPackageSources: (files: readonly PackageSource[], { packageNames, variables, normalizeImportSource, }: ClassifyPackageSourcesOptions) => ClassifiedPackageSources;
|
|
37
|
+
export type ExtractPagePackageIslandsInput = {
|
|
38
|
+
path: string;
|
|
39
|
+
content: string;
|
|
40
|
+
packageNames: ReadonlySet<string>;
|
|
41
|
+
packageSnippets: ReadonlySet<string>;
|
|
42
|
+
variables?: Record<string, string>;
|
|
43
|
+
snippetTreeMap?: Record<string, Root>;
|
|
44
|
+
resolvedTree?: Root;
|
|
45
|
+
};
|
|
46
|
+
export type PagePackageIslands = {
|
|
47
|
+
islands: IslandSpec[];
|
|
48
|
+
ids: string[];
|
|
49
|
+
errors: PackageSourceProblem[];
|
|
50
|
+
};
|
|
51
|
+
export declare const packageIslandPagePath: (path: string) => string;
|
|
52
|
+
export declare const extractPagePackageIslands: ({ path, content, packageNames, packageSnippets, variables, snippetTreeMap, resolvedTree, }: ExtractPagePackageIslandsInput) => PagePackageIslands;
|
|
53
|
+
export type MergedPackageIslands = {
|
|
54
|
+
islands: IslandSpec[];
|
|
55
|
+
islandPages: Record<string, string[]>;
|
|
56
|
+
};
|
|
57
|
+
export declare const mergePackageIslands: (pages: Iterable<{
|
|
58
|
+
islands: readonly IslandSpec[];
|
|
59
|
+
}>) => MergedPackageIslands;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { getFileCategory } from '../../getFileCategory.js';
|
|
2
|
+
import { getAST } from '../remark.js';
|
|
3
|
+
import { replaceVariables } from '../replaceVariables.js';
|
|
4
|
+
import { extractImportSources } from '../snippets/extractImportSources.js';
|
|
5
|
+
import { getSpecifierPackageName } from '../snippets/isBareSpecifier.js';
|
|
6
|
+
import { resolveImportPath } from '../snippets/resolveImportPath.js';
|
|
7
|
+
import { isMdxJsEsm } from '../utils.js';
|
|
8
|
+
import { extractPackageIslands } from './extractPackageIslands.js';
|
|
9
|
+
import { isDeclaredPackageImport, isNonHostBareImport, isPackageSnippetPath, } from './packageImports.js';
|
|
10
|
+
import { getPackageIslandsForContent } from './remarkPackageIslands.js';
|
|
11
|
+
const IMPORT_SOURCE = /\bfrom\s*['"]([^'"\n]+)['"]|\bimport\s*['"]([^'"\n]+)['"]/g;
|
|
12
|
+
const compare = (a, b) => {
|
|
13
|
+
if (a < b)
|
|
14
|
+
return -1;
|
|
15
|
+
if (a > b)
|
|
16
|
+
return 1;
|
|
17
|
+
return 0;
|
|
18
|
+
};
|
|
19
|
+
const messageOf = (error) => (error instanceof Error ? error.message : String(error));
|
|
20
|
+
const parse = (content, path) => {
|
|
21
|
+
try {
|
|
22
|
+
return getAST(content, path);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
return messageOf(error);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const topLevelImports = (tree) => tree.children.flatMap((node) => {
|
|
29
|
+
var _a, _b, _c;
|
|
30
|
+
if (!isMdxJsEsm(node))
|
|
31
|
+
return [];
|
|
32
|
+
return ((_c = (_b = (_a = node.data) === null || _a === void 0 ? void 0 : _a.estree) === null || _b === void 0 ? void 0 : _b.body) !== null && _c !== void 0 ? _c : []).flatMap((statement) => {
|
|
33
|
+
var _a, _b, _c;
|
|
34
|
+
if (statement.type !== 'ImportDeclaration' || typeof statement.source.value !== 'string') {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
return [
|
|
38
|
+
{
|
|
39
|
+
source: statement.source.value,
|
|
40
|
+
line: (_b = (_a = statement.loc) === null || _a === void 0 ? void 0 : _a.start.line) !== null && _b !== void 0 ? _b : (_c = node.position) === null || _c === void 0 ? void 0 : _c.start.line,
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
export const sortPackageProblems = (problems) => [...problems].sort((a, b) => { var _a, _b; return compare(a.file, b.file) || ((_a = a.line) !== null && _a !== void 0 ? _a : 0) - ((_b = b.line) !== null && _b !== void 0 ? _b : 0); });
|
|
46
|
+
export const classifyPackageSources = (files, { packageNames, variables, normalizeImportSource = (source) => source, }) => {
|
|
47
|
+
var _a;
|
|
48
|
+
const resolve = (source, importer) => resolveImportPath(normalizeImportSource(source, importer), importer);
|
|
49
|
+
const usesPackages = ({ path, content }) => [...replaceVariables(content, variables).matchAll(IMPORT_SOURCE)].some(([, from, bare]) => {
|
|
50
|
+
var _a;
|
|
51
|
+
const source = normalizeImportSource((_a = from !== null && from !== void 0 ? from : bare) !== null && _a !== void 0 ? _a : '', path);
|
|
52
|
+
if (isDeclaredPackageImport(source, packageNames))
|
|
53
|
+
return true;
|
|
54
|
+
const resolved = resolveImportPath(source, path);
|
|
55
|
+
return resolved !== null && isPackageSnippetPath(resolved);
|
|
56
|
+
});
|
|
57
|
+
const parsed = [];
|
|
58
|
+
const failed = [];
|
|
59
|
+
for (const file of [...files].sort((a, b) => compare(a.path, b.path))) {
|
|
60
|
+
const tree = parse(replaceVariables(file.content, variables), file.path);
|
|
61
|
+
if (typeof tree === 'string')
|
|
62
|
+
failed.push(Object.assign(Object.assign({}, file), { message: tree }));
|
|
63
|
+
else
|
|
64
|
+
parsed.push(Object.assign(Object.assign({}, file), { tree, imports: topLevelImports(tree) }));
|
|
65
|
+
}
|
|
66
|
+
const importedFiles = new Set();
|
|
67
|
+
for (const file of parsed) {
|
|
68
|
+
for (const source of extractImportSources(file.tree)) {
|
|
69
|
+
const resolved = resolve(source, file.path);
|
|
70
|
+
if (resolved !== null)
|
|
71
|
+
importedFiles.add(resolved.toLowerCase());
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const categoryOf = (path) => {
|
|
75
|
+
const category = getFileCategory(path, { importedFiles });
|
|
76
|
+
if (category === 'page')
|
|
77
|
+
return 'page';
|
|
78
|
+
if (category === 'snippet-v2')
|
|
79
|
+
return 'snippet';
|
|
80
|
+
if (category === 'snippet')
|
|
81
|
+
return 'legacySnippet';
|
|
82
|
+
return 'other';
|
|
83
|
+
};
|
|
84
|
+
const result = {
|
|
85
|
+
pages: [],
|
|
86
|
+
snippets: [],
|
|
87
|
+
legacySnippets: [],
|
|
88
|
+
unparsed: [],
|
|
89
|
+
errors: [],
|
|
90
|
+
};
|
|
91
|
+
for (const file of failed) {
|
|
92
|
+
const category = categoryOf(file.path);
|
|
93
|
+
result.unparsed.push({ path: file.path, content: file.content, category });
|
|
94
|
+
if (usesPackages(file))
|
|
95
|
+
result.errors.push({ file: file.path, message: file.message });
|
|
96
|
+
}
|
|
97
|
+
for (const file of parsed) {
|
|
98
|
+
const category = categoryOf(file.path);
|
|
99
|
+
if (category === 'legacySnippet')
|
|
100
|
+
result.legacySnippets.push(file);
|
|
101
|
+
if (category !== 'page' && category !== 'snippet')
|
|
102
|
+
continue;
|
|
103
|
+
(category === 'page' ? result.pages : result.snippets).push(file);
|
|
104
|
+
for (const { source, line } of file.imports) {
|
|
105
|
+
const normalizedSource = normalizeImportSource(source, file.path);
|
|
106
|
+
if (!isNonHostBareImport(normalizedSource) ||
|
|
107
|
+
isDeclaredPackageImport(normalizedSource, packageNames)) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
result.errors.push(Object.assign(Object.assign({ file: file.path }, (line !== undefined && { line })), { message: `imports "${(_a = getSpecifierPackageName(normalizedSource)) !== null && _a !== void 0 ? _a : normalizedSource}", which isn't listed in package.json dependencies.` }));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
};
|
|
115
|
+
export const packageIslandPagePath = (path) => path.replace(/\.mdx?$/, '');
|
|
116
|
+
export const extractPagePackageIslands = ({ path, content, packageNames, packageSnippets, variables, snippetTreeMap, resolvedTree, }) => {
|
|
117
|
+
const options = { packageNames, packageSnippets, pagePath: packageIslandPagePath(path) };
|
|
118
|
+
let extraction;
|
|
119
|
+
try {
|
|
120
|
+
extraction = getPackageIslandsForContent(Object.assign(Object.assign({}, options), { content, variables, snippetTreeMap }));
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
return {
|
|
124
|
+
islands: [],
|
|
125
|
+
ids: [],
|
|
126
|
+
errors: [
|
|
127
|
+
{ file: path, message: `can't be parsed after resolving imports: ${messageOf(error)}` },
|
|
128
|
+
],
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const located = resolvedTree && extraction.errors.length > 0 && !extraction.errors.some((error) => error.code)
|
|
132
|
+
? extractPackageIslands(resolvedTree, options).errors.filter((error) => !error.code)
|
|
133
|
+
: [];
|
|
134
|
+
return {
|
|
135
|
+
islands: extraction.islands,
|
|
136
|
+
ids: extraction.roots.map((root) => root.id),
|
|
137
|
+
errors: (located.length > 0 ? located : extraction.errors).map(({ line, message }) => (Object.assign(Object.assign({ file: path }, (line !== undefined && { line })), { message }))),
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
export const mergePackageIslands = (pages) => {
|
|
141
|
+
var _a;
|
|
142
|
+
const islands = new Map();
|
|
143
|
+
const islandPages = new Map();
|
|
144
|
+
for (const page of pages) {
|
|
145
|
+
for (const island of page.islands) {
|
|
146
|
+
if (!islands.has(island.id))
|
|
147
|
+
islands.set(island.id, island);
|
|
148
|
+
islandPages.set(island.id, ((_a = islandPages.get(island.id)) !== null && _a !== void 0 ? _a : new Set()).add(island.pagePath));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
islands: [...islands.values()],
|
|
153
|
+
islandPages: Object.fromEntries([...islandPages].map(([id, paths]) => [id, [...paths].sort(compare)])),
|
|
154
|
+
};
|
|
155
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { createSnippetTreeMap, getMdx, type GetMdxType, type PackageBuildInput, type Snippet, } from './getMdx.js';
|
|
2
2
|
export { getSerializeExtras } from './getSerializeExtras.js';
|
|
3
|
+
export { createPackageSnippetTreeMap } from './package-snippet-tree-map.js';
|
|
3
4
|
export { packagePageBaseUrl, packagePageStylesheets } from '../islands/package-page-url.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { createSnippetTreeMap, getMdx, } from './getMdx.js';
|
|
2
2
|
export { getSerializeExtras } from './getSerializeExtras.js';
|
|
3
|
+
export { createPackageSnippetTreeMap } from './package-snippet-tree-map.js';
|
|
3
4
|
export { packagePageBaseUrl, packagePageStylesheets } from '../islands/package-page-url.js';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Root } from 'mdast';
|
|
2
|
+
import type { PackageSource } from '../islands/package-sources.js';
|
|
3
|
+
export declare const createPackageSnippetTreeMap: (legacySnippets: readonly PackageSource[], variables: Record<string, string> | undefined) => Promise<Record<string, Root>>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { preprocessPageContent } from '../islands/remarkPackageIslands.js';
|
|
2
|
+
import { createSnippetTreeMap } from './getMdx/snippets.js';
|
|
3
|
+
export const createPackageSnippetTreeMap = (legacySnippets, variables) => createSnippetTreeMap(legacySnippets.map(({ path, content }) => ({
|
|
4
|
+
snippetFileLocation: path.replace(/^\/?_snippets\//, ''),
|
|
5
|
+
content: preprocessPageContent(content, variables),
|
|
6
|
+
})));
|
|
@@ -10,6 +10,7 @@ export * from './isImportedAsSnippet.js';
|
|
|
10
10
|
export * from './nodeIncludesExport.js';
|
|
11
11
|
export * from './removeExports.js';
|
|
12
12
|
export * from './resolveAllImports.js';
|
|
13
|
+
export * from './resolveSnippetImports.js';
|
|
13
14
|
export * from './resolveImport/index.js';
|
|
14
15
|
export * from './resolveImportPath.js';
|
|
15
16
|
export * from './isBareSpecifier.js';
|
|
@@ -10,6 +10,7 @@ export * from './isImportedAsSnippet.js';
|
|
|
10
10
|
export * from './nodeIncludesExport.js';
|
|
11
11
|
export * from './removeExports.js';
|
|
12
12
|
export * from './resolveAllImports.js';
|
|
13
|
+
export * from './resolveSnippetImports.js';
|
|
13
14
|
export * from './resolveImport/index.js';
|
|
14
15
|
export * from './resolveImportPath.js';
|
|
15
16
|
export * from './isBareSpecifier.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FileWithImports } from '../../types/mdx/index.js';
|
|
2
|
+
import type { PackageImportOptions } from '../islands/packageImports.js';
|
|
3
|
+
import { type ResolveImportsWarning } from './resolveAllImports.js';
|
|
4
|
+
export type ResolveSnippetImportsOptions = {
|
|
5
|
+
packages?: PackageImportOptions;
|
|
6
|
+
onWarning?: (warning: ResolveImportsWarning, filename: string) => void;
|
|
7
|
+
skip?: (filename: string) => boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare const resolveSnippetImports: (snippets: FileWithImports[], { packages, onWarning, skip }?: ResolveSnippetImportsOptions) => Promise<FileWithImports[]>;
|
|
@@ -0,0 +1,29 @@
|
|
|
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 { topologicalSort } from '../../topologicalSort.js';
|
|
11
|
+
import { hasImports } from './hasImports.js';
|
|
12
|
+
import { resolveAllImports } from './resolveAllImports.js';
|
|
13
|
+
import { resolveImportPath } from './resolveImportPath.js';
|
|
14
|
+
export const resolveSnippetImports = (snippets_1, ...args_1) => __awaiter(void 0, [snippets_1, ...args_1], void 0, function* (snippets, { packages, onWarning, skip } = {}) {
|
|
15
|
+
const graph = {};
|
|
16
|
+
for (const snippet of snippets) {
|
|
17
|
+
graph[snippet.filename] = Object.keys(snippet.importMap).flatMap((source) => { var _a; return (_a = resolveImportPath(source, snippet.filename)) !== null && _a !== void 0 ? _a : []; });
|
|
18
|
+
}
|
|
19
|
+
const byFilename = new Map(snippets.map((snippet) => [snippet.filename, snippet]));
|
|
20
|
+
const ordered = topologicalSort(graph)
|
|
21
|
+
.reverse()
|
|
22
|
+
.flatMap((filename) => { var _a; return (_a = byFilename.get(filename)) !== null && _a !== void 0 ? _a : []; });
|
|
23
|
+
for (const snippet of ordered) {
|
|
24
|
+
if (!hasImports(snippet) || (skip === null || skip === void 0 ? void 0 : skip(snippet.filename)))
|
|
25
|
+
continue;
|
|
26
|
+
snippet.tree = yield resolveAllImports(Object.assign(Object.assign({ snippets: ordered, fileWithImports: snippet }, (onWarning && { onWarning: (warning) => onWarning(warning, snippet.filename) })), (packages && { packages })));
|
|
27
|
+
}
|
|
28
|
+
return snippets;
|
|
29
|
+
});
|