@mintlify/common 1.0.1221 → 1.0.1223

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.
Files changed (43) hide show
  1. package/dist/colors.d.ts +4 -0
  2. package/dist/colors.js +6 -1
  3. package/dist/mdx/getMDXOptions.d.ts +3 -1
  4. package/dist/mdx/getMDXOptions.js +7 -1
  5. package/dist/mdx/index.d.ts +1 -0
  6. package/dist/mdx/index.js +1 -0
  7. package/dist/mdx/islands/extractPackageIslands.d.ts +5 -0
  8. package/dist/mdx/islands/extractPackageIslands.js +147 -0
  9. package/dist/mdx/islands/index.d.ts +5 -0
  10. package/dist/mdx/islands/index.js +5 -0
  11. package/dist/mdx/islands/island-bindings.d.ts +40 -0
  12. package/dist/mdx/islands/island-bindings.js +149 -0
  13. package/dist/mdx/islands/island-module-builder.d.ts +37 -0
  14. package/dist/mdx/islands/island-module-builder.js +472 -0
  15. package/dist/mdx/islands/island-placement.d.ts +10 -0
  16. package/dist/mdx/islands/island-placement.js +229 -0
  17. package/dist/mdx/islands/packageImports.d.ts +28 -0
  18. package/dist/mdx/islands/packageImports.js +155 -0
  19. package/dist/mdx/islands/remarkPackageIslands.d.ts +23 -0
  20. package/dist/mdx/islands/remarkPackageIslands.js +177 -0
  21. package/dist/mdx/islands/sha256.d.ts +1 -0
  22. package/dist/mdx/islands/sha256.js +71 -0
  23. package/dist/mdx/islands/static-expressions.d.ts +24 -0
  24. package/dist/mdx/islands/static-expressions.js +169 -0
  25. package/dist/mdx/islands/types.d.ts +36 -0
  26. package/dist/mdx/islands/types.js +1 -0
  27. package/dist/mdx/plugins/remark/remarkMdxExpandExpressions.js +1 -1
  28. package/dist/mdx/plugins/remark/remarkMdxInjectSnippets.d.ts +4 -1
  29. package/dist/mdx/plugins/remark/remarkMdxInjectSnippets.js +68 -17
  30. package/dist/mdx/plugins/remark/remarkMdxRemoveJs.js +1 -1
  31. package/dist/mdx/plugins/remark/remarkResolveRelativeLinks.d.ts +1 -0
  32. package/dist/mdx/plugins/remark/remarkResolveRelativeLinks.js +6 -3
  33. package/dist/mdx/server-only/getMdx.d.ts +11 -1
  34. package/dist/mdx/server-only/getMdx.js +66 -31
  35. package/dist/mdx/server-only/index.d.ts +1 -1
  36. package/dist/mdx/server-only/index.js +1 -1
  37. package/dist/mdx/snippets/resolveAllImports.d.ts +3 -1
  38. package/dist/mdx/snippets/resolveAllImports.js +65 -1
  39. package/dist/mdx/static-object-keys.d.ts +1 -0
  40. package/dist/mdx/static-object-keys.js +1 -0
  41. package/dist/tsconfig.build.tsbuildinfo +1 -1
  42. package/dist/types/mdx/MdxExtracts.d.ts +6 -0
  43. package/package.json +3 -3
@@ -11,15 +11,35 @@ import { serialize } from '@mintlify/mdx/server';
11
11
  import { getTailwindSelectors } from '../../css/tailwind.js';
12
12
  import { getMDXOptions, remarkMdxRemoveJs, remarkExpandContent, remarkSplitCodeGroup, remarkSplitTabs, remarkWrapCodeBlocks, remarkValidateAccordions, remarkValidateSteps, remarkValidateVisibility, remarkValidateTabs, remarkValidateApiExamples, } from '../../index.js';
13
13
  import { codeStylingToThemeOrThemes } from '../getCodeStyling.js';
14
+ import { MINT_ISLAND_COMPONENT } from '../islands/packageImports.js';
15
+ import { PackageIslandsError, parsePackageIslands, preprocessPageContent, } from '../islands/remarkPackageIslands.js';
14
16
  import { preprocessCustomHeadingIds } from '../preprocessCustomHeadingIds.js';
15
17
  import { preprocessMdxFragmentCode } from '../preprocessMdxFragmentCode.js';
16
18
  import { replaceVariables } from '../replaceVariables.js';
17
19
  import { createSnippetTreeMap } from './getMdx/snippets.js';
18
20
  import { getSerializeExtras } from './getSerializeExtras.js';
21
+ const PACKAGE_BASE_URL = /^\/_mintlify\/pkg\/[A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*$/;
22
+ const getPackageIslandOptions = (packageBuild, pagePath, processedContent, snippetTreeMap) => {
23
+ if (!PACKAGE_BASE_URL.test(packageBuild.baseUrl)) {
24
+ throw new Error(`packageBuild.baseUrl must be a /_mintlify/pkg/ path: ${packageBuild.baseUrl}`);
25
+ }
26
+ const options = Object.assign({ packageNames: new Set(packageBuild.packageNames), pagePath }, (packageBuild.packageSnippets && {
27
+ packageSnippets: new Set(packageBuild.packageSnippets),
28
+ }));
29
+ let plan;
30
+ try {
31
+ plan = parsePackageIslands(processedContent, options, snippetTreeMap);
32
+ }
33
+ catch (_a) {
34
+ plan = undefined;
35
+ }
36
+ return Object.assign(Object.assign({}, options), { plan, islandHtml: packageBuild.islands, baseUrl: packageBuild.baseUrl });
37
+ };
19
38
  export function getMdx(_a) {
20
- return __awaiter(this, arguments, void 0, function* ({ path, content, metadata, snippets, subdomain, codeStyling, config, tailwindSelectors = undefined, pageType = 'default', trace = undefined, customLanguages = [], variables = undefined, rehypePlugins = [], remarkPlugins = [], earlyRemarkPlugins = [], allowedComponents = [], singlePass = true, }) {
39
+ return __awaiter(this, arguments, void 0, function* ({ path, content, metadata, snippets, subdomain, codeStyling, config, tailwindSelectors = undefined, pageType = 'default', trace = undefined, customLanguages = [], variables = undefined, rehypePlugins = [], remarkPlugins = [], earlyRemarkPlugins = [], allowedComponents = [], packageBuild = undefined, singlePass = true, }) {
40
+ var _b, _c;
21
41
  const traceFn = trace !== null && trace !== void 0 ? trace : ((_name, fn) => fn());
22
- const processedContent = preprocessCustomHeadingIds(preprocessMdxFragmentCode(replaceVariables(content, variables)));
42
+ const processedContent = preprocessPageContent(content, variables);
23
43
  const processedSnippets = snippets.map((snippet) => (Object.assign(Object.assign({}, snippet), { content: preprocessCustomHeadingIds(preprocessMdxFragmentCode(replaceVariables(snippet.content, variables))) })));
24
44
  if (!tailwindSelectors)
25
45
  tailwindSelectors = yield traceFn('getMdx.getTailwindSelectors', () => __awaiter(this, void 0, void 0, function* () { return getTailwindSelectors({ content: processedContent }); }));
@@ -33,6 +53,13 @@ export function getMdx(_a) {
33
53
  tailwindSelectors,
34
54
  path,
35
55
  };
56
+ const packageIslands = packageBuild &&
57
+ (yield traceFn('getMdx.packageIslands', () => __awaiter(this, void 0, void 0, function* () { return getPackageIslandOptions(packageBuild, path, processedContent, snippetTreeMap); })));
58
+ if (packageBuild)
59
+ allowedComponents = [...allowedComponents, MINT_ISLAND_COMPONENT];
60
+ const planErrors = (_c = (_b = packageIslands === null || packageIslands === void 0 ? void 0 : packageIslands.plan) === null || _b === void 0 ? void 0 : _b.errors) !== null && _c !== void 0 ? _c : [];
61
+ const planFailure = planErrors.length > 0 ? new PackageIslandsError(path, planErrors) : undefined;
62
+ const compiledIslands = planFailure ? undefined : packageIslands;
36
63
  let mdxExtracts = {};
37
64
  let plugins = [
38
65
  remarkValidateSteps,
@@ -55,24 +82,30 @@ export function getMdx(_a) {
55
82
  // detector sits exactly where remarkMdxRemoveJs sits in the no-js pass, so it
56
83
  // sees the identical tree state and applies the identical decisions
57
84
  const jsDetection = { hasJs: true };
58
- const mdxOptions = getMDXOptions({
59
- data: mdxOptionsData,
60
- remarkPlugins: plugins,
61
- earlyRemarkPlugins,
62
- rehypePlugins,
63
- mdxExtracts,
64
- detectJs: jsDetection,
65
- allowedComponents,
66
- fragmentPlugins: getSerializeExtras(syntaxHighlightingOptions),
67
- });
68
- const mdxOptionsNoJs = getMDXOptions({
69
- data: mdxOptionsData,
70
- remarkPlugins: [remarkMdxRemoveJs, ...plugins],
71
- earlyRemarkPlugins,
72
- rehypePlugins,
73
- allowedComponents,
74
- fragmentPlugins: getSerializeExtras(syntaxHighlightingOptions),
85
+ const buildMdxOptions = (islands) => ({
86
+ mdxOptions: getMDXOptions({
87
+ data: mdxOptionsData,
88
+ remarkPlugins: plugins,
89
+ earlyRemarkPlugins,
90
+ rehypePlugins,
91
+ mdxExtracts,
92
+ allowedComponents,
93
+ fragmentPlugins: getSerializeExtras(syntaxHighlightingOptions),
94
+ detectJs: jsDetection,
95
+ packageIslands: islands,
96
+ }),
97
+ mdxOptionsNoJs: getMDXOptions({
98
+ data: mdxOptionsData,
99
+ remarkPlugins: [remarkMdxRemoveJs, ...plugins],
100
+ earlyRemarkPlugins,
101
+ rehypePlugins,
102
+ allowedComponents,
103
+ fragmentPlugins: getSerializeExtras(syntaxHighlightingOptions),
104
+ packageIslands: islands,
105
+ }),
75
106
  });
107
+ const { mdxOptions, mdxOptionsNoJs } = buildMdxOptions(compiledIslands);
108
+ const withoutIslands = compiledIslands ? buildMdxOptions() : { mdxOptions, mdxOptionsNoJs };
76
109
  const scope = {
77
110
  codeStyling,
78
111
  config,
@@ -81,14 +114,16 @@ export function getMdx(_a) {
81
114
  const { mdxSource, mdxSourceWithNoJs, parseFailed: mainParseFailed, } = yield traceFn('getMdx.compileMdx.mainContent', () => __awaiter(this, void 0, void 0, function* () {
82
115
  return compileMdx({
83
116
  content: processedContent,
117
+ failure: planFailure,
84
118
  scope,
85
119
  mdxOptions,
86
120
  mdxOptionsNoJs,
121
+ fallbackOptions: withoutIslands,
87
122
  codeStyling,
88
123
  path,
89
124
  traceFn,
90
125
  customLanguages,
91
- skipNoJs: singlePass ? { detection: jsDetection, mdxExtracts } : undefined,
126
+ skipNoJs: singlePass && !compiledIslands ? { detection: jsDetection, mdxExtracts } : undefined,
92
127
  });
93
128
  }));
94
129
  let parseFailed = mainParseFailed;
@@ -98,16 +133,10 @@ export function getMdx(_a) {
98
133
  if (mdxExtracts.panel && mdxExtracts.panel.content) {
99
134
  const panelContent = mdxExtracts.panel.content;
100
135
  const compiledMdx = yield traceFn('getMdx.compileMdx.panelContent', () => __awaiter(this, void 0, void 0, function* () {
101
- return compileMdx({
102
- content: panelContent,
103
- scope,
104
- mdxOptions,
105
- mdxOptionsNoJs,
106
- codeStyling,
136
+ return compileMdx(Object.assign(Object.assign({ content: panelContent, scope }, withoutIslands), { codeStyling,
107
137
  path,
108
138
  traceFn,
109
- customLanguages,
110
- });
139
+ customLanguages }));
111
140
  }));
112
141
  panelMdxSource = compiledMdx.mdxSource;
113
142
  panelMdxSourceWithNoJs = compiledMdx.mdxSourceWithNoJs;
@@ -128,12 +157,14 @@ export function getMdx(_a) {
128
157
  });
129
158
  }
130
159
  function compileMdx(_a) {
131
- return __awaiter(this, arguments, void 0, function* ({ content, scope, mdxOptions, mdxOptionsNoJs, codeStyling, path, traceFn, customLanguages, skipNoJs, }) {
160
+ return __awaiter(this, arguments, void 0, function* ({ content, failure, scope, mdxOptions, mdxOptionsNoJs, fallbackOptions, codeStyling, path, traceFn, customLanguages, skipNoJs, }) {
132
161
  const withJs = yield traceFn('compileMdx.withJs', () => __awaiter(this, void 0, void 0, function* () {
133
162
  return getCompiledMdxWithFallback({
134
163
  source: content,
164
+ failure,
135
165
  scope,
136
166
  mdxOptions,
167
+ fallbackMdxOptions: fallbackOptions === null || fallbackOptions === void 0 ? void 0 : fallbackOptions.mdxOptions,
137
168
  codeStyling,
138
169
  path,
139
170
  traceFn,
@@ -159,8 +190,10 @@ function compileMdx(_a) {
159
190
  const withoutJs = yield traceFn('compileMdx.withoutJs', () => __awaiter(this, void 0, void 0, function* () {
160
191
  return getCompiledMdxWithFallback({
161
192
  source: content,
193
+ failure,
162
194
  scope,
163
195
  mdxOptions: mdxOptionsNoJs,
196
+ fallbackMdxOptions: fallbackOptions === null || fallbackOptions === void 0 ? void 0 : fallbackOptions.mdxOptionsNoJs,
164
197
  codeStyling,
165
198
  path,
166
199
  traceFn,
@@ -175,9 +208,11 @@ function compileMdx(_a) {
175
208
  });
176
209
  }
177
210
  function getCompiledMdxWithFallback(_a) {
178
- return __awaiter(this, arguments, void 0, function* ({ source, scope, mdxOptions, codeStyling, path, traceFn, customLanguages, }) {
211
+ return __awaiter(this, arguments, void 0, function* ({ source, failure, scope, mdxOptions, fallbackMdxOptions = mdxOptions, codeStyling, path, traceFn, customLanguages, }) {
179
212
  const themeOrThemes = codeStylingToThemeOrThemes(codeStyling);
180
213
  try {
214
+ if (failure)
215
+ throw failure;
181
216
  const result = yield traceFn('getCompiledMdxWithFallback.serialize', () => __awaiter(this, void 0, void 0, function* () {
182
217
  return serialize({
183
218
  source,
@@ -200,7 +235,7 @@ function getCompiledMdxWithFallback(_a) {
200
235
  return serialize({
201
236
  source: '🚧 A parsing error occured. Please contact the owner of this website. They can use the Mintlify CLI to test this website locally and see the errors that occur.',
202
237
  scope,
203
- mdxOptions,
238
+ mdxOptions: fallbackMdxOptions,
204
239
  syntaxHighlightingOptions: Object.assign(Object.assign({}, themeOrThemes), { customLanguages }),
205
240
  });
206
241
  }));
@@ -1,2 +1,2 @@
1
- export { createSnippetTreeMap, getMdx, type GetMdxType, type Snippet } from './getMdx.js';
1
+ export { createSnippetTreeMap, getMdx, type GetMdxType, type PackageBuildInput, type Snippet, } from './getMdx.js';
2
2
  export { getSerializeExtras } from './getSerializeExtras.js';
@@ -1,2 +1,2 @@
1
- export { createSnippetTreeMap, getMdx } from './getMdx.js';
1
+ export { createSnippetTreeMap, getMdx, } from './getMdx.js';
2
2
  export { getSerializeExtras } from './getSerializeExtras.js';
@@ -1,11 +1,13 @@
1
1
  import type { Root } from 'mdast';
2
2
  import type { FileType, FileWithImports } from '../../types/mdx/index.js';
3
+ import { type PackageImportOptions } from '../islands/packageImports.js';
3
4
  export type ResolveImportsWarning = {
4
- type: 'invalid-import-path' | 'missing-file' | 'import-resolution-error' | 'default-import-no-default-export' | 'export-collision';
5
+ type: 'invalid-import-path' | 'missing-file' | 'import-resolution-error' | 'default-import-no-default-export' | 'export-collision' | 'undeclared-package' | 'package-import-conflict';
5
6
  message: string;
6
7
  };
7
8
  export declare const resolveAllImports: (params: {
8
9
  snippets: FileType[];
9
10
  fileWithImports: FileWithImports;
10
11
  onWarning?: (warning: ResolveImportsWarning) => void;
12
+ packages?: PackageImportOptions;
11
13
  }) => Promise<Root>;
@@ -7,16 +7,60 @@ 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 { remove } from 'unist-util-remove';
11
+ import { visit } from 'unist-util-visit';
10
12
  import { MdxImportSpecifier } from '../../types/mdx/snippets/import.js';
13
+ import { createImportNode, isDeclaredPackageImport, isNonHostBareImport, mergeImports, } from '../islands/packageImports.js';
14
+ import { estreeIsProgram, isImportDeclaration, isMdxJsEsm } from '../utils.js';
15
+ import { findAndRemoveImports } from './findAndRemoveImports.js';
11
16
  import { getExportMapFromTree } from './getExportMap.js';
17
+ import { getSpecifierPackageName } from './isBareSpecifier.js';
12
18
  import { resolveImport } from './resolveImport/index.js';
19
+ import { injectToTopOfFileOfTree } from './resolveImport/injectToTopOfFile.js';
13
20
  import { resolveImportPath } from './resolveImportPath.js';
21
+ const hasImportDeclaration = (node) => isMdxJsEsm(node) && estreeIsProgram(node) && node.data.estree.body.some(isImportDeclaration);
22
+ const takeImports = (tree) => __awaiter(void 0, void 0, void 0, function* () {
23
+ const carriers = new Set();
24
+ visit(tree, (node) => {
25
+ if (hasImportDeclaration(node))
26
+ carriers.add(node);
27
+ });
28
+ if (carriers.size === 0)
29
+ return {};
30
+ const { importMap, tree: rest } = yield findAndRemoveImports({
31
+ type: 'root',
32
+ children: [...carriers],
33
+ });
34
+ const remaining = new Set(rest.children);
35
+ remove(tree, { cascade: false }, (node) => carriers.has(node) && !remaining.has(node));
36
+ return importMap;
37
+ });
14
38
  export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, function* () {
15
- const { snippets, fileWithImports, onWarning } = params;
39
+ var _a, _b, _c, _d;
40
+ const { snippets, fileWithImports, onWarning, packages } = params;
16
41
  let ast = fileWithImports.tree;
17
42
  const exportMap = getExportMapFromTree(ast);
43
+ const kept = [];
18
44
  for (const source of Object.keys(fileWithImports.importMap)) {
19
45
  const resolvedPath = resolveImportPath(source, fileWithImports.filename);
46
+ if (packages && resolvedPath == null && isNonHostBareImport(source)) {
47
+ if (isDeclaredPackageImport(source, packages.packageNames)) {
48
+ kept.push([source, (_a = fileWithImports.importMap[source]) !== null && _a !== void 0 ? _a : []]);
49
+ continue;
50
+ }
51
+ const message = `Package ${(_b = getSpecifierPackageName(source)) !== null && _b !== void 0 ? _b : source} imported in ${fileWithImports.filename} is not declared in package.json dependencies.`;
52
+ if (onWarning) {
53
+ onWarning({ type: 'undeclared-package', message });
54
+ }
55
+ else {
56
+ console.log(message);
57
+ }
58
+ continue;
59
+ }
60
+ if (resolvedPath != null && ((_c = packages === null || packages === void 0 ? void 0 : packages.packageSnippets) === null || _c === void 0 ? void 0 : _c.has(resolvedPath))) {
61
+ kept.push([resolvedPath, (_d = fileWithImports.importMap[source]) !== null && _d !== void 0 ? _d : []]);
62
+ continue;
63
+ }
20
64
  if (resolvedPath == null) {
21
65
  const message = `Invalid import path ${source} in ${fileWithImports.filename}. Only local imports are supported.`;
22
66
  if (onWarning) {
@@ -73,5 +117,25 @@ export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, f
73
117
  }
74
118
  }
75
119
  }
120
+ if (packages)
121
+ kept.push(...Object.entries(yield takeImports(ast)));
122
+ if (!packages || kept.length === 0)
123
+ return ast;
124
+ const { imports, conflicts } = mergeImports(kept.filter(([source, specifiers]) => {
125
+ var _a;
126
+ return specifiers.length > 0 ||
127
+ isDeclaredPackageImport(source, packages.packageNames) ||
128
+ ((_a = packages.packageSnippets) === null || _a === void 0 ? void 0 : _a.has(source));
129
+ }));
130
+ for (const { local, first, second } of conflicts) {
131
+ const message = `${local} is imported twice in ${fileWithImports.filename}, as ${first} and as ${second}. Snippets inlined into a page share its imports, so import one of them under another name.`;
132
+ if (onWarning) {
133
+ onWarning({ type: 'package-import-conflict', message });
134
+ }
135
+ else {
136
+ console.log(message);
137
+ }
138
+ }
139
+ injectToTopOfFileOfTree(ast, [createImportNode(imports)]);
76
140
  return ast;
77
141
  });
@@ -0,0 +1 @@
1
+ export declare const UNSAFE_OBJECT_KEYS: Set<string>;
@@ -0,0 +1 @@
1
+ export const UNSAFE_OBJECT_KEYS = new Set(['__proto__', 'constructor', 'prototype']);