Skip to content

Commit 3cfeb38

Browse files
authored
docs: fix full url to absolute conversion (#2101)
1 parent 971f363 commit 3cfeb38

9 files changed

Lines changed: 112 additions & 10 deletions

File tree

scripts/apidoc/fakerClass.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { DeclarationReflection, ProjectReflection } from 'typedoc';
22
import { ReflectionKind } from 'typedoc';
33
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
44
import { writeApiDocsModule } from './apiDocsWriter';
5-
import { processModuleMethods } from './moduleMethods';
5+
import { analyzeModule, processModuleMethods } from './moduleMethods';
66
import { analyzeSignature } from './signature';
7-
import { extractDescription, selectApiSignature } from './typedoc';
7+
import { selectApiSignature } from './typedoc';
88
import type { ModuleSummary } from './utils';
99

1010
export function processFakerClass(project: ProjectReflection): ModuleSummary {
@@ -21,15 +21,15 @@ export function processFakerClass(project: ProjectReflection): ModuleSummary {
2121

2222
function processClass(fakerClass: DeclarationReflection): ModuleSummary {
2323
console.log(`Processing Faker class`);
24-
const comment = extractDescription(fakerClass);
24+
const { comment, deprecated } = analyzeModule(fakerClass);
2525
const methods: Method[] = [];
2626

2727
console.debug(`- constructor`);
2828
methods.push(processConstructor(fakerClass));
2929

3030
methods.push(...processModuleMethods(fakerClass, 'faker.'));
3131

32-
return writeApiDocsModule('Faker', 'faker', comment, undefined, methods);
32+
return writeApiDocsModule('Faker', 'faker', comment, deprecated, methods);
3333
}
3434

3535
function processConstructor(fakerClass: DeclarationReflection): Method {

scripts/apidoc/markdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sanitizeHtml from 'sanitize-html';
22
import type { MarkdownRenderer } from 'vitepress';
33
import { createMarkdownRenderer } from 'vitepress';
44
import vitepressConfig from '../../docs/.vitepress/config';
5-
import { pathOutputDir } from './utils';
5+
import { adjustUrls, pathOutputDir } from './utils';
66

77
let markdown: MarkdownRenderer;
88

@@ -57,7 +57,7 @@ export function mdToHtml(md: string, inline: boolean = false): string {
5757
const safeHtml: string = sanitizeHtml(rawHtml, htmlSanitizeOptions);
5858
// Revert some escaped characters for comparison.
5959
if (comparableSanitizedHtml(rawHtml) === comparableSanitizedHtml(safeHtml)) {
60-
return safeHtml.replace(/https:\/\/(next.)?fakerjs.dev\//g, '/');
60+
return adjustUrls(safeHtml);
6161
}
6262

6363
console.debug('Rejected unsafe md:', md);

scripts/apidoc/moduleMethods.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
selectApiModules,
1616
} from './typedoc';
1717
import type { ModuleSummary } from './utils';
18+
import { adjustUrls } from './utils';
1819

1920
/**
2021
* Analyzes and writes the documentation for modules and their methods such as `faker.animal.cat()`.
@@ -34,10 +35,9 @@ export function processModules(project: ProjectReflection): ModuleSummary[] {
3435
*/
3536
function processModule(module: DeclarationReflection): ModuleSummary {
3637
const moduleName = extractModuleName(module);
37-
const moduleFieldName = extractModuleFieldName(module);
3838
console.log(`Processing Module ${moduleName}`);
39-
const comment = extractDescription(module);
40-
const deprecated = extractDeprecated(module);
39+
const moduleFieldName = extractModuleFieldName(module);
40+
const { comment, deprecated } = analyzeModule(module);
4141
const methods = processModuleMethods(module, `faker.${moduleFieldName}.`);
4242

4343
return writeApiDocsModule(
@@ -49,6 +49,22 @@ function processModule(module: DeclarationReflection): ModuleSummary {
4949
);
5050
}
5151

52+
/**
53+
* Analyzes the documentation for a class.
54+
*
55+
* @param module The class to process.
56+
* @returns The class information.
57+
*/
58+
export function analyzeModule(module: DeclarationReflection): {
59+
comment: string;
60+
deprecated: string | undefined;
61+
} {
62+
return {
63+
comment: adjustUrls(extractDescription(module)),
64+
deprecated: extractDeprecated(module),
65+
};
66+
}
67+
5268
/**
5369
* Processes all api methods of the given class. This does not include the constructor.
5470
*

scripts/apidoc/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export const pathOutputDir = resolve(pathDocsDir, 'api');
4343

4444
// Functions
4545

46+
export function adjustUrls(description: string): string {
47+
return description.replace(/https:\/\/(next.)?fakerjs.dev\//g, '/');
48+
}
49+
4650
export function mapByName<T extends { name: string }, V>(
4751
input: T[],
4852
valueExtractor: (item: T) => V
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`module > analyzeModule() > ModuleFakerJsLinkTest 1`] = `
4+
{
5+
"comment": "Description with a link to our [website](/)
6+
and [api docs](/api/).",
7+
"deprecated": undefined,
8+
}
9+
`;
10+
11+
exports[`module > analyzeModule() > ModuleNextFakerJsLinkTest 1`] = `
12+
{
13+
"comment": "Description with a link to our [website](/)
14+
and [api docs](/api/).",
15+
"deprecated": undefined,
16+
}
17+
`;
18+
19+
exports[`module > analyzeModule() > expected and actual modules are equal 1`] = `
20+
[
21+
"ModuleFakerJsLinkTest",
22+
"ModuleNextFakerJsLinkTest",
23+
]
24+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Description with a link to our [website](https://fakerjs.dev/)
3+
* and [api docs](https://fakerjs.dev/api/).
4+
*/
5+
export class ModuleFakerJsLinkTest {}
6+
7+
/**
8+
* Description with a link to our [website](https://next.fakerjs.dev/)
9+
* and [api docs](https://next.fakerjs.dev/api/).
10+
*/
11+
export class ModuleNextFakerJsLinkTest {}

test/scripts/apidoc/module.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { beforeAll, describe, expect, it } from 'vitest';
2+
import { initMarkdownRenderer } from '../../../scripts/apidoc/markdown';
3+
import { analyzeModule } from '../../../scripts/apidoc/moduleMethods';
4+
import * as ModuleTests from './module.example';
5+
import { loadExampleModules } from './utils';
6+
7+
describe('module', () => {
8+
describe('analyzeModule()', () => {
9+
const modules = loadExampleModules();
10+
11+
beforeAll(initMarkdownRenderer);
12+
13+
it('dummy dependency to rerun the test if the example changes', () => {
14+
expect(Object.keys(ModuleTests)).not.toEqual([]);
15+
});
16+
17+
it('expected and actual modules are equal', () => {
18+
expect(Object.keys(modules).sort()).toMatchSnapshot();
19+
});
20+
21+
it.each(Object.entries(modules))('%s', (_, module) => {
22+
const actual = analyzeModule(module);
23+
24+
expect(actual).toMatchSnapshot();
25+
});
26+
});
27+
});

test/scripts/apidoc/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"compilerOptions": {
33
"target": "ES5"
44
},
5-
"include": ["signature.example.ts"]
5+
"include": ["signature.example.ts", "module.example.ts"]
66
}

test/scripts/apidoc/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,23 @@ export function loadExampleMethods(): Record<string, SignatureReflection> {
3939
true
4040
)['SignatureTest'][1];
4141
}
42+
43+
/**
44+
* Loads the example modules using TypeDoc.
45+
*/
46+
export function loadExampleModules(): Record<string, DeclarationReflection> {
47+
const modules = loadProjectModules(
48+
{
49+
entryPoints: ['test/scripts/apidoc/module.example.ts'],
50+
tsconfig: 'test/scripts/apidoc/tsconfig.json',
51+
},
52+
true
53+
);
54+
55+
const result: Record<string, DeclarationReflection> = {};
56+
for (const key in modules) {
57+
result[key] = modules[key][0];
58+
}
59+
60+
return result;
61+
}

0 commit comments

Comments
 (0)