@mintlify/common 1.0.1245 → 1.0.1247

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,3 +1,4 @@
1
1
  export * from './mdx-utils.js';
2
2
  export * from './remark-utils.js';
3
3
  export * from './findExportedNode.js';
4
+ export * from './node-text.js';
@@ -1,3 +1,4 @@
1
1
  export * from './mdx-utils.js';
2
2
  export * from './remark-utils.js';
3
3
  export * from './findExportedNode.js';
4
+ export * from './node-text.js';
@@ -0,0 +1,9 @@
1
+ import type { ElementContent } from 'hast';
2
+ type ValueTree = {
3
+ type: string;
4
+ value?: unknown;
5
+ children?: readonly ValueTree[];
6
+ };
7
+ export declare const mdastToText: (node: ValueTree) => string;
8
+ export declare const hastToText: (node: ElementContent) => string;
9
+ export {};
@@ -0,0 +1,14 @@
1
+ export const mdastToText = (node) => {
2
+ if (typeof node.value === 'string')
3
+ return node.value;
4
+ if (!node.children)
5
+ return '';
6
+ return node.children.map(mdastToText).join('');
7
+ };
8
+ export const hastToText = (node) => {
9
+ if (node.type === 'text')
10
+ return node.value;
11
+ if (node.type === 'element')
12
+ return node.children.map(hastToText).join('');
13
+ return '';
14
+ };
@@ -1,18 +1,7 @@
1
1
  import { u } from 'unist-builder';
2
2
  import { visit } from 'unist-util-visit';
3
+ import { hastToText } from '../../lib/node-text.js';
3
4
  import { createMdxJsxAttribute } from '../../lib/remark-utils.js';
4
- const extractTextContent = (node) => {
5
- let text = '';
6
- for (const child of node.children) {
7
- if (child.type === 'text') {
8
- text += child.value;
9
- }
10
- else if (child.type === 'element') {
11
- text += extractTextContent(child);
12
- }
13
- }
14
- return text;
15
- };
16
5
  const PROCESS_NUMERIC_CELLS_THRESHOLD = 0.8;
17
6
  const UNITS = '(GiB|MiB|KiB|TiB|PiB|EiB|ZiB|' + // Storage (binary prefixes - IEC)
18
7
  'GB|MB|KB|TB|PB|EB|ZB|' + // Storage (decimal prefixes)
@@ -118,7 +107,7 @@ const isPrimarilyNumeric = (text) => {
118
107
  const processTableCells = (node) => {
119
108
  visit(node, 'element', (cellNode) => {
120
109
  if (cellNode.tagName === 'td') {
121
- const textContent = extractTextContent(cellNode);
110
+ const textContent = hastToText(cellNode);
122
111
  if (isPrimarilyNumeric(textContent)) {
123
112
  cellNode.properties['data-numeric'] = 'true';
124
113
  }
@@ -1,21 +1,16 @@
1
1
  import { visit } from 'unist-util-visit';
2
+ import { mdastToText } from '../../lib/node-text.js';
2
3
  const isList = (node) => node.type === 'list';
3
4
  const isTreeItemElement = (node) => node.type === 'mdxJsxFlowElement' &&
4
5
  'name' in node &&
5
6
  (node.name === 'Tree.Folder' ||
6
7
  node.name === 'Tree.File');
7
8
  const isExpression = (node) => node.type === 'mdxFlowExpression' || node.type === 'mdxTextExpression';
8
- const getNodeText = (node) => {
9
- var _a;
10
- if (typeof node.value === 'string')
11
- return node.value;
12
- return ((_a = node.children) !== null && _a !== void 0 ? _a : []).map(getNodeText).join('');
13
- };
14
9
  const describeNode = (node) => {
15
10
  if (node.type === 'mdxJsxFlowElement' && 'name' in node && node.name) {
16
11
  return `<${String(node.name)}>`;
17
12
  }
18
- const text = getNodeText(node).trim().replace(/\s+/g, ' ');
13
+ const text = mdastToText(node).trim().replace(/\s+/g, ' ');
19
14
  const preview = text.length > 40 ? `${text.slice(0, 40)}…` : text;
20
15
  return preview ? `Text ("${preview}")` : `A ${node.type} node`;
21
16
  };
@@ -23,7 +18,7 @@ const parseListItems = (list) => list.children.map((item) => {
23
18
  const nestedList = item.children.find(isList);
24
19
  const name = item.children
25
20
  .filter((child) => !isList(child))
26
- .map(getNodeText)
21
+ .map(mdastToText)
27
22
  .join('')
28
23
  .trim();
29
24
  return {
@@ -1,12 +1,15 @@
1
1
  import type { Root } from 'mdast';
2
2
  import type { FileType, FileWithImports } from '../../types/mdx/index.js';
3
+ import type { ImportMap } from '../../types/mdx/snippets/import.js';
3
4
  import { type PackageImportOptions } from '../islands/packageImports.js';
4
5
  export type ResolveImportsWarning = {
5
6
  type: 'invalid-import-path' | 'missing-file' | 'import-resolution-error' | 'default-import-no-default-export' | 'export-collision' | 'undeclared-package' | 'package-import-conflict';
6
7
  message: string;
7
8
  };
8
9
  export declare const resolveAllImports: (params: {
9
- snippets: FileType[];
10
+ snippets: (FileType & {
11
+ importMap?: ImportMap;
12
+ })[];
10
13
  fileWithImports: FileWithImports;
11
14
  onWarning?: (warning: ResolveImportsWarning) => void;
12
15
  packages?: PackageImportOptions;
@@ -7,6 +7,7 @@ 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 { jsx, toJs } from 'estree-util-to-js';
10
11
  import { remove } from 'unist-util-remove';
11
12
  import { visit } from 'unist-util-visit';
12
13
  import { MdxImportSpecifier } from '../../types/mdx/snippets/import.js';
@@ -35,12 +36,90 @@ const takeImports = (tree) => __awaiter(void 0, void 0, void 0, function* () {
35
36
  remove(tree, { cascade: false }, (node) => carriers.has(node) && !remaining.has(node));
36
37
  return importMap;
37
38
  });
39
+ const splitTopLevelImports = (tree) => __awaiter(void 0, void 0, void 0, function* () {
40
+ const carriers = tree.children.filter(hasImportDeclaration);
41
+ if (carriers.length === 0)
42
+ return { tree: Object.assign(Object.assign({}, tree), { children: [...tree.children] }), importMap: {} };
43
+ const { importMap } = yield findAndRemoveImports({
44
+ type: 'root',
45
+ children: structuredClone(carriers),
46
+ });
47
+ const taken = new Set(carriers);
48
+ return {
49
+ tree: Object.assign(Object.assign({}, tree), { children: tree.children.filter((node) => !taken.has(node)) }),
50
+ importMap,
51
+ };
52
+ });
53
+ const exportName = (name) => 'value' in name ? String(name.value) : name.name;
54
+ const toImportSpecifier = ({ local, exported }) => {
55
+ const imported = exportName(local);
56
+ const bound = exportName(exported);
57
+ if (bound === 'default')
58
+ return undefined;
59
+ if (imported === 'default')
60
+ return { type: MdxImportSpecifier.ImportDefaultSpecifier, name: bound };
61
+ if (imported === bound)
62
+ return { type: MdxImportSpecifier.ImportSpecifier, name: imported };
63
+ return { type: MdxImportSpecifier.RenamedImportSpecifier, name: imported, renamedName: bound };
64
+ };
65
+ const isPackageReExport = (statement, packageNames) => {
66
+ var _a;
67
+ return statement.type === 'ExportNamedDeclaration' &&
68
+ typeof ((_a = statement.source) === null || _a === void 0 ? void 0 : _a.value) === 'string' &&
69
+ isNonHostBareImport(statement.source.value) &&
70
+ isDeclaredPackageImport(statement.source.value, packageNames);
71
+ };
72
+ const takePackageReExports = (tree, packageNames) => {
73
+ const reExports = [];
74
+ tree.children = tree.children.flatMap((node) => {
75
+ var _a;
76
+ if (!isMdxJsEsm(node) || !estreeIsProgram(node))
77
+ return [node];
78
+ const statements = node.data.estree.body;
79
+ const reExported = statements.filter((statement) => isPackageReExport(statement, packageNames));
80
+ for (const statement of reExported) {
81
+ if (statement.type !== 'ExportNamedDeclaration' ||
82
+ typeof ((_a = statement.source) === null || _a === void 0 ? void 0 : _a.value) !== 'string')
83
+ continue;
84
+ reExports.push([
85
+ statement.source.value,
86
+ statement.specifiers.flatMap((s) => { var _a; return (_a = toImportSpecifier(s)) !== null && _a !== void 0 ? _a : []; }),
87
+ ]);
88
+ }
89
+ const rest = statements.filter((statement) => !reExported.includes(statement));
90
+ if (rest.length === statements.length)
91
+ return [node];
92
+ if (rest.length === 0)
93
+ return [];
94
+ const estree = Object.assign(Object.assign({}, node.data.estree), { body: rest });
95
+ return [
96
+ Object.assign(Object.assign({}, node), { value: toJs(estree, { handlers: jsx }).value, data: Object.assign(Object.assign({}, node.data), { estree }) }),
97
+ ];
98
+ });
99
+ return reExports;
100
+ };
101
+ const declaredPackageImports = (snippet, packages) => {
102
+ var _a;
103
+ return Object.entries((_a = snippet.importMap) !== null && _a !== void 0 ? _a : {}).flatMap(([source, specifiers]) => {
104
+ var _a;
105
+ const resolvedPath = resolveImportPath(source, snippet.filename);
106
+ if (resolvedPath == null) {
107
+ return isNonHostBareImport(source) && isDeclaredPackageImport(source, packages.packageNames)
108
+ ? [[source, specifiers]]
109
+ : [];
110
+ }
111
+ return ((_a = packages.packageSnippets) === null || _a === void 0 ? void 0 : _a.has(resolvedPath)) ? [[resolvedPath, specifiers]] : [];
112
+ });
113
+ };
38
114
  export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, function* () {
39
- var _a, _b, _c, _d;
115
+ var _a, _b, _c, _d, _e;
40
116
  const { snippets, fileWithImports, onWarning, packages } = params;
41
117
  let ast = fileWithImports.tree;
118
+ const kept = packages && /\.mdx?$/.test(fileWithImports.filename)
119
+ ? takePackageReExports(ast, packages.packageNames)
120
+ : [];
42
121
  const exportMap = getExportMapFromTree(ast);
43
- const kept = [];
122
+ const strippedSnippets = new Map();
44
123
  for (const source of Object.keys(fileWithImports.importMap)) {
45
124
  const resolvedPath = resolveImportPath(source, fileWithImports.filename);
46
125
  if (packages && resolvedPath == null && isNonHostBareImport(source)) {
@@ -71,8 +150,8 @@ export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, f
71
150
  }
72
151
  continue;
73
152
  }
74
- const importedSnippet = snippets.find((snippet) => snippet.filename === resolvedPath);
75
- if (importedSnippet == undefined) {
153
+ const foundSnippet = snippets.find((snippet) => snippet.filename === resolvedPath);
154
+ if (foundSnippet == undefined) {
76
155
  const message = `Could not find file ${resolvedPath} - imported from ${fileWithImports.filename}`;
77
156
  if (onWarning) {
78
157
  onWarning({ type: 'missing-file', message });
@@ -82,6 +161,16 @@ export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, f
82
161
  }
83
162
  continue;
84
163
  }
164
+ let importedSnippet = (_e = strippedSnippets.get(foundSnippet)) !== null && _e !== void 0 ? _e : foundSnippet;
165
+ if (packages && !strippedSnippets.has(foundSnippet)) {
166
+ const { tree, importMap } = yield splitTopLevelImports(foundSnippet.tree);
167
+ importedSnippet = Object.assign(Object.assign({}, foundSnippet), { tree });
168
+ strippedSnippets.set(foundSnippet, importedSnippet);
169
+ kept.push(...Object.entries(importMap), ...declaredPackageImports(foundSnippet, packages));
170
+ if (/\.mdx?$/.test(foundSnippet.filename)) {
171
+ kept.push(...takePackageReExports(tree, packages.packageNames));
172
+ }
173
+ }
85
174
  const snippetExportMap = getExportMapFromTree(importedSnippet.tree);
86
175
  const specifiers = fileWithImports.importMap[source];
87
176
  if (specifiers == undefined)