Skip to content

Commit

Permalink
Truncate long list of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Feb 15, 2024
1 parent 6664064 commit 5c8dce7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/formatter/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as md from './markdown';
import { defaultNameTransform } from './name-transform';
import type { NameTransformFn } from './types';

const MAX_VALUES = 20;

export function formatModelsAsMarkdown(
models: NamedModel[],
options: FormatterOptions
Expand Down Expand Up @@ -97,11 +99,20 @@ function formatModel(model: Model, transformName: NameTransformFn): string {
)
);
case 'enum':
const enumList = md.list.unordered(
model.values.map(value => md.code.inline(`'${value}'`))
);
return md.paragraphs(
md.italic('Enum string, one of the following possible values:'),
md.list.unordered(
model.values.map(value => md.code.inline(`'${value}'`))
)
model.values.length > MAX_VALUES
? md.details(
md.italic(
`Expand for full list of ${model.values.length} values`,
'html'
),
enumList
)
: enumList
);
case 'native-enum':
return md.paragraphs(
Expand Down Expand Up @@ -373,7 +384,11 @@ function formatModelInline(
);
case 'enum':
return md.code.inline(
model.values.map(value => `'${value}'`).join(' | ')
model.values
.slice(0, MAX_VALUES)
.map(value => `'${value}'`)
.concat(model.values.length > MAX_VALUES ? ['...'] : [])
.join(' | ')
);
case 'native-enum':
return (
Expand Down
14 changes: 12 additions & 2 deletions src/formatter/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export const paragraphs = (...texts: (string | null | undefined | false)[]) =>
export const heading = (level: 1 | 2 | 3 | 4 | 5 | 6, text: string) =>
`${'#'.repeat(level)} ${text}`;

export const bold = (text: string) => `**${text}**`;
export const bold = (text: string, lang: 'md' | 'html' = 'md') =>
lang === 'html' ? `<b>${text}</b>` : `**${text}**`;

export const italic = (text: string) => `_${text}_`;
export const italic = (text: string, lang: 'md' | 'html' = 'md') =>
lang === 'html' ? `<i>${text}</i>` : `_${text}_`;

export const link = (href: string, text?: string) =>
`[${text ?? href}](${href})`;
Expand Down Expand Up @@ -99,3 +101,11 @@ export const table = (

return lines(...rows.map(row => `| ${row.map(formatCell).join(' | ')} |`));
};

export const details = (summary: string, details: string) =>
lines(
'<details>',
`<summary>${summary}</summary>`,
`\n${details}\n`.replace(/^\n{2,}/, '\n').replace(/\n{2,}$/, '\n'),
'</details>'
);

0 comments on commit 5c8dce7

Please sign in to comment.