From fda44bb899efbcbac2071a199964a3e269cbd805 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Sun, 14 Aug 2022 01:59:21 +0800 Subject: docs: rewrite api pages to have a right aside toc (#1265) --- scripts/apidoc/apiDocsWriter.ts | 74 +++++++++++++++++++++++------------------ scripts/apidoc/directMethods.ts | 8 ++--- scripts/apidoc/moduleMethods.ts | 7 +++- 3 files changed, 51 insertions(+), 38 deletions(-) (limited to 'scripts') diff --git a/scripts/apidoc/apiDocsWriter.ts b/scripts/apidoc/apiDocsWriter.ts index cb803499..76edc3c3 100644 --- a/scripts/apidoc/apiDocsWriter.ts +++ b/scripts/apidoc/apiDocsWriter.ts @@ -26,40 +26,41 @@ editLink: false * @param moduleName The name of the module to write the docs for. * @param lowerModuleName The lowercase name of the module. * @param comment The module comments. + * @param methods The methods of the module. */ export function writeApiDocsModulePage( moduleName: string, lowerModuleName: string, - comment: string + comment: string, + methods: Method[] ): void { // Write api docs page let content = ` - # ${moduleName} - + # ${moduleName} + ::: v-pre ${comment} ::: - + ${methods + .map( + (method) => ` + ## ${method.name} - + + ` + ) + .join('')} `.replace(/\n +/g, '\n'); content = vitePressInFileOptions + formatMarkdown(content); @@ -71,18 +72,26 @@ export function writeApiDocsModulePage( * Writes the api page for the given method to the correct location. * * @param methodName The name of the method to write the docs for. + * @param capitalizedMethodName The capitalized name of the method. */ -export function writeApiDocsDirectPage(methodName: string): void { +export function writeApiDocsDirectPage( + methodName: string, + capitalizedMethodName: string +): void { let content = ` - + + + + # ${capitalizedMethodName} + + ## ${methodName} + + `.replace(/\n +/g, '\n'); content = vitePressInFileOptions + formatMarkdown(content); @@ -100,18 +109,17 @@ export function writeApiDocsData( lowerModuleName: string, methods: Method[] ): void { - let contentTs = ` -import type { Method } from '../.vitepress/components/api-docs/method'; - -export const ${lowerModuleName}: Method[] = ${JSON.stringify( - methods, - null, - 2 - )}`; - - contentTs = formatTypescript(contentTs); - - writeFileSync(resolve(pathOutputDir, `${lowerModuleName}.ts`), contentTs); + const content = JSON.stringify( + methods.reduce>( + (map, method) => ({ + ...map, + [method.name]: method, + }), + {} + ) + ); + + writeFileSync(resolve(pathOutputDir, `${lowerModuleName}.json`), content); } /** diff --git a/scripts/apidoc/directMethods.ts b/scripts/apidoc/directMethods.ts index 159ad848..fbdd094e 100644 --- a/scripts/apidoc/directMethods.ts +++ b/scripts/apidoc/directMethods.ts @@ -37,21 +37,21 @@ export function processDirectMethod( direct: TypeDoc.DeclarationReflection ): Page { const methodName = direct.name; - const upperMethodName = + const capitalizedMethodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1); - console.log(`Processing Direct: ${upperMethodName}`); + console.log(`Processing Direct: ${capitalizedMethodName}`); const signatures = (direct.type as TypeDoc.ReflectionType).declaration .signatures; const signature = signatures[signatures.length - 1]; - writeApiDocsDirectPage(methodName); + writeApiDocsDirectPage(methodName, capitalizedMethodName); writeApiDocsData(methodName, [ analyzeSignature(signature, undefined, methodName), ]); return { - text: upperMethodName, + text: capitalizedMethodName, link: `/api/${methodName}.html`, }; } diff --git a/scripts/apidoc/moduleMethods.ts b/scripts/apidoc/moduleMethods.ts index 3d81c2db..64f6ed1b 100644 --- a/scripts/apidoc/moduleMethods.ts +++ b/scripts/apidoc/moduleMethods.ts @@ -56,7 +56,12 @@ function processModuleMethod(module: TypeDoc.DeclarationReflection): PageIndex { methods.push(analyzeSignature(signature, lowerModuleName, methodName)); } - writeApiDocsModulePage(moduleName, lowerModuleName, toBlock(module.comment)); + writeApiDocsModulePage( + moduleName, + lowerModuleName, + toBlock(module.comment), + methods + ); writeApiDocsData(lowerModuleName, methods); return [ -- cgit v1.2.3