Skip to content

Commit a734ca0

Browse files
authored
docs: properly handle multiple @see tags (#1270)
1 parent 13ad646 commit a734ca0

File tree

7 files changed

+71
-20
lines changed

7 files changed

+71
-20
lines changed

docs/.vitepress/components/api-docs/method.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ function seeAlsoToUrl(see: string): string {
3030

3131
<div v-if="props.method.seeAlsos.length > 0">
3232
<h3>See Also</h3>
33-
<div v-for="seeAlso of props.method.seeAlsos" :key="seeAlso">
34-
<a :href="seeAlsoToUrl(seeAlso)" v-if="seeAlso.startsWith('faker.')">
35-
<p>{{ seeAlso }}</p>
36-
</a>
37-
<p v-else>{{ seeAlso }}</p>
38-
</div>
33+
<ul>
34+
<li v-for="seeAlso of props.method.seeAlsos" :key="seeAlso">
35+
<a v-if="seeAlso.startsWith('faker.')" :href="seeAlsoToUrl(seeAlso)">
36+
{{ seeAlso }}
37+
</a>
38+
<template v-else>{{ seeAlso }}</template>
39+
</li>
40+
</ul>
3941
</div>
4042
</div>
4143
</template>

scripts/apidoc/signature.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import vitepressConfig from '../../docs/.vitepress/config';
2020
import { faker } from '../../src';
2121
import {
2222
extractRawExamples,
23-
extractTagContent,
23+
extractSeeAlsos,
2424
formatTypescript,
2525
isDeprecated,
2626
joinTagParts,
@@ -143,7 +143,7 @@ export function analyzeSignature(
143143
examples += `${exampleTags.join('\n').trim()}\n`;
144144
}
145145

146-
const seeAlsos = extractTagContent('@see', signature);
146+
const seeAlsos = extractSeeAlsos(signature);
147147

148148
const prettyMethodName = prettifyMethodName(methodName);
149149
const code = '```';

scripts/apidoc/utils.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ const prettierTypescript: Options = {
8989
*/
9090
export function extractTagContent(
9191
tag: `@${string}`,
92-
signature?: SignatureReflection
92+
signature?: SignatureReflection,
93+
tagProcessor: (tag: CommentTag) => string[] = joinTagContent
9394
): string[] {
94-
return signature?.comment?.getTags(tag).map(joinTagContent) ?? [];
95+
return signature?.comment?.getTags(tag).flatMap(tagProcessor) ?? [];
9596
}
9697

9798
/**
@@ -105,11 +106,28 @@ export function extractRawExamples(signature?: SignatureReflection): string[] {
105106
);
106107
}
107108

109+
/**
110+
* Extracts all the `@see` references from the jsdocs separately.
111+
*
112+
* @param signature The signature to extract the see also references from.
113+
*/
114+
export function extractSeeAlsos(signature?: SignatureReflection): string[] {
115+
return extractTagContent('@see', signature, (tag) => {
116+
const content = tag.content;
117+
if (content.length === 1) {
118+
return joinTagContent(tag);
119+
}
120+
return tag.content
121+
.filter((_, index) => index % 3 === 1) // ['-', 'content', '\n']
122+
.map((part) => part.text);
123+
});
124+
}
125+
108126
/**
109127
* Joins the parts of the given jsdocs tag.
110128
*/
111-
export function joinTagContent(tag: CommentTag): string {
112-
return joinTagParts(tag?.content);
129+
export function joinTagContent(tag: CommentTag): string[] {
130+
return [joinTagParts(tag?.content)];
113131
}
114132

115133
export function joinTagParts(parts: CommentDisplayPart[]): string;

test/scripts/apidoc/examplesAndDeprecations.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { selectDirectMethods } from '../../../scripts/apidoc/directMethods';
88
import { selectApiModules } from '../../../scripts/apidoc/moduleMethods';
99
import {
1010
extractRawExamples,
11+
extractSeeAlsos,
1112
extractTagContent,
1213
isDeprecated,
1314
} from '../../../scripts/apidoc/utils';
@@ -111,8 +112,8 @@ describe('examples and deprecations', () => {
111112
}
112113

113114
// Verify @see tag
114-
extractTagContent('@see', signature).forEach((link) => {
115-
if (link.startsWith('faker')) {
115+
extractSeeAlsos(signature).forEach((link) => {
116+
if (link.startsWith('faker.')) {
116117
// Expected @see faker.xxx.yyy()
117118
expect(link, 'Expect method reference to contain ()').toContain('(');
118119
expect(link, 'Expect method reference to contain ()').toContain(')');

test/scripts/apidoc/signature.debug.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
* This file exists, because vitest doesn't allow me to debug code outside of src and test.
33
* And it's easier to test these features independently from the main project.
44
*/
5-
import { analyzeSignature } from '../../../scripts/apidoc/signature';
5+
import {
6+
analyzeSignature,
7+
initMarkdownRenderer,
8+
} from '../../../scripts/apidoc/signature';
69
import { loadExampleMethods } from './utils';
710

811
/* Run with `pnpm tsx test/scripts/apidoc/signature.debug.ts` */
912

1013
const methods = loadExampleMethods();
1114

12-
Object.entries(methods).forEach(([name, method]) => {
13-
console.log('Analyzing: ', name);
14-
const result = analyzeSignature(method.signatures[0], null, method.name);
15-
console.log('Result: ', result);
16-
});
15+
initMarkdownRenderer()
16+
.then(() => {
17+
Object.entries(methods).forEach(([name, method]) => {
18+
console.log('Analyzing: ', name);
19+
const result = analyzeSignature(method.signatures[0], null, method.name);
20+
console.log('Result: ', result);
21+
});
22+
})
23+
.catch(console.error);

test/scripts/apidoc/signature.example.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,14 @@ export class SignatureTest {
230230
methodWithDeprecated(): number {
231231
return 0;
232232
}
233+
234+
/**
235+
* Test with multiple see markers.
236+
*
237+
* @see test.apidoc.methodWithExample()
238+
* @see test.apidoc.methodWithDeprecated()
239+
*/
240+
methodWithMultipleSeeMarkers(): number {
241+
return 0;
242+
}
233243
}

test/scripts/apidoc/signature.expected.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@
6868
"deprecated": false,
6969
"seeAlsos": []
7070
},
71+
"methodWithMultipleSeeMarkers": {
72+
"name": "methodWithMultipleSeeMarkers",
73+
"title": "Method With Multiple See Markers",
74+
"description": "<p>Test with multiple see markers.</p>\n",
75+
"parameters": [],
76+
"returns": "number",
77+
"examples": "<div class=\"language-ts\"><button class=\"copy\"></button><span class=\"lang\">ts</span><pre v-pre><code><span class=\"line\"><span style=\"color: #A6ACCD\">faker</span><span style=\"color: #89DDFF\">.</span><span style=\"color: #82AAFF\">methodWithMultipleSeeMarkers</span><span style=\"color: #A6ACCD\">(): number</span></span>\n<span class=\"line\"></span></code></pre>\n</div>",
78+
"deprecated": false,
79+
"seeAlsos": [
80+
"test.apidoc.methodWithExample()",
81+
"test.apidoc.methodWithDeprecated()"
82+
]
83+
},
7184
"multiParamMethod": {
7285
"name": "multiParamMethod",
7386
"title": "Multi Param Method",

0 commit comments

Comments
 (0)