Skip to content

Commit

Permalink
fix(docs): handle examples not starting with code
Browse files Browse the repository at this point in the history
fixes #211
  • Loading branch information
almostSouji committed Jul 25, 2024
1 parent ff136e8 commit e5ec9a1
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/functions/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ function formatSummary(blocks: any[], _package: string, version: string) {
.join('');
}

function formatExample(blocks: any[]) {
const comments: string[] = [];
for (const block of blocks) {
if (block.kind === 'PlainText' && block.text.length) {
comments.push(`// ${block.text}`);
continue;
}

if (block.kind === 'FencedCode') {
return codeBlock(block.language, `${comments.join('\n')}\n${block.text}`);
}
}
}

function formatItem(_item: any, _package: string, version: string, member?: string) {
const itemLink = docsLink(_item, _package, version, member);
const item = effectiveItem(_item, member);
Expand Down Expand Up @@ -249,7 +263,7 @@ function formatItem(_item: any, _package: string, version: string, member?: stri

const summary = item.summary?.summarySection;
const deprecationNote = item.summary?.deprecatedBlock;
const example = item.summary?.exampleBlocks?.[0];
const example = formatExample(item.summary?.exampleBlocks);

if (deprecationNote?.length) {
lines.push(`${bold('[DEPRECATED]')} ${formatSummary(deprecationNote, _package, version)}`);
Expand All @@ -259,7 +273,7 @@ function formatItem(_item: any, _package: string, version: string, member?: stri
}

if (example) {
lines.push(codeBlock(example.language, example.text));
lines.push(example);
}
}

Expand Down

0 comments on commit e5ec9a1

Please sign in to comment.