Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/ts-docs-gen/src/contracts/plugin-result-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PluginResult } from "./plugin";
import { ApiItemReference } from "./api-item-reference";

export interface ReadonlyPluginResultRegistry {
GetItem(itemReference: ApiItemReference): PluginResult | undefined;
Exists(itemReference: ApiItemReference): boolean;
}

export interface PluginResultRegistry extends ReadonlyPluginResultRegistry {
AddItem(itemReference: ApiItemReference, pluginResult: PluginResult): void;
}
2 changes: 2 additions & 0 deletions packages/ts-docs-gen/src/contracts/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export interface PluginMember {
}

export type GetItemPluginResultHandler = (reference: ApiItemReference) => PluginResult;
export type IsPluginResultExistsHandler = (reference: ApiItemReference) => boolean;

export interface PluginOptions<TKind = Contracts.ApiItemDto> {
Reference: ApiItemReference;
ApiItem: TKind;
ExtractedData: ExtractDto;
GetItemPluginResult: GetItemPluginResultHandler;
IsPluginResultExists: IsPluginResultExistsHandler;
}

export interface PluginResult<TKind = Contracts.ApiItemDto> {
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-docs-gen/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Generator } from "./generator";
async function Main(): Promise<void> {
const projectDirectory = path.join(process.cwd(), "./examples/simple/");
// const entryFiles = ["./index.ts", "./exported-const-variables.ts", "./exported-functions.ts"];
const entryFiles = ["./index.ts", "./exported-functions.ts"];
const entryFiles = ["./exported-functions.ts", "./index.ts"];

const configPromise = new GeneratorConfigurationBuilder(projectDirectory).Build(entryFiles);
const config = await configPromise;
Expand Down
3 changes: 1 addition & 2 deletions packages/ts-docs-gen/src/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export class FileManager {
for (const [fileLocation, items] of this.filesList) {
// Link definitions to file location.
const linkDefinitions: string[] = [];
for (const item of items) {

for (const item of items.reverse()) {
item.UsedReferences
.forEach(referenceId => {
const filePath = path.dirname(fileLocation);
Expand Down
20 changes: 11 additions & 9 deletions packages/ts-docs-gen/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import { ApiDefaultPlugin } from "./plugins/api-default-plugin";
import { ApiItemReference } from "./contracts/api-item-reference";
import { PluginResult, PluginOptions, GetItemPluginResultHandler } from "./contracts/plugin";
import { FileResult } from "./contracts/file-result";
import { PluginResultRegistry } from "./contracts/plugin-result-registry";
import { PluginResultRegistry as PluginResultRegistryClass } from "./registries/plugin-result-registry";

export class Generator {
constructor(private configuration: GeneratorConfiguration) {
this.fileManager = new FileManager();
this.pluginResultRegistry = new PluginResultRegistryClass();
const { ExtractedData } = this.configuration;

for (const entryFile of ExtractedData.EntryFiles) {
Expand All @@ -26,10 +29,7 @@ export class Generator {
this.outputData = this.fileManager.ToFilesOutput();
}

/**
* FIXME: Reference check.... how Map Works here.
*/
private renderedItems: Map<ApiItemReference, PluginResult> = new Map();
private pluginResultRegistry: PluginResultRegistry;
private fileManager: FileManager;
private outputData: FileResult[];

Expand All @@ -53,14 +53,15 @@ export class Generator {
}

private getItemPluginResult: GetItemPluginResultHandler = (apiItemReference: ApiItemReference): PluginResult => {
const renderedItem = this.renderedItems.get(apiItemReference);
const renderedItem = this.pluginResultRegistry.GetItem(apiItemReference);

if (renderedItem == null) {
const { Registry } = this.configuration.ExtractedData;
const renderedData = this.renderApiItem(apiItemReference, Registry[apiItemReference.Id]);
this.renderedItems.set(apiItemReference, renderedData);

return renderedData;
const pluginResult = this.renderApiItem(apiItemReference, Registry[apiItemReference.Id]);
this.pluginResultRegistry.AddItem(apiItemReference, pluginResult);

return pluginResult;
}

return renderedItem;
Expand All @@ -76,7 +77,8 @@ export class Generator {
ExtractedData: this.configuration.ExtractedData,
Reference: apiItemReference,
ApiItem: apiItem,
GetItemPluginResult: this.getItemPluginResult
GetItemPluginResult: this.getItemPluginResult,
IsPluginResultExists: reference => this.pluginResultRegistry.Exists(reference)
};

for (const plugin of plugins) {
Expand Down
57 changes: 35 additions & 22 deletions packages/ts-docs-gen/src/plugins/api-source-file-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PluginMember, Plugin, SupportedApiItemKindType, PluginOptions, PluginRe

interface RenderItems {
References: string[];
Headings: PluginHeading[];
Output: string[];
Members: PluginMember[];
}
Expand All @@ -23,49 +24,60 @@ export class ApiSourceFilePlugin implements Plugin<Contracts.ApiSourceFileDto> {
// TODO: Move this to helpers.
private renderItems(data: PluginOptions<Contracts.ApiSourceFileDto>): RenderItems {
const references = GeneratorHelpers.GetApiItemReferences(data.ExtractedData, data.ApiItem.Members);
const referencesList: string[] = [];
let referencesList: string[] = [];
let headingsList: PluginHeading[] = [];
const members: PluginMember[] = [];
const builder = new MarkdownBuilder();

for (const reference of references) {
const apiItem = data.ExtractedData.Registry[reference.Id];

switch (apiItem.ApiKind) {
case Contracts.ApiItemKinds.Namespace:
case Contracts.ApiItemKinds.Class: {
if (data.IsPluginResultExists(reference)) {
builder
.Text(md => md.Header(md.Link(apiItem.Name, reference.Id, true), 2))
.EmptyLine();
referencesList.push(reference.Id);
} else {
switch (apiItem.ApiKind) {
case Contracts.ApiItemKinds.Namespace:
case Contracts.ApiItemKinds.Class: {

const renderedItem = data.GetItemPluginResult(reference);
members.push({
Reference: reference,
PluginResult: renderedItem
});
const renderedItem = data.GetItemPluginResult(reference);
members.push({
Reference: reference,
PluginResult: renderedItem
});

builder
.Text(md => md.Header(md.Link(renderedItem.ApiItem.Name, reference.Id, true), 2))
.EmptyLine();
referencesList.push(reference.Id);
break;
}
default: {
const renderedItem = data.GetItemPluginResult(reference);
// Something to do with heading. Maybe heading reference registry?
builder
.Text(renderedItem.Result)
.EmptyLine();
builder
.Text(md => md.Header(md.Link(renderedItem.ApiItem.Name, reference.Id, true), 2))
.EmptyLine();
referencesList.push(reference.Id);
break;
}
default: {
const renderedItem = data.GetItemPluginResult(reference);
builder
.Text(renderedItem.Result)
.EmptyLine();

headingsList = headingsList.concat(renderedItem.Headings);
referencesList = referencesList.concat(renderedItem.UsedReferences);
}
}
}
}

return {
References: referencesList,
Headings: headingsList,
Output: builder.GetOutput(),
Members: members
};
}

public Render(data: PluginOptions<Contracts.ApiSourceFileDto>): PluginResult {
const heading = path.basename(data.ApiItem.Name, path.extname(data.ApiItem.Name));
const headings: PluginHeading[] = [
let headings: PluginHeading[] = [
{
Heading: heading,
ApiItemId: data.Reference.Id
Expand All @@ -74,6 +86,7 @@ export class ApiSourceFilePlugin implements Plugin<Contracts.ApiSourceFileDto> {

const renderedItems = this.renderItems(data);
const references: string[] = renderedItems.References;
headings = headings.concat(renderedItems.Headings);

// Header
const builder = new MarkdownBuilder()
Expand Down
36 changes: 36 additions & 0 deletions packages/ts-docs-gen/src/registries/plugin-result-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ApiItemReference } from "../contracts/api-item-reference";
import { PluginResult } from "../contracts/plugin";
import { PluginResultRegistry as PluginResultRegistryInterface } from "../contracts/plugin-result-registry";

export class PluginResultRegistry implements PluginResultRegistryInterface {
private results: Map<ApiItemReference, PluginResult> = new Map();

private getKey(itemReference: ApiItemReference): ApiItemReference | undefined {
for (const [reference] of this.results) {
if (itemReference.Alias === reference.Alias &&
itemReference.Id === reference.Id) {
return reference;
}
}

return undefined;
}

public AddItem(itemReference: ApiItemReference, pluginResult: PluginResult): void {
const key = this.getKey(itemReference) || itemReference;
this.results.set(key, pluginResult);
}

public GetItem(itemReference: ApiItemReference): PluginResult | undefined {
const realKey = this.getKey(itemReference);
if (realKey != null) {
return this.results.get(realKey);
} else {
return undefined;
}
}

public Exists(itemReference: ApiItemReference): boolean {
return this.getKey(itemReference) != null;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.