Skip to content

Commit 3dba048

Browse files
refactor: extract render jsdocs metadata and url (#71)
1 parent 9d3b2ca commit 3dba048

File tree

3 files changed

+66
-38
lines changed

3 files changed

+66
-38
lines changed

src/lib/parser/markdown.ts

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
jsDocsToReferences,
1313
jsDocsToReturnType
1414
} from './jdocs/mapper';
15-
import {inlineReferences} from './jdocs/render';
15+
import {emojiTitle, metadataToMarkdown, sourceCodeLink} from './render';
1616
import type {Params, Row} from './types';
1717

1818
const toParams = (parameters?: DocEntry[]): Params[] =>
@@ -180,12 +180,6 @@ const interfacesToMarkdown = ({
180180
return markdown.join('\n');
181181
};
182182

183-
const sourceCodeLink = ({
184-
url,
185-
emoji
186-
}: Pick<MarkdownOptions, 'emoji'> & Required<Pick<DocEntry, 'url'>>): string =>
187-
`[${emojiTitle({emoji, key: 'link'}).trim()}${emoji !== null && emoji !== undefined ? ' ' : ''}Source](${url})\n`;
188-
189183
// Avoid issue if the Markdown table gets formatted with Prettier
190184
const parseType = (type: string): string =>
191185
type
@@ -248,26 +242,15 @@ const toMarkdown = ({
248242
markdown.push('\n');
249243
}
250244

251-
if (returnType !== undefined && returnType !== '') {
252-
markdown.push(`Returns:\n`);
253-
markdown.push(`${returnType}\n`);
254-
}
245+
const metadata = metadataToMarkdown({
246+
returnType,
247+
references,
248+
examples,
249+
url,
250+
emoji
251+
});
255252

256-
if (references?.length) {
257-
markdown.push(`References:\n`);
258-
markdown.push(...inlineReferences(references));
259-
markdown.push('\n');
260-
}
261-
262-
if (examples.length) {
263-
markdown.push('Examples:\n');
264-
markdown.push(...examples);
265-
markdown.push('\n');
266-
}
267-
268-
if (url !== undefined) {
269-
markdown.push(sourceCodeLink({emoji, url}));
270-
}
253+
markdown.push(...metadata);
271254

272255
return markdown.join('\n');
273256
};
@@ -290,14 +273,6 @@ const tableOfContent = ({
290273
)
291274
.join('\n');
292275

293-
const emojiTitle = ({
294-
emoji,
295-
key
296-
}: {
297-
key: keyof MarkdownEmoji;
298-
} & Pick<MarkdownOptions, 'emoji'>): string =>
299-
emoji === undefined || emoji === null ? '' : ` :${emoji[key]}:`;
300-
301276
const DEFAULT_EMOJI: MarkdownEmoji = {
302277
classes: 'factory',
303278
functions: 'toolbox',

src/lib/parser/render.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type {DocEntry, MarkdownEmoji, MarkdownOptions} from '../types';
2+
import {inlineReferences} from './jdocs/render';
3+
import type {MappedJsDocs} from './types';
4+
5+
export const emojiTitle = ({
6+
emoji,
7+
key
8+
}: {
9+
key: keyof MarkdownEmoji;
10+
} & Pick<MarkdownOptions, 'emoji'>): string =>
11+
emoji === undefined || emoji === null ? '' : ` :${emoji[key]}:`;
12+
13+
export const sourceCodeLink = ({
14+
url,
15+
emoji
16+
}: Pick<MarkdownOptions, 'emoji'> & Required<Pick<DocEntry, 'url'>>): string =>
17+
`[${emojiTitle({emoji, key: 'link'}).trim()}${emoji !== null && emoji !== undefined ? ' ' : ''}Source](${url})\n`;
18+
19+
export const metadataToMarkdown = ({
20+
returnType,
21+
references,
22+
examples,
23+
url,
24+
emoji
25+
}: MappedJsDocs & Pick<DocEntry, 'url'> & {emoji: MarkdownEmoji | null | undefined}): string[] => {
26+
const markdown: string[] = [];
27+
28+
if (returnType !== undefined && returnType !== '') {
29+
markdown.push(`Returns:\n`);
30+
markdown.push(`${returnType}\n`);
31+
}
32+
33+
if (references?.length) {
34+
markdown.push(`References:\n`);
35+
markdown.push(...inlineReferences(references));
36+
markdown.push('\n');
37+
}
38+
39+
if (examples.length) {
40+
markdown.push('Examples:\n');
41+
markdown.push(...examples);
42+
markdown.push('\n');
43+
}
44+
45+
if (url !== undefined) {
46+
markdown.push(sourceCodeLink({emoji, url}));
47+
}
48+
49+
return markdown;
50+
};

src/lib/parser/types.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ export interface Params {
55
documentation: string;
66
}
77

8+
export interface MappedJsDocs {
9+
examples: string[];
10+
returnType?: string;
11+
references?: string[];
12+
}
13+
814
export type Row = Required<Pick<DocEntry, 'name' | 'type' | 'documentation'>> &
915
Pick<DocEntry, 'url'> & {
1016
params: Params[];
11-
examples: string[];
12-
returnType?: string;
13-
references?: string[];
14-
};
17+
} & MappedJsDocs;

0 commit comments

Comments
 (0)