From 0866ee9217e5e0b60e1c5f604e3576fb2604a3d5 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Mon, 22 Aug 2022 23:38:15 +0800 Subject: docs: searchable api (#1253) --- scripts/apidoc.ts | 11 +++++++- scripts/apidoc/apiDocsWriter.ts | 57 +++++++++++++++++++++++++++++++++++++++++ scripts/apidoc/moduleMethods.ts | 2 +- 3 files changed, 68 insertions(+), 2 deletions(-) (limited to 'scripts') diff --git a/scripts/apidoc.ts b/scripts/apidoc.ts index ed6f679f..165034bc 100644 --- a/scripts/apidoc.ts +++ b/scripts/apidoc.ts @@ -1,5 +1,8 @@ import { resolve } from 'path'; -import { writeApiPagesIndex } from './apidoc/apiDocsWriter'; +import { + writeApiPagesIndex, + writeApiSearchIndex, +} from './apidoc/apiDocsWriter'; import { processDirectMethods } from './apidoc/directMethods'; import { processModuleMethods } from './apidoc/moduleMethods'; import { initMarkdownRenderer } from './apidoc/signature'; @@ -21,6 +24,10 @@ async function build(): Promise { const project = app.convert(); + if (!project) { + throw new Error('Failed to convert project'); + } + // Useful for manually analyzing the content await app.generateJson(project, pathOutputJson); @@ -31,6 +38,8 @@ async function build(): Promise { modulesPages.push(...processModuleMethods(project)); modulesPages.push(...processDirectMethods(project)); writeApiPagesIndex(modulesPages); + + writeApiSearchIndex(project); } build().catch(console.error); 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)); +} diff --git a/scripts/apidoc/moduleMethods.ts b/scripts/apidoc/moduleMethods.ts index a37bcae0..d5b8f590 100644 --- a/scripts/apidoc/moduleMethods.ts +++ b/scripts/apidoc/moduleMethods.ts @@ -38,7 +38,7 @@ export function processModuleMethods(project: ProjectReflection): PageIndex { return pages; } -function extractModuleName(module: DeclarationReflection): string { +export function extractModuleName(module: DeclarationReflection): string { return module.name.replace('_', ''); } -- cgit v1.2.3