From b05be9ec084a46cc3d850c76cb24526449a7d763 Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com> Date: Tue, 11 Oct 2022 10:01:32 -0400 Subject: [PATCH] feat(website): Add utility for resolving summaries for json items (#8734) --- apps/website/src/util/summary.ts | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 apps/website/src/util/summary.ts diff --git a/apps/website/src/util/summary.ts b/apps/website/src/util/summary.ts new file mode 100644 index 000000000..46852655f --- /dev/null +++ b/apps/website/src/util/summary.ts @@ -0,0 +1,47 @@ +import type { + ApiItemJSON, + DocNodeJSON, + DocCodeSpanJSON, + DocPlainTextJSON, + DocNodeContainerJSON, +} from '@discordjs/api-extractor-utils'; + +export function tryResolveDescription(member: ApiItemJSON) { + const { summary } = member!; + + if (!summary) { + return undefined; + } + + let retVal = ''; + + function recurseNodes(node: DocNodeJSON) { + switch (node.kind) { + case 'CodeSpan': + retVal += (node as DocCodeSpanJSON).code; + break; + case 'PlainText': + retVal += (node as DocPlainTextJSON).text; + break; + case 'Section': + case 'Paragraph': + for (const currentNode of (node as DocNodeContainerJSON).nodes) { + recurseNodes(currentNode); + } + + break; + default: + break; + } + } + + for (const node of summary.nodes) { + recurseNodes(node); + } + + if (retVal === '') { + return null; + } + + return retVal; +}