Skip to content

Commit

Permalink
docs: fix full url to absolute conversion (#2101)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Apr 28, 2023
1 parent 971f363 commit 3cfeb38
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 10 deletions.
8 changes: 4 additions & 4 deletions scripts/apidoc/fakerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { DeclarationReflection, ProjectReflection } from 'typedoc';
import { ReflectionKind } from 'typedoc';
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
import { writeApiDocsModule } from './apiDocsWriter';
import { processModuleMethods } from './moduleMethods';
import { analyzeModule, processModuleMethods } from './moduleMethods';
import { analyzeSignature } from './signature';
import { extractDescription, selectApiSignature } from './typedoc';
import { selectApiSignature } from './typedoc';
import type { ModuleSummary } from './utils';

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

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

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

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

return writeApiDocsModule('Faker', 'faker', comment, undefined, methods);
return writeApiDocsModule('Faker', 'faker', comment, deprecated, methods);
}

function processConstructor(fakerClass: DeclarationReflection): Method {
Expand Down
4 changes: 2 additions & 2 deletions scripts/apidoc/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sanitizeHtml from 'sanitize-html';
import type { MarkdownRenderer } from 'vitepress';
import { createMarkdownRenderer } from 'vitepress';
import vitepressConfig from '../../docs/.vitepress/config';
import { pathOutputDir } from './utils';
import { adjustUrls, pathOutputDir } from './utils';

let markdown: MarkdownRenderer;

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

console.debug('Rejected unsafe md:', md);
Expand Down
22 changes: 19 additions & 3 deletions scripts/apidoc/moduleMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
selectApiModules,
} from './typedoc';
import type { ModuleSummary } from './utils';
import { adjustUrls } from './utils';

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

return writeApiDocsModule(
Expand All @@ -49,6 +49,22 @@ function processModule(module: DeclarationReflection): ModuleSummary {
);
}

/**
* Analyzes the documentation for a class.
*
* @param module The class to process.
* @returns The class information.
*/
export function analyzeModule(module: DeclarationReflection): {
comment: string;
deprecated: string | undefined;
} {
return {
comment: adjustUrls(extractDescription(module)),
deprecated: extractDeprecated(module),
};
}

/**
* Processes all api methods of the given class. This does not include the constructor.
*
Expand Down
4 changes: 4 additions & 0 deletions scripts/apidoc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export const pathOutputDir = resolve(pathDocsDir, 'api');

// Functions

export function adjustUrls(description: string): string {
return description.replace(/https:\/\/(next.)?fakerjs.dev\//g, '/');
}

export function mapByName<T extends { name: string }, V>(
input: T[],
valueExtractor: (item: T) => V
Expand Down
24 changes: 24 additions & 0 deletions test/scripts/apidoc/__snapshots__/module.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`module > analyzeModule() > ModuleFakerJsLinkTest 1`] = `
{
"comment": "Description with a link to our [website](/)
and [api docs](/api/).",
"deprecated": undefined,
}
`;

exports[`module > analyzeModule() > ModuleNextFakerJsLinkTest 1`] = `
{
"comment": "Description with a link to our [website](/)
and [api docs](/api/).",
"deprecated": undefined,
}
`;

exports[`module > analyzeModule() > expected and actual modules are equal 1`] = `
[
"ModuleFakerJsLinkTest",
"ModuleNextFakerJsLinkTest",
]
`;
11 changes: 11 additions & 0 deletions test/scripts/apidoc/module.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Description with a link to our [website](https://fakerjs.dev/)
* and [api docs](https://fakerjs.dev/api/).
*/
export class ModuleFakerJsLinkTest {}

/**
* Description with a link to our [website](https://next.fakerjs.dev/)
* and [api docs](https://next.fakerjs.dev/api/).
*/
export class ModuleNextFakerJsLinkTest {}
27 changes: 27 additions & 0 deletions test/scripts/apidoc/module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { beforeAll, describe, expect, it } from 'vitest';
import { initMarkdownRenderer } from '../../../scripts/apidoc/markdown';
import { analyzeModule } from '../../../scripts/apidoc/moduleMethods';
import * as ModuleTests from './module.example';
import { loadExampleModules } from './utils';

describe('module', () => {
describe('analyzeModule()', () => {
const modules = loadExampleModules();

beforeAll(initMarkdownRenderer);

it('dummy dependency to rerun the test if the example changes', () => {
expect(Object.keys(ModuleTests)).not.toEqual([]);
});

it('expected and actual modules are equal', () => {
expect(Object.keys(modules).sort()).toMatchSnapshot();
});

it.each(Object.entries(modules))('%s', (_, module) => {
const actual = analyzeModule(module);

expect(actual).toMatchSnapshot();
});
});
});
2 changes: 1 addition & 1 deletion test/scripts/apidoc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"compilerOptions": {
"target": "ES5"
},
"include": ["signature.example.ts"]
"include": ["signature.example.ts", "module.example.ts"]
}
20 changes: 20 additions & 0 deletions test/scripts/apidoc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,23 @@ export function loadExampleMethods(): Record<string, SignatureReflection> {
true
)['SignatureTest'][1];
}

/**
* Loads the example modules using TypeDoc.
*/
export function loadExampleModules(): Record<string, DeclarationReflection> {
const modules = loadProjectModules(
{
entryPoints: ['test/scripts/apidoc/module.example.ts'],
tsconfig: 'test/scripts/apidoc/tsconfig.json',
},
true
);

const result: Record<string, DeclarationReflection> = {};
for (const key in modules) {
result[key] = modules[key][0];
}

return result;
}

0 comments on commit 3cfeb38

Please sign in to comment.