@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
@@ -0,0 +1,229 @@
1
+ import { walk } from 'estree-walker';
2
+ const BLOCK_PARENTS = new Set(['table', 'thead', 'tbody', 'tfoot', 'tr', 'colgroup', 'svg']);
3
+ const SVG_HTML_POINTS = new Set(['foreignObject', 'desc', 'title']);
4
+ const OPAQUE_PARENTS = new Set(['head', 'script']);
5
+ export const isItemLoose = (item) => item.spread === null || item.spread === undefined ? item.children.length > 1 : item.spread;
6
+ export const isListLoose = (list) => Boolean(list.spread) || list.children.some(isItemLoose);
7
+ export const isIntrinsic = (name) => /^[a-z]/.test(name) && !name.includes('.');
8
+ const blockerInside = (name, blocker) => {
9
+ if (name === 'p')
10
+ return 'p';
11
+ if (!name || !isIntrinsic(name) || blocker === 'p' || blocker === 'paragraph')
12
+ return blocker;
13
+ if (blocker && OPAQUE_PARENTS.has(blocker))
14
+ return blocker;
15
+ if (OPAQUE_PARENTS.has(name))
16
+ return name;
17
+ if (blocker === 'svg')
18
+ return SVG_HTML_POINTS.has(name) ? false : blocker;
19
+ if (BLOCK_PARENTS.has(name))
20
+ return name;
21
+ return name === 'form' ? blocker : false;
22
+ };
23
+ const jsxNameOf = (name) => {
24
+ if (name.type === 'JSXIdentifier')
25
+ return name.name;
26
+ if (name.type === 'JSXNamespacedName')
27
+ return `${name.namespace.name}:${name.name.name}`;
28
+ return `${jsxNameOf(name.object)}.${name.property.name}`;
29
+ };
30
+ const isFunction = (node) => node.type === 'FunctionDeclaration' ||
31
+ node.type === 'FunctionExpression' ||
32
+ node.type === 'ArrowFunctionExpression';
33
+ const propertyName = (node, computed) => {
34
+ if (!computed && node.type === 'Identifier')
35
+ return node.name;
36
+ if (node.type === 'Literal' && typeof node.value === 'string')
37
+ return node.value;
38
+ return undefined;
39
+ };
40
+ const bindPattern = (pattern, origin, scope) => {
41
+ if (pattern.type === 'Identifier') {
42
+ scope.set(pattern.name, origin);
43
+ }
44
+ else if (pattern.type === 'AssignmentPattern') {
45
+ bindPattern(pattern.left, origin, scope);
46
+ }
47
+ else if (pattern.type === 'RestElement') {
48
+ bindPattern(pattern.argument, origin, scope);
49
+ }
50
+ else if (pattern.type === 'ArrayPattern') {
51
+ for (const item of pattern.elements)
52
+ if (item)
53
+ bindPattern(item, false, scope);
54
+ }
55
+ else if (pattern.type === 'ObjectPattern') {
56
+ const hasChildren = pattern.properties.some((property) => property.type === 'Property' && propertyName(property.key, property.computed) === 'children');
57
+ for (const property of pattern.properties) {
58
+ if (property.type === 'RestElement') {
59
+ bindPattern(property.argument, origin === 'props' && !hasChildren ? 'props' : false, scope);
60
+ }
61
+ else {
62
+ const childOrigin = origin === 'props' && propertyName(property.key, property.computed) === 'children'
63
+ ? 'children'
64
+ : false;
65
+ bindPattern(property.value, childOrigin, scope);
66
+ }
67
+ }
68
+ }
69
+ };
70
+ const bindingOrigin = (name, scopes) => {
71
+ var _a;
72
+ for (let index = scopes.length - 1; index >= 0; index--) {
73
+ const scope = scopes[index];
74
+ if (scope === null || scope === void 0 ? void 0 : scope.has(name))
75
+ return (_a = scope.get(name)) !== null && _a !== void 0 ? _a : false;
76
+ }
77
+ return false;
78
+ };
79
+ const expressionOrigin = (node, scopes) => {
80
+ if ((node === null || node === void 0 ? void 0 : node.type) === 'Identifier')
81
+ return bindingOrigin(node.name, scopes);
82
+ if ((node === null || node === void 0 ? void 0 : node.type) === 'ChainExpression')
83
+ return expressionOrigin(node.expression, scopes);
84
+ if ((node === null || node === void 0 ? void 0 : node.type) === 'MemberExpression' &&
85
+ expressionOrigin(node.object, scopes) === 'props' &&
86
+ propertyName(node.property, node.computed) === 'children') {
87
+ return 'children';
88
+ }
89
+ return false;
90
+ };
91
+ const blockScope = (node) => {
92
+ const scope = new Map();
93
+ for (const statement of node.body) {
94
+ if (statement.type === 'VariableDeclaration') {
95
+ for (const declaration of statement.declarations)
96
+ bindPattern(declaration.id, false, scope);
97
+ }
98
+ else if ((statement.type === 'FunctionDeclaration' || statement.type === 'ClassDeclaration') &&
99
+ statement.id) {
100
+ scope.set(statement.id.name, false);
101
+ }
102
+ }
103
+ return scope;
104
+ };
105
+ const forwardsSpreadChildren = (element, attribute) => {
106
+ if (element.children.some((child) => child.type !== 'JSXText' || child.value.trim().length > 0)) {
107
+ return false;
108
+ }
109
+ const attributes = element.openingElement.attributes;
110
+ const index = attributes.indexOf(attribute);
111
+ return !attributes
112
+ .slice(index + 1)
113
+ .some((later) => later.type === 'JSXAttribute' && later.name.name === 'children');
114
+ };
115
+ const placementsIn = (component) => {
116
+ const scopes = [];
117
+ const open = [];
118
+ const elements = [];
119
+ const rendering = [];
120
+ const found = [];
121
+ walk(component, {
122
+ enter(node, parent, key) {
123
+ if (isFunction(node)) {
124
+ if (node !== component && open.length === 0) {
125
+ this.skip();
126
+ return;
127
+ }
128
+ const scope = new Map();
129
+ for (const [index, parameter] of node.params.entries()) {
130
+ bindPattern(parameter, node === component && index === 0 ? 'props' : false, scope);
131
+ }
132
+ scopes.push(scope);
133
+ }
134
+ else if (node.type === 'BlockStatement') {
135
+ scopes.push(blockScope(node));
136
+ }
137
+ else if (node.type === 'CatchClause') {
138
+ const scope = new Map();
139
+ if (node.param)
140
+ bindPattern(node.param, false, scope);
141
+ scopes.push(scope);
142
+ }
143
+ else if (node.type === 'VariableDeclarator') {
144
+ const scope = scopes.at(-1);
145
+ if (scope)
146
+ bindPattern(node.id, expressionOrigin(node.init, scopes), scope);
147
+ }
148
+ else if (node.type === 'JSXElement') {
149
+ open.push(jsxNameOf(node.openingElement.name));
150
+ elements.push(node);
151
+ }
152
+ else if (node.type === 'JSXFragment') {
153
+ open.push(null);
154
+ elements.push(null);
155
+ }
156
+ else if (node.type === 'JSXExpressionContainer') {
157
+ rendering.push((parent === null || parent === void 0 ? void 0 : parent.type) === 'JSXElement' ||
158
+ (parent === null || parent === void 0 ? void 0 : parent.type) === 'JSXFragment' ||
159
+ ((parent === null || parent === void 0 ? void 0 : parent.type) === 'JSXAttribute' && parent.name.name === 'children'));
160
+ }
161
+ else if (node.type === 'JSXSpreadAttribute') {
162
+ const element = elements.at(-1);
163
+ if (element &&
164
+ expressionOrigin(node.argument, scopes) === 'props' &&
165
+ forwardsSpreadChildren(element, node)) {
166
+ found.push([...open]);
167
+ }
168
+ }
169
+ else if (rendering.at(-1) &&
170
+ open.length > 0 &&
171
+ expressionOrigin(node, scopes) === 'children' &&
172
+ !((parent === null || parent === void 0 ? void 0 : parent.type) === 'Property' && key === 'key' && !parent.computed) &&
173
+ !((parent === null || parent === void 0 ? void 0 : parent.type) === 'MemberExpression' && key === 'property' && !parent.computed)) {
174
+ found.push([...open]);
175
+ }
176
+ },
177
+ leave(node) {
178
+ if (isFunction(node) || node.type === 'BlockStatement' || node.type === 'CatchClause') {
179
+ scopes.pop();
180
+ }
181
+ else if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
182
+ open.pop();
183
+ elements.pop();
184
+ }
185
+ else if (node.type === 'JSXExpressionContainer') {
186
+ rendering.pop();
187
+ }
188
+ },
189
+ });
190
+ return found;
191
+ };
192
+ export const childrenPlacements = (statements) => {
193
+ const placements = new Map();
194
+ for (const { statement } of statements) {
195
+ if (statement.type !== 'ExportNamedDeclaration')
196
+ continue;
197
+ const declaration = statement.declaration;
198
+ if ((declaration === null || declaration === void 0 ? void 0 : declaration.type) === 'FunctionDeclaration' && declaration.id) {
199
+ const found = placementsIn(declaration);
200
+ if (found.length > 0)
201
+ placements.set(declaration.id.name, found);
202
+ }
203
+ else if ((declaration === null || declaration === void 0 ? void 0 : declaration.type) === 'VariableDeclaration') {
204
+ for (const declarator of declaration.declarations) {
205
+ if (declarator.id.type !== 'Identifier' ||
206
+ !declarator.init ||
207
+ !isFunction(declarator.init)) {
208
+ continue;
209
+ }
210
+ const found = placementsIn(declarator.init);
211
+ if (found.length > 0)
212
+ placements.set(declarator.id.name, found);
213
+ }
214
+ }
215
+ }
216
+ return placements;
217
+ };
218
+ export const blockerWithin = (name, blocker, placements, seen = new Set()) => {
219
+ var _a;
220
+ if (name === null || isIntrinsic(name))
221
+ return blockerInside(name, blocker);
222
+ const inner = new Set([...seen, name]);
223
+ for (const path of seen.has(name) ? [] : ((_a = placements.get(name)) !== null && _a !== void 0 ? _a : [])) {
224
+ const inside = path.reduce((current, element) => blockerWithin(element, current, placements, inner), blocker);
225
+ if (inside)
226
+ return inside;
227
+ }
228
+ return blocker;
229
+ };
@@ -0,0 +1,28 @@
1
+ import type { Root } from 'mdast';
2
+ import type { MdxjsEsm } from 'mdast-util-mdx';
3
+ import { type ImportSpecifier } from '../../types/mdx/snippets/import.js';
4
+ export { isIdentifierName } from '../snippets/importedExportName.js';
5
+ export declare const MINT_ISLAND_COMPONENT = "_MintIsland";
6
+ export type PackageImportOptions = {
7
+ packageNames: ReadonlySet<string>;
8
+ packageSnippets?: ReadonlySet<string>;
9
+ };
10
+ export declare const isDeclaredPackageImport: (source: string, packageNames: ReadonlySet<string>) => boolean;
11
+ export declare const isNonHostBareImport: (source: string) => boolean;
12
+ export declare const importingFile: (node: MdxjsEsm, pagePath: string) => string;
13
+ export declare const isPackageSnippetPath: (path: string) => boolean;
14
+ export declare const isPackageSnippet: (filename: string, importSources: string[]) => boolean;
15
+ export declare const findPackageSnippets: (snippets: {
16
+ filename: string;
17
+ tree: Root;
18
+ }[]) => Set<string>;
19
+ export type ImportConflict = {
20
+ local: string;
21
+ first: string;
22
+ second: string;
23
+ };
24
+ export declare const mergeImports: (imports: [string, ImportSpecifier[]][]) => {
25
+ imports: [string, ImportSpecifier[]][];
26
+ conflicts: ImportConflict[];
27
+ };
28
+ export declare const createImportNode: (imports: [string, ImportSpecifier[]][]) => MdxjsEsm;
@@ -0,0 +1,155 @@
1
+ import { jsx, toJs } from 'estree-util-to-js';
2
+ import { MdxImportSpecifier } from '../../types/mdx/snippets/import.js';
3
+ import { injectedSnippetFile } from '../plugins/remark/remarkMdxInjectSnippets.js';
4
+ import { isIdentifierName } from '../snippets/importedExportName.js';
5
+ import { getSpecifierPackageName, isBareSpecifier, isHostProvidedSpecifier, } from '../snippets/isBareSpecifier.js';
6
+ import { isJsxOrTsx } from '../snippets/isJsxOrTsx.js';
7
+ import { resolveImportPath } from '../snippets/resolveImportPath.js';
8
+ import { isMdxJsEsm } from '../utils.js';
9
+ export { isIdentifierName } from '../snippets/importedExportName.js';
10
+ export const MINT_ISLAND_COMPONENT = '_MintIsland';
11
+ const identifier = (name) => ({ type: 'Identifier', name });
12
+ const printedExportName = (name) => identifier(isIdentifierName(name) ? name : JSON.stringify(name));
13
+ const exportName = (name) => isIdentifierName(name)
14
+ ? identifier(name)
15
+ : { type: 'Literal', value: name };
16
+ export const isDeclaredPackageImport = (source, packageNames) => {
17
+ if (!isBareSpecifier(source) || isHostProvidedSpecifier(source))
18
+ return false;
19
+ const packageName = getSpecifierPackageName(source);
20
+ return packageName !== undefined && packageNames.has(packageName);
21
+ };
22
+ export const isNonHostBareImport = (source) => isBareSpecifier(source) && !isHostProvidedSpecifier(source);
23
+ export const importingFile = (node, pagePath) => {
24
+ const file = injectedSnippetFile(node);
25
+ return file === undefined ? pagePath : `/_snippets/${file}`;
26
+ };
27
+ export const isPackageSnippetPath = (path) => {
28
+ const normalized = path.startsWith('/') ? path : `/${path}`;
29
+ return normalized.startsWith('/snippets/') && isJsxOrTsx(normalized);
30
+ };
31
+ export const isPackageSnippet = (filename, importSources) => isPackageSnippetPath(filename) && importSources.some(isNonHostBareImport);
32
+ const moduleSources = (tree) => {
33
+ var _a, _b, _c, _d;
34
+ const sources = [];
35
+ for (const node of tree.children) {
36
+ if (!isMdxJsEsm(node))
37
+ continue;
38
+ for (const statement of (_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 : []) {
39
+ if (statement.type !== 'ImportDeclaration' &&
40
+ statement.type !== 'ExportNamedDeclaration' &&
41
+ statement.type !== 'ExportAllDeclaration') {
42
+ continue;
43
+ }
44
+ const source = (_d = statement.source) === null || _d === void 0 ? void 0 : _d.value;
45
+ if (typeof source === 'string')
46
+ sources.push(source);
47
+ }
48
+ }
49
+ return sources;
50
+ };
51
+ export const findPackageSnippets = (snippets) => {
52
+ const candidates = new Map();
53
+ for (const { filename, tree } of snippets) {
54
+ const path = filename.startsWith('/') ? filename : `/${filename}`;
55
+ if (isPackageSnippetPath(path))
56
+ candidates.set(path, moduleSources(tree));
57
+ }
58
+ const found = new Set();
59
+ let grew = true;
60
+ while (grew) {
61
+ grew = false;
62
+ for (const [path, sources] of candidates) {
63
+ if (found.has(path))
64
+ continue;
65
+ const usesPackages = sources.some((source) => { var _a; return isNonHostBareImport(source) || found.has((_a = resolveImportPath(source, path)) !== null && _a !== void 0 ? _a : ''); });
66
+ if (usesPackages) {
67
+ found.add(path);
68
+ grew = true;
69
+ }
70
+ }
71
+ }
72
+ return found;
73
+ };
74
+ const importedBinding = (specifier) => {
75
+ if (specifier.type === MdxImportSpecifier.ImportNamespaceSpecifier)
76
+ return null;
77
+ if (specifier.type === MdxImportSpecifier.ImportDefaultSpecifier)
78
+ return 'default';
79
+ return specifier.name;
80
+ };
81
+ const describeBinding = (source, imported) => {
82
+ const name = imported === null ? '*' : isIdentifierName(imported) ? imported : JSON.stringify(imported);
83
+ return `${name} from "${source}"`;
84
+ };
85
+ export const mergeImports = (imports) => {
86
+ var _a, _b;
87
+ const merged = new Map();
88
+ const bound = new Map();
89
+ const seen = new Set();
90
+ const conflicts = [];
91
+ for (const [source, specifiers] of imports) {
92
+ if (specifiers.length === 0 && !merged.has(source))
93
+ merged.set(source, []);
94
+ for (const specifier of specifiers) {
95
+ const local = (_a = specifier.renamedName) !== null && _a !== void 0 ? _a : specifier.name;
96
+ const imported = importedBinding(specifier);
97
+ const key = JSON.stringify([local, source, imported]);
98
+ if (seen.has(key))
99
+ continue;
100
+ seen.add(key);
101
+ const binding = describeBinding(source, imported);
102
+ const first = bound.get(local);
103
+ if (first === undefined)
104
+ bound.set(local, binding);
105
+ else
106
+ conflicts.push({ local, first, second: binding });
107
+ merged.set(source, [...((_b = merged.get(source)) !== null && _b !== void 0 ? _b : []), specifier]);
108
+ }
109
+ }
110
+ return { imports: [...merged], conflicts };
111
+ };
112
+ const importDeclarations = (source, specifiers, imported) => {
113
+ var _a;
114
+ const declaration = (entries) => ({
115
+ type: 'ImportDeclaration',
116
+ specifiers: entries,
117
+ source: { type: 'Literal', value: source },
118
+ });
119
+ const main = [];
120
+ const separate = [];
121
+ for (const specifier of specifiers) {
122
+ switch (specifier.type) {
123
+ case MdxImportSpecifier.ImportDefaultSpecifier:
124
+ if (main.some((entry) => entry.type === 'ImportDefaultSpecifier')) {
125
+ separate.push(declaration([{ type: 'ImportDefaultSpecifier', local: identifier(specifier.name) }]));
126
+ }
127
+ else {
128
+ main.unshift({ type: 'ImportDefaultSpecifier', local: identifier(specifier.name) });
129
+ }
130
+ break;
131
+ case MdxImportSpecifier.ImportNamespaceSpecifier:
132
+ separate.push(declaration([{ type: 'ImportNamespaceSpecifier', local: identifier(specifier.name) }]));
133
+ break;
134
+ default:
135
+ main.push({
136
+ type: 'ImportSpecifier',
137
+ imported: imported(specifier.name),
138
+ local: identifier((_a = specifier.renamedName) !== null && _a !== void 0 ? _a : specifier.name),
139
+ });
140
+ }
141
+ }
142
+ return [...(main.length > 0 || specifiers.length === 0 ? [declaration(main)] : []), ...separate];
143
+ };
144
+ export const createImportNode = (imports) => {
145
+ const program = (imported) => ({
146
+ type: 'Program',
147
+ sourceType: 'module',
148
+ body: imports.flatMap(([source, specifiers]) => importDeclarations(source, specifiers, imported)),
149
+ });
150
+ return {
151
+ type: 'mdxjsEsm',
152
+ value: toJs(program(printedExportName), { handlers: jsx }).value.trim(),
153
+ data: { estree: program(exportName) },
154
+ };
155
+ };
@@ -0,0 +1,23 @@
1
+ import type { Root } from 'mdast';
2
+ import type { MdxExtracts, MdxIslandExtract } from '../../types/mdx/MdxExtracts.js';
3
+ import type { ExtractPackageIslandsOptions, ExtractPackageIslandsResult, IslandError } from './types.js';
4
+ export type RemarkPackageIslandsOptions = ExtractPackageIslandsOptions & {
5
+ plan?: ExtractPackageIslandsResult;
6
+ islandHtml?: Record<string, MdxIslandExtract>;
7
+ baseUrl?: string;
8
+ mdxExtracts?: MdxExtracts;
9
+ };
10
+ export declare class PackageIslandsError extends Error {
11
+ readonly pagePath: string;
12
+ readonly errors: IslandError[];
13
+ constructor(pagePath: string, errors: IslandError[]);
14
+ }
15
+ export declare const islandPrefixFor: (id: string) => string;
16
+ export declare const preprocessPageContent: (content: string, variables?: Record<string, string>) => string;
17
+ export declare const parsePackageIslands: (processedContent: string, options: ExtractPackageIslandsOptions, snippetTreeMap?: Record<string, Root>) => ExtractPackageIslandsResult;
18
+ export declare const getPackageIslandsForContent: ({ content, variables, snippetTreeMap, ...options }: ExtractPackageIslandsOptions & {
19
+ content: string;
20
+ variables?: Record<string, string>;
21
+ snippetTreeMap?: Record<string, Root>;
22
+ }) => ExtractPackageIslandsResult;
23
+ export declare const remarkPackageIslands: (options: RemarkPackageIslandsOptions) => (tree: Root) => void;
@@ -0,0 +1,177 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { jsx, toJs } from 'estree-util-to-js';
13
+ import { SKIP, visit } from 'unist-util-visit';
14
+ import { VFile } from 'vfile';
15
+ import { findSnippetCycle, remarkMdxInjectSnippets, } from '../plugins/remark/remarkMdxInjectSnippets.js';
16
+ import { preprocessCustomHeadingIds } from '../preprocessCustomHeadingIds.js';
17
+ import { preprocessMdxFragmentCode } from '../preprocessMdxFragmentCode.js';
18
+ import { coreRemark } from '../remark.js';
19
+ import { replaceVariables } from '../replaceVariables.js';
20
+ import { resolveImportPath } from '../snippets/resolveImportPath.js';
21
+ import { extractPackageIslands, soleParagraphChild } from './extractPackageIslands.js';
22
+ import { MINT_ISLAND_COMPONENT, importingFile, isDeclaredPackageImport, isPackageSnippetPath, } from './packageImports.js';
23
+ export class PackageIslandsError extends Error {
24
+ constructor(pagePath, errors) {
25
+ super([
26
+ `Package components in ${pagePath} can't be built:`,
27
+ ...errors.map(({ line, message }) => ` ${line === undefined ? '' : `line ${line}: `}${message}`),
28
+ ].join('\n'));
29
+ this.pagePath = pagePath;
30
+ this.errors = errors;
31
+ this.name = 'PackageIslandsError';
32
+ }
33
+ }
34
+ export const islandPrefixFor = (id) => `mi-${id}-`;
35
+ export const preprocessPageContent = (content, variables) => preprocessCustomHeadingIds(preprocessMdxFragmentCode(replaceVariables(content, variables)));
36
+ export const parsePackageIslands = (processedContent, options, snippetTreeMap) => {
37
+ const tree = coreRemark().parse(processedContent);
38
+ if (snippetTreeMap) {
39
+ const cycle = findSnippetCycle(tree, snippetTreeMap);
40
+ if (cycle) {
41
+ const message = `<Snippet file="${cycle[0]}"> includes itself (${cycle.join(' -> ')}).`;
42
+ return { islands: [], roots: [], errors: [{ message }] };
43
+ }
44
+ remarkMdxInjectSnippets(snippetTreeMap)(tree, new VFile());
45
+ }
46
+ return extractPackageIslands(tree, options);
47
+ };
48
+ export const getPackageIslandsForContent = (_a) => {
49
+ var { content, variables, snippetTreeMap } = _a, options = __rest(_a, ["content", "variables", "snippetTreeMap"]);
50
+ return parsePackageIslands(preprocessPageContent(content, variables), options, snippetTreeMap);
51
+ };
52
+ const placeholder = (type, id) => ({
53
+ type,
54
+ name: MINT_ISLAND_COMPONENT,
55
+ attributes: [{ type: 'mdxJsxAttribute', name: 'id', value: id }],
56
+ children: [],
57
+ });
58
+ const isIslandImport = (source, importer, options) => {
59
+ if (isDeclaredPackageImport(source, options.packageNames))
60
+ return true;
61
+ const resolved = resolveImportPath(source, importer);
62
+ if (resolved === null)
63
+ return false;
64
+ return options.packageSnippets
65
+ ? options.packageSnippets.has(resolved)
66
+ : isPackageSnippetPath(resolved);
67
+ };
68
+ const removeIslandImports = (tree, options) => {
69
+ const emptied = new Set();
70
+ visit(tree, 'mdxjsEsm', (node) => {
71
+ var _a;
72
+ const program = (_a = node.data) === null || _a === void 0 ? void 0 : _a.estree;
73
+ if (!program)
74
+ return;
75
+ const importer = importingFile(node, options.pagePath);
76
+ const body = program.body.filter((statement) => statement.type !== 'ImportDeclaration' ||
77
+ typeof statement.source.value !== 'string' ||
78
+ !isIslandImport(statement.source.value, importer, options));
79
+ if (body.length === program.body.length)
80
+ return;
81
+ if (body.length === 0) {
82
+ emptied.add(node);
83
+ return;
84
+ }
85
+ program.body = body;
86
+ node.value = toJs(program, { handlers: jsx }).value;
87
+ });
88
+ if (emptied.size === 0)
89
+ return;
90
+ visit(tree, (node, index, parent) => {
91
+ if (parent && index !== undefined && emptied.has(node)) {
92
+ parent.children.splice(index, 1);
93
+ return [SKIP, index];
94
+ }
95
+ });
96
+ };
97
+ export const remarkPackageIslands = (options) => (tree) => {
98
+ var _a, _b, _c;
99
+ const own = extractPackageIslands(tree, options);
100
+ const plan = (_a = options.plan) !== null && _a !== void 0 ? _a : own;
101
+ if (plan.errors.length > 0)
102
+ throw new PackageIslandsError(options.pagePath, plan.errors);
103
+ if (own.errors.length > 0)
104
+ throw new PackageIslandsError(options.pagePath, own.errors);
105
+ if (own.roots.length !== plan.roots.length ||
106
+ own.roots.some((root, index) => { var _a; return root.name !== ((_a = plan.roots[index]) === null || _a === void 0 ? void 0 : _a.name); })) {
107
+ throw new PackageIslandsError(options.pagePath, [
108
+ { message: 'Package components found at compile time differ from the extracted islands.' },
109
+ ]);
110
+ }
111
+ if (own.roots.length === 0)
112
+ return;
113
+ const ids = new Map();
114
+ const buildErrors = [];
115
+ own.roots.forEach((root, index) => {
116
+ var _a, _b, _c, _d, _e;
117
+ const id = (_b = (_a = plan.roots[index]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : root.id;
118
+ ids.set(root.node, id);
119
+ if (!options.islandHtml)
120
+ return;
121
+ const entry = options.islandHtml[id];
122
+ if (entry === undefined) {
123
+ buildErrors.push({
124
+ line: (_c = root.node.position) === null || _c === void 0 ? void 0 : _c.start.line,
125
+ message: `<${root.name}> compiles to island ${id}, which isn't in the package build.`,
126
+ });
127
+ return;
128
+ }
129
+ if (entry.prefix !== islandPrefixFor(id)) {
130
+ buildErrors.push({
131
+ line: (_d = root.node.position) === null || _d === void 0 ? void 0 : _d.start.line,
132
+ message: `Island ${id} must use the prefix ${islandPrefixFor(id)}.`,
133
+ });
134
+ }
135
+ if (entry.html !== undefined && typeof entry.html !== 'string') {
136
+ buildErrors.push({
137
+ line: (_e = root.node.position) === null || _e === void 0 ? void 0 : _e.start.line,
138
+ message: `Island ${id} HTML must be a string.`,
139
+ });
140
+ }
141
+ });
142
+ if (buildErrors.length > 0)
143
+ throw new PackageIslandsError(options.pagePath, buildErrors);
144
+ visit(tree, (node, index, parent) => {
145
+ if (!parent || index === undefined)
146
+ return;
147
+ if (node.type === 'paragraph') {
148
+ const only = soleParagraphChild(node);
149
+ const id = only ? ids.get(only) : undefined;
150
+ if (id === undefined)
151
+ return;
152
+ parent.children[index] = placeholder('mdxJsxFlowElement', id);
153
+ return SKIP;
154
+ }
155
+ const id = ids.get(node);
156
+ if (id === undefined)
157
+ return;
158
+ parent.children[index] =
159
+ node.type === 'mdxJsxTextElement'
160
+ ? placeholder('mdxJsxTextElement', id)
161
+ : placeholder('mdxJsxFlowElement', id);
162
+ return SKIP;
163
+ });
164
+ removeIslandImports(tree, options);
165
+ if (!options.mdxExtracts)
166
+ return;
167
+ const islands = {};
168
+ for (const id of ids.values()) {
169
+ if (id in islands)
170
+ continue;
171
+ const entry = (_b = options.islandHtml) === null || _b === void 0 ? void 0 : _b[id];
172
+ islands[id] = Object.assign(Object.assign({}, ((entry === null || entry === void 0 ? void 0 : entry.html) !== undefined && { html: entry.html })), { prefix: (_c = entry === null || entry === void 0 ? void 0 : entry.prefix) !== null && _c !== void 0 ? _c : islandPrefixFor(id) });
173
+ }
174
+ options.mdxExtracts.islands = islands;
175
+ if (options.baseUrl !== undefined)
176
+ options.mdxExtracts.packageBaseUrl = options.baseUrl;
177
+ };
@@ -0,0 +1 @@
1
+ export declare const sha256Hex: (input: string) => string;