diff options
| author | Shinigami <[email protected]> | 2022-08-22 23:38:15 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-08-22 17:38:15 +0200 |
| commit | 0866ee9217e5e0b60e1c5f604e3576fb2604a3d5 (patch) | |
| tree | b54c310380b6892bacf5552d1dde4b325f00b09c /scripts/apidoc/apiDocsWriter.ts | |
| parent | f684a14ddc3729c74f8434db68324269ae9a640f (diff) | |
| download | faker-0866ee9217e5e0b60e1c5f604e3576fb2604a3d5.tar.xz faker-0866ee9217e5e0b60e1c5f604e3576fb2604a3d5.zip | |
docs: searchable api (#1253)
Diffstat (limited to 'scripts/apidoc/apiDocsWriter.ts')
| -rw-r--r-- | scripts/apidoc/apiDocsWriter.ts | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/scripts/apidoc/apiDocsWriter.ts b/scripts/apidoc/apiDocsWriter.ts index 76edc3c3..99193242 100644 --- a/scripts/apidoc/apiDocsWriter.ts +++ b/scripts/apidoc/apiDocsWriter.ts @@ -1,6 +1,11 @@ import { writeFileSync } from 'node:fs'; import { resolve } from 'node:path'; +import type { ProjectReflection } from 'typedoc'; +import { ReflectionKind } from 'typedoc'; import type { Method } from '../../docs/.vitepress/components/api-docs/method'; +import type { APIGroup, APIItem } from '../../docs/api/api-types'; +import { selectDirectMethods } from './directMethods'; +import { extractModuleName, selectApiModules } from './moduleMethods'; import type { PageIndex } from './utils'; import { formatMarkdown, @@ -10,6 +15,11 @@ import { } from './utils'; const pathDocsApiPages = resolve(pathDocsDir, '.vitepress', 'api-pages.ts'); +const pathDocsApiSearchIndex = resolve( + pathDocsDir, + 'api', + 'api-search-index.json' +); const scriptCommand = 'pnpm run generate:api-docs'; @@ -141,3 +151,50 @@ export function writeApiPagesIndex(pages: PageIndex): void { writeFileSync(pathDocsApiPages, apiPagesContent); } + +export function writeApiSearchIndex(project: ProjectReflection): void { + const apiIndex: APIGroup[] = []; + + const moduleApiSection: APIGroup = { + text: 'Module API', + items: [], + }; + + apiIndex.push(moduleApiSection); + + const apiModules = selectApiModules(project); + const directMethods = selectDirectMethods(project); + + moduleApiSection.items = [...apiModules, ...directMethods] + .map((module) => { + const apiSection: APIItem = { + text: extractModuleName(module), + link: module.name.toLowerCase(), + headers: [], + }; + if (module.kind !== ReflectionKind.Property) { + apiSection.headers = module + .getChildrenByKind(ReflectionKind.Method) + .map((child) => ({ + anchor: child.name, + text: child.name, + })); + } else { + // TODO @Shinigami92 2022-08-17: Extract capitalization into own function + apiSection.text = + apiSection.text.substring(0, 1).toUpperCase() + + apiSection.text.substring(1); + + apiSection.headers = [ + { + anchor: module.name, + text: module.name, + }, + ]; + } + return apiSection; + }) + .sort((a, b) => a.text.localeCompare(b.text)); + + writeFileSync(pathDocsApiSearchIndex, JSON.stringify(apiIndex)); +} |
