fumadocs-core 9.1.0 → 10.0.1

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.
@@ -21,12 +21,12 @@ function remarkHeading() {
21
21
  const toc = [];
22
22
  slugger.reset();
23
23
  visit(node, ["heading"], (heading) => {
24
- var _a;
24
+ var _a, _b;
25
25
  heading.data || (heading.data = {});
26
26
  (_a = heading.data).hProperties || (_a.hProperties = {});
27
27
  const text = flattenNode(heading);
28
28
  const properties = heading.data.hProperties;
29
- const id = slugger.slug(properties.id || text);
29
+ const id = slugger.slug((_b = properties.id) != null ? _b : text);
30
30
  properties.id = id;
31
31
  toc.push({
32
32
  title: text,
@@ -2,7 +2,7 @@ import {
2
2
  flattenNode,
3
3
  remarkHeading,
4
4
  visit
5
- } from "../chunk-GT3Y35O6.js";
5
+ } from "../chunk-M6ZDX3QR.js";
6
6
  import {
7
7
  slash
8
8
  } from "../chunk-UWEEHUJV.js";
@@ -8,6 +8,11 @@ interface UseDocsSearch {
8
8
  keepPreviousData: true;
9
9
  }>;
10
10
  }
11
- declare function useDocsSearch(locale?: string, tag?: string): UseDocsSearch;
11
+ /**
12
+ * @param locale - Filter with locale
13
+ * @param tag - Filter with specific tag
14
+ * @param api - The Search API URL
15
+ */
16
+ declare function useDocsSearch(locale?: string, tag?: string, api?: string): UseDocsSearch;
12
17
 
13
18
  export { useDocsSearch };
@@ -21,14 +21,12 @@ function fetchDocs(api, query, locale, tag) {
21
21
  return yield res.json();
22
22
  });
23
23
  }
24
- function useDocsSearch(locale, tag) {
24
+ function useDocsSearch(locale, tag, api = "/api/search") {
25
25
  const [search, setSearch] = useState("");
26
26
  const debouncedValue = useDebounce(search, 100);
27
27
  const query = useSWR(
28
- ["/api/search", debouncedValue, locale, tag],
29
- (_0) => __async(this, [_0], function* ([api, value]) {
30
- return fetchDocs(api, value, locale, tag);
31
- }),
28
+ [api, debouncedValue, locale, tag],
29
+ (args) => fetchDocs(...args),
32
30
  {
33
31
  keepPreviousData: true
34
32
  }
@@ -5,7 +5,7 @@ import {
5
5
 
6
6
  // src/search/server.ts
7
7
  import { Document } from "flexsearch";
8
- import nextLib from "next/server";
8
+ import { NextResponse } from "next/server";
9
9
  function createSearchAPI(type, options) {
10
10
  if (type === "simple") {
11
11
  return initSearchAPI(options);
@@ -32,7 +32,7 @@ function createI18nSearchAPI(type, options) {
32
32
  if (handler)
33
33
  return handler.GET(request);
34
34
  }
35
- return nextLib.NextResponse.json([]);
35
+ return NextResponse.json([]);
36
36
  }
37
37
  };
38
38
  }
@@ -80,20 +80,20 @@ function initSearchAPI({ indexes, language }) {
80
80
  const { searchParams } = request.nextUrl;
81
81
  const query = searchParams.get("query");
82
82
  if (!query)
83
- return nextLib.NextResponse.json([]);
83
+ return NextResponse.json([]);
84
84
  const results = index.search(query, 5, {
85
85
  enrich: true,
86
86
  suggest: true
87
87
  });
88
88
  if (results.length === 0)
89
- return nextLib.NextResponse.json([]);
89
+ return NextResponse.json([]);
90
90
  const pages = results[0].result.map((page) => ({
91
91
  type: "page",
92
92
  content: page.doc.title,
93
93
  id: page.doc.url,
94
94
  url: page.doc.url
95
95
  }));
96
- return nextLib.NextResponse.json(pages);
96
+ return NextResponse.json(pages);
97
97
  }
98
98
  };
99
99
  }
@@ -158,7 +158,7 @@ function initSearchAPIAdvanced({
158
158
  const query = request.nextUrl.searchParams.get("query");
159
159
  const paramTag = request.nextUrl.searchParams.get("tag");
160
160
  if (!query)
161
- return nextLib.NextResponse.json([]);
161
+ return NextResponse.json([]);
162
162
  const results = index.search(query, 5, {
163
163
  enrich: true,
164
164
  tag: paramTag != null ? paramTag : void 0,
@@ -197,7 +197,7 @@ function initSearchAPIAdvanced({
197
197
  });
198
198
  sortedResult.push(...items);
199
199
  }
200
- return nextLib.NextResponse.json(sortedResult);
200
+ return NextResponse.json(sortedResult);
201
201
  }
202
202
  };
203
203
  }
@@ -49,7 +49,7 @@ function useAlgoliaSearch(index, _a = {}) {
49
49
  var _b = _a, { allowEmpty = true } = _b, options = __objRest(_b, ["allowEmpty"]);
50
50
  const [search, setSearch] = useState("");
51
51
  const query = useSWR(
52
- ["/api/search", search, allowEmpty, options],
52
+ ["algolia-search", search, allowEmpty, options],
53
53
  () => __async(this, null, function* () {
54
54
  if (allowEmpty && search.length === 0)
55
55
  return "empty";
@@ -42,4 +42,4 @@ interface BaseIndex {
42
42
  content: string;
43
43
  }
44
44
 
45
- export { type BaseIndex, setIndexSettings, sync, updateDocuments };
45
+ export { type BaseIndex, type SyncOptions, setIndexSettings, sync, updateDocuments };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  remarkHeading
3
- } from "../chunk-GT3Y35O6.js";
3
+ } from "../chunk-M6ZDX3QR.js";
4
4
  import {
5
5
  __async
6
6
  } from "../chunk-WEAGW6MQ.js";
@@ -1,6 +1,25 @@
1
1
  import { ReactElement } from 'react';
2
2
  import { R as Root } from '../page-tree-mLHMYx2B.js';
3
3
 
4
+ interface FileInfo {
5
+ locale?: string;
6
+ /**
7
+ * Original path of file
8
+ */
9
+ path: string;
10
+ /**
11
+ * File path without extension
12
+ */
13
+ flattenedPath: string;
14
+ /**
15
+ * File name without locale and extension
16
+ */
17
+ name: string;
18
+ dirname: string;
19
+ }
20
+ declare function parseFilePath(path: string): FileInfo;
21
+ declare function parseFolderPath(path: string): FileInfo;
22
+
4
23
  interface LoadOptions {
5
24
  files: VirtualFile[];
6
25
  transformers?: Transformer[];
@@ -13,13 +32,11 @@ interface VirtualFile {
13
32
  type: 'page' | 'meta';
14
33
  data: unknown;
15
34
  }
16
- interface LoadResult {
35
+ type Transformer = (context: {
17
36
  storage: Storage;
18
37
  getSlugs: (info: FileInfo) => string[];
19
38
  getUrl: (slugs: string[], locale?: string) => string;
20
- data: Record<string, unknown>;
21
- }
22
- declare function load(options: LoadOptions): LoadResult;
39
+ }) => void;
23
40
 
24
41
  interface LoaderConfig {
25
42
  source: SourceConfig;
@@ -54,7 +71,7 @@ interface Source<Config extends SourceConfig> {
54
71
  }
55
72
  interface LoaderOutput<Config extends LoaderConfig> {
56
73
  pageTree: Config['i18n'] extends true ? Record<string, Root> : Root;
57
- files: Node<Config['source']['metaData'], Config['source']['pageData']>[];
74
+ files: (Meta<Config['source']['metaData']> | Page<Config['source']['pageData']>)[];
58
75
  /**
59
76
  * Get list of pages from language, empty if language hasn't specified
60
77
  *
@@ -66,28 +83,14 @@ interface LoaderOutput<Config extends LoaderConfig> {
66
83
  */
67
84
  getPage: (slugs: string[] | undefined, language?: string) => Page<Config['source']['pageData']> | undefined;
68
85
  }
86
+ declare function createGetUrl(baseUrl: string): (slugs: string[], locale?: string) => string;
87
+ declare function getSlugs(info: FileInfo): string[];
69
88
  type InferSourceConfig<T> = T extends Source<infer Config> ? Config : never;
70
89
  declare function loader<Options extends LoaderOptions>(options: Options): LoaderOutput<{
71
90
  source: InferSourceConfig<Options['source']>;
72
91
  i18n: Options['languages'] extends string[] ? true : false;
73
92
  }>;
74
93
 
75
- interface FileInfo {
76
- locale?: string;
77
- /**
78
- * Original path of file
79
- */
80
- path: string;
81
- /**
82
- * File path without extension
83
- */
84
- flattenedPath: string;
85
- /**
86
- * File name without locale and extension
87
- */
88
- name: string;
89
- dirname: string;
90
- }
91
94
  interface MetaData {
92
95
  icon?: string;
93
96
  title?: string;
@@ -101,7 +104,6 @@ interface PageData {
101
104
  }
102
105
  type InferPageType<Utils extends LoaderOutput<any>> = Utils extends LoaderOutput<infer Config> ? Page<Config['source']['pageData']> : never;
103
106
  type InferMetaType<Utils extends LoaderOutput<any>> = Utils extends LoaderOutput<infer Config> ? Meta<Config['source']['metaData']> : never;
104
- type Transformer = (context: LoadResult) => void;
105
107
 
106
108
  interface Meta<Data extends MetaData = MetaData> {
107
109
  type: 'meta';
@@ -118,9 +120,8 @@ interface Page<Data extends PageData = PageData> {
118
120
  interface Folder<MD extends MetaData = MetaData, PD extends PageData = PageData> {
119
121
  type: 'folder';
120
122
  file: FileInfo;
121
- children: Node<MD, PD>[];
123
+ children: (Meta<MD> | Page<PD> | Folder<MD, PD>)[];
122
124
  }
123
- type Node<MD extends MetaData = MetaData, PD extends PageData = PageData> = Meta<MD> | Page<PD> | Folder<MD, PD>;
124
125
  /**
125
126
  * A virtual file system that solves inconsistent behaviours
126
127
  *
@@ -152,12 +153,11 @@ declare class Storage {
152
153
 
153
154
  type fileSystem_Folder<MD extends MetaData = MetaData, PD extends PageData = PageData> = Folder<MD, PD>;
154
155
  type fileSystem_Meta<Data extends MetaData = MetaData> = Meta<Data>;
155
- type fileSystem_Node<MD extends MetaData = MetaData, PD extends PageData = PageData> = Node<MD, PD>;
156
156
  type fileSystem_Page<Data extends PageData = PageData> = Page<Data>;
157
157
  type fileSystem_Storage = Storage;
158
158
  declare const fileSystem_Storage: typeof Storage;
159
159
  declare namespace fileSystem {
160
- export { type fileSystem_Folder as Folder, type fileSystem_Meta as Meta, type fileSystem_Node as Node, type fileSystem_Page as Page, fileSystem_Storage as Storage };
160
+ export { type fileSystem_Folder as Folder, type fileSystem_Meta as Meta, type fileSystem_Page as Page, fileSystem_Storage as Storage };
161
161
  }
162
162
 
163
163
  interface BuildPageTreeOptionsWithI18n {
@@ -176,4 +176,4 @@ interface CreatePageTreeBuilderOptions {
176
176
  }
177
177
  declare function createPageTreeBuilder({ storage, resolveIcon, }: CreatePageTreeBuilderOptions): PageTreeBuilder;
178
178
 
179
- export { type BuildPageTreeOptionsWithI18n, type CreatePageTreeBuilderOptions, type FileInfo, fileSystem as FileSystem, type InferMetaType, type InferPageType, type LoadOptions, type LoadResult, type LoaderOptions, type LoaderOutput, type MetaData, type PageData, type PageTreeBuilder, type Source, type Transformer, type VirtualFile, createPageTreeBuilder, load, loader };
179
+ export { type BuildPageTreeOptionsWithI18n, type CreatePageTreeBuilderOptions, type FileInfo, fileSystem as FileSystem, type InferMetaType, type InferPageType, type LoadOptions, type LoaderConfig, type LoaderOptions, type LoaderOutput, type MetaData, type PageData, type PageTreeBuilder, type Source, type SourceConfig, type Transformer, type VirtualFile, createGetUrl, createPageTreeBuilder, getSlugs, loader, parseFilePath, parseFolderPath };
@@ -49,6 +49,7 @@ function joinPaths(paths, slashMode = "none") {
49
49
  }
50
50
 
51
51
  // src/source/page-tree-builder.ts
52
+ var external = new RegExp("\\[(?<text>.+)\\]\\((?<url>.+)\\)");
52
53
  var separator = new RegExp("---(?<name>.*?)---");
53
54
  var rest = "...";
54
55
  var extractor = new RegExp("\\.\\.\\.(?<name>.+)");
@@ -95,6 +96,17 @@ function resolveFolderItem(folder, item, ctx, addedNodePaths) {
95
96
  }
96
97
  ];
97
98
  }
99
+ const externalResult = external.exec(item);
100
+ if (externalResult == null ? void 0 : externalResult.groups) {
101
+ return [
102
+ {
103
+ type: "page",
104
+ name: externalResult.groups.text,
105
+ url: externalResult.groups.url,
106
+ external: true
107
+ }
108
+ ];
109
+ }
98
110
  const extractResult = extractor.exec(item);
99
111
  const path2 = joinPaths([
100
112
  folder.file.path,
@@ -106,7 +118,7 @@ function resolveFolderItem(folder, item, ctx, addedNodePaths) {
106
118
  addedNodePaths.add(itemNode.file.path);
107
119
  if (itemNode.type === "folder") {
108
120
  const node = buildFolderNode(itemNode, false, ctx);
109
- return (extractResult == null ? void 0 : extractResult.groups) ? node.children : [node];
121
+ return extractResult ? node.children : [node];
110
122
  }
111
123
  return [buildFileNode(itemNode, ctx)];
112
124
  }
@@ -139,7 +151,7 @@ function buildFolderNode(folder, defaultIsRoot, ctx) {
139
151
  });
140
152
  children = nodes != null ? nodes : restNodes;
141
153
  }
142
- return {
154
+ return removeUndefined({
143
155
  type: "folder",
144
156
  name: (_e = (_d = meta == null ? void 0 : meta.title) != null ? _d : index == null ? void 0 : index.name) != null ? _e : pathToName(folder.file.name),
145
157
  icon: ctx.resolveIcon(meta == null ? void 0 : meta.icon),
@@ -147,7 +159,7 @@ function buildFolderNode(folder, defaultIsRoot, ctx) {
147
159
  defaultOpen: meta == null ? void 0 : meta.defaultOpen,
148
160
  index,
149
161
  children
150
- };
162
+ });
151
163
  }
152
164
  function buildFileNode(page, ctx) {
153
165
  let localePage = page;
@@ -158,12 +170,12 @@ function buildFileNode(page, ctx) {
158
170
  if (result)
159
171
  localePage = result;
160
172
  }
161
- return {
173
+ return removeUndefined({
162
174
  type: "page",
163
175
  name: localePage.data.title,
164
176
  icon: ctx.resolveIcon(localePage.data.icon),
165
177
  url: localePage.url
166
- };
178
+ });
167
179
  }
168
180
  function build(ctx) {
169
181
  const root = ctx.storage.root();
@@ -205,6 +217,14 @@ function createPageTreeBuilder({
205
217
  function pathToName(path2) {
206
218
  return path2.slice(0, 1).toUpperCase() + path2.slice(1);
207
219
  }
220
+ function removeUndefined(value) {
221
+ const obj = value;
222
+ Object.keys(obj).forEach((key) => {
223
+ if (obj[key] === void 0)
224
+ delete obj[key];
225
+ });
226
+ return value;
227
+ }
208
228
 
209
229
  // src/source/load.ts
210
230
  import * as path from "path";
@@ -289,16 +309,14 @@ var Storage = class {
289
309
  function load(options) {
290
310
  const { transformers = [] } = options;
291
311
  const storage = buildStorage(options);
292
- const ctx = {
293
- getSlugs: options.getSlugs,
294
- getUrl: options.getUrl,
295
- storage,
296
- data: {}
297
- };
298
312
  for (const transformer of transformers) {
299
- transformer(ctx);
313
+ transformer({
314
+ storage,
315
+ getUrl: options.getUrl,
316
+ getSlugs: options.getSlugs
317
+ });
300
318
  }
301
- return ctx;
319
+ return { storage };
302
320
  }
303
321
  function buildStorage(options) {
304
322
  const storage = new Storage();
@@ -307,8 +325,7 @@ function buildStorage(options) {
307
325
  path.relative(options.rootDir, path.join("./", path.dirname(file.path))),
308
326
  path.basename(file.path)
309
327
  );
310
- const isRelative = !relativePath.startsWith("..") && !path.isAbsolute(relativePath);
311
- if (!isRelative)
328
+ if (relativePath.startsWith("..") || path.isAbsolute(relativePath))
312
329
  continue;
313
330
  if (file.type === "page") {
314
331
  const parsedPath = parseFilePath(relativePath);
@@ -330,26 +347,20 @@ function buildStorage(options) {
330
347
  return storage;
331
348
  }
332
349
 
333
- // src/source/create.ts
334
- function groupByLanguages(nodes, languages) {
350
+ // src/source/loader.ts
351
+ function groupByLanguages(storage, languages) {
335
352
  var _a, _b, _c;
336
- const pageMap = /* @__PURE__ */ new Map();
337
- for (const node of nodes) {
338
- if (node.type === "page")
339
- pageMap.set(node.file.flattenedPath, node);
340
- }
341
353
  const langMap = /* @__PURE__ */ new Map();
342
354
  langMap.set("", []);
343
- for (const lang of languages) {
344
- langMap.set(lang, []);
345
- }
346
- for (const [key, node] of pageMap) {
347
- if (node.file.locale)
355
+ for (const node of storage.list()) {
356
+ if (node.type !== "page" || node.file.locale)
348
357
  continue;
349
358
  (_a = langMap.get("")) == null ? void 0 : _a.push(node);
350
359
  for (const lang of languages) {
351
- const v = (_b = pageMap.get(`${key}.${lang}`)) != null ? _b : node;
352
- (_c = langMap.get(lang)) == null ? void 0 : _c.push(v);
360
+ const list = (_b = langMap.get(lang)) != null ? _b : [];
361
+ const page = (_c = storage.readPage(`${node.file.flattenedPath}.${lang}`)) != null ? _c : node;
362
+ list.push(page);
363
+ langMap.set(lang, list);
353
364
  }
354
365
  }
355
366
  return langMap;
@@ -386,7 +397,7 @@ function createOutput({
386
397
  getSlugs: slugs,
387
398
  getUrl: url
388
399
  });
389
- const i18nMap = groupByLanguages(result.storage.list(), languages != null ? languages : []);
400
+ const i18nMap = groupByLanguages(result.storage, languages != null ? languages : []);
390
401
  const builder = createPageTreeBuilder({
391
402
  storage: result.storage,
392
403
  resolveIcon: icon
@@ -408,7 +419,10 @@ function createOutput({
408
419
  }
409
420
  export {
410
421
  file_system_exports as FileSystem,
422
+ createGetUrl,
411
423
  createPageTreeBuilder,
412
- load,
413
- loader
424
+ getSlugs,
425
+ loader,
426
+ parseFilePath,
427
+ parseFolderPath
414
428
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-core",
3
- "version": "9.1.0",
3
+ "version": "10.0.1",
4
4
  "description": "The library for building a documentation website in Next.js",
5
5
  "keywords": [
6
6
  "NextJs",
@@ -44,10 +44,6 @@
44
44
  "import": "./dist/link.js",
45
45
  "types": "./dist/link.d.ts"
46
46
  },
47
- "./typescript": {
48
- "import": "./dist/typescript.js",
49
- "types": "./dist/typescript.d.ts"
50
- },
51
47
  "./middleware": {
52
48
  "import": "./dist/middleware.js",
53
49
  "types": "./dist/middleware.d.ts"
@@ -76,9 +72,6 @@
76
72
  "toc": [
77
73
  "./dist/toc.d.ts"
78
74
  ],
79
- "typescript": [
80
- "./dist/typescript.d.ts"
81
- ],
82
75
  "search/client": [
83
76
  "./dist/search/client.d.ts"
84
77
  ],
@@ -115,40 +108,39 @@
115
108
  "dist/*"
116
109
  ],
117
110
  "dependencies": {
118
- "@formatjs/intl-localematcher": "^0.5.0",
119
- "@shikijs/rehype": "1.1.7",
120
- "@shikijs/transformers": "1.1.7",
121
- "flexsearch": "0.7.21",
111
+ "@formatjs/intl-localematcher": "^0.5.4",
112
+ "@shikijs/rehype": "^1.1.7",
113
+ "@shikijs/transformers": "^1.1.7",
114
+ "flexsearch": "^0.7.43",
122
115
  "github-slugger": "^2.0.0",
123
116
  "hast-util-to-estree": "^3.1.0",
124
117
  "negotiator": "^0.6.3",
125
- "react-remove-scroll": "^2.5.6",
118
+ "react-remove-scroll": "^2.5.7",
126
119
  "remark": "^15.0.0",
127
120
  "remark-gfm": "^4.0.0",
128
- "remark-mdx": "^3.0.0",
121
+ "remark-mdx": "^3.0.1",
129
122
  "scroll-into-view-if-needed": "^3.1.0",
130
- "shiki": "1.1.7",
131
- "swr": "^2.2.2",
123
+ "shiki": "^1.1.7",
124
+ "swr": "^2.2.5",
132
125
  "unist-util-visit": "^5.0.0"
133
126
  },
134
127
  "devDependencies": {
135
- "@algolia/client-search": "^4.20.0",
136
- "@next/eslint-plugin-next": "^14.0.0",
128
+ "@algolia/client-search": "^4.22.1",
137
129
  "@types/flexsearch": "0.7.6",
138
130
  "@types/hast": "^3.0.4",
139
131
  "@types/mdast": "^4.0.3",
140
- "@types/negotiator": "^0.6.1",
132
+ "@types/negotiator": "^0.6.3",
141
133
  "@types/node": "18.17.5",
142
- "@types/react": "18.2.0",
143
- "@types/react-dom": "18.2.1",
144
- "algoliasearch": "^4.20.0",
145
- "next": "14.1.0",
134
+ "@types/react": "^18.2.0",
135
+ "@types/react-dom": "^18.2.1",
136
+ "algoliasearch": "^4.22.1",
137
+ "next": "^14.1.2",
146
138
  "unified": "^11.0.4",
147
139
  "eslint-config-custom": "0.0.0",
148
140
  "tsconfig": "0.0.0"
149
141
  },
150
142
  "peerDependencies": {
151
- "next": ">= 13.4",
143
+ "next": ">= 14.1.0",
152
144
  "react": ">= 18",
153
145
  "react-dom": ">= 18"
154
146
  },
@@ -1,37 +0,0 @@
1
- import * as ts from 'typescript';
2
-
3
- interface DocEntry {
4
- name: string;
5
- description: string;
6
- type: string;
7
- default?: string;
8
- }
9
- interface Context {
10
- program: ts.Program;
11
- checker: ts.TypeChecker;
12
- options: Options;
13
- }
14
- interface EntryContext extends Context {
15
- type: ts.Type;
16
- symbol: ts.Symbol;
17
- }
18
- interface Options {
19
- file: string;
20
- name: string;
21
- /**
22
- * Modify output property entry
23
- */
24
- transform?: (this: EntryContext, entry: DocEntry, propertyType: ts.Type, propertySymbol: ts.Symbol) => void;
25
- options?: Partial<{
26
- files: string[];
27
- tsconfigPath: string;
28
- /** A root directory to resolve relative path entries in the config file to. e.g. outDir */
29
- basePath: string;
30
- }>;
31
- }
32
- /**
33
- * Generate documentation for properties in an exported type/interface
34
- */
35
- declare function generateDocumentation(options: Options): DocEntry[];
36
-
37
- export { type DocEntry, type Options, generateDocumentation };
@@ -1,90 +0,0 @@
1
- import {
2
- __spreadProps,
3
- __spreadValues
4
- } from "./chunk-WEAGW6MQ.js";
5
-
6
- // src/typescript.ts
7
- import * as ts from "typescript";
8
- var cache = /* @__PURE__ */ new Map();
9
- function getProgram(options = {}) {
10
- var _a, _b, _c;
11
- const key = JSON.stringify(options);
12
- const cached = cache.get(key);
13
- if (cached)
14
- return cached;
15
- const configFile = ts.readJsonConfigFile(
16
- (_a = options.tsconfigPath) != null ? _a : "./tsconfig.json",
17
- (path) => ts.sys.readFile(path)
18
- );
19
- const parsed = ts.parseJsonSourceFileConfigFileContent(
20
- configFile,
21
- ts.sys,
22
- (_b = options.basePath) != null ? _b : "./"
23
- );
24
- const program = ts.createProgram({
25
- rootNames: (_c = options.files) != null ? _c : parsed.fileNames,
26
- options: __spreadProps(__spreadValues({}, parsed.options), {
27
- incremental: false
28
- })
29
- });
30
- cache.set(key, program);
31
- return program;
32
- }
33
- function getExportedSymbol({
34
- options: { file, name },
35
- checker,
36
- program
37
- }) {
38
- const sourceFile = program.getSourceFile(file);
39
- if (!sourceFile)
40
- return;
41
- const fileSymbol = checker.getSymbolAtLocation(sourceFile);
42
- if (!fileSymbol)
43
- return;
44
- const exports = checker.getExportsOfModule(fileSymbol);
45
- return exports.find((e) => e.getEscapedName().toString() === name);
46
- }
47
- function generateDocumentation(options) {
48
- const program = getProgram(options.options);
49
- const checker = program.getTypeChecker();
50
- const ctx = {
51
- checker,
52
- program,
53
- options
54
- };
55
- const symbol = getExportedSymbol(ctx);
56
- if (!symbol)
57
- return [];
58
- const type = checker.getDeclaredTypeOfSymbol(symbol);
59
- const entryContext = __spreadProps(__spreadValues({}, ctx), {
60
- type,
61
- symbol
62
- });
63
- return type.getProperties().map(getDocEntry.bind(entryContext));
64
- }
65
- function getDocEntry(prop) {
66
- var _a;
67
- const subType = this.checker.getTypeOfSymbol(prop);
68
- const defaultJsDocTag = prop.getJsDocTags().find((info) => ["default", "defaultValue"].includes(info.name));
69
- let typeName = this.checker.typeToString(
70
- subType.getNonNullableType(),
71
- void 0,
72
- ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope
73
- );
74
- if (subType.aliasSymbol && !subType.aliasTypeArguments) {
75
- typeName = subType.aliasSymbol.escapedName.toString();
76
- }
77
- const entry = {
78
- name: prop.getName(),
79
- description: ts.displayPartsToString(
80
- prop.getDocumentationComment(this.checker)
81
- ),
82
- default: (defaultJsDocTag == null ? void 0 : defaultJsDocTag.text) ? ts.displayPartsToString(defaultJsDocTag.text) : void 0,
83
- type: typeName
84
- };
85
- (_a = this.options.transform) == null ? void 0 : _a.call(this, entry, subType, prop);
86
- return entry;
87
- }
88
- export {
89
- generateDocumentation
90
- };