Skip to content

Commit

Permalink
chore(scripts): fixed some issues with sidebar and links
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Jun 17, 2022
1 parent 355e49d commit 3027bda
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
12 changes: 5 additions & 7 deletions scripts/generate-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { join, resolve } from 'path';
import { ApiModel } from '@microsoft/api-extractor-model';
import { CustomMarkdownDocumenter } from './libs/CustomMarkdownDocumenter';
Expand All @@ -19,14 +19,12 @@ function build(): void {

markdown.generateFiles();

const categoryForApiFolder = {
label: 'Introduction',
position: -1,
};
const filename = join(outputFolder, 'Introduction.md');
const introductionMarkdownContent = readFileSync(filename);

const filename = join(outputFolder, 'Introduction', '_category_.json');
const introductionContent = `---\ntitle: Introduction\nsidebar_position: -1\n---${introductionMarkdownContent}`;

writeFileSync(filename, JSON.stringify(categoryForApiFolder, null, 2));
writeFileSync(filename, introductionContent);
}

build();
27 changes: 22 additions & 5 deletions scripts/libs/CustomMarkdownDocumenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class CustomMarkdownDocumenter {

const filename: string = path.join(
this._outputFolder,
this._getFilenameForApiItem(apiItem),
this._getFilenameForApiItem(apiItem, true),
);
const stringBuilder: StringBuilder = new StringBuilder();

Expand Down Expand Up @@ -1155,7 +1155,10 @@ export class CustomMarkdownDocumenter {
}
}

private _getFilenameForApiItem(apiItem: ApiItem): string {
private _getFilenameForApiItem(
apiItem: ApiItem,
nestedWhenSameName: boolean = false,
): string {
if (apiItem.kind === ApiItemKind.Model) {
// this file will be ignored, we don't like the old index file.
return 'ignored.md';
Expand Down Expand Up @@ -1184,7 +1187,16 @@ export class CustomMarkdownDocumenter {
const safeName = CustomUtilities.getSafeFilenameForNameWithCase(
apiItem.displayName,
);
const filename = safeName + '.md';

if (breadcrumbs.length === 0) return safeName + '.md';

const filename =
!nestedWhenSameName &&
breadcrumbs[breadcrumbs.length - 1] === safeName
? ''
: safeName + '.md';

if (!filename) return breadcrumbs.join('/') + '.md';

return [...breadcrumbs, filename].join('/');
}
Expand Down Expand Up @@ -1219,13 +1231,18 @@ export class CustomMarkdownDocumenter {
}

// when we don't have name, usually is the package documentation
if (!baseName) return 'Introduction/Introduction.md';
if (!baseName) return 'Introduction.md';

return baseName + '.md';
}

private _getLinkFilenameForApiItem(apiItem: ApiItem): string {
return '/docs/api/' + this._getFilenameForApiItem(apiItem);
return (
'/docs/api/' +
this._getFilenameForApiItem(apiItem)
.replace(/\.md/g, '')
.replace(/ /g, '%20')
);
}

private _deleteOldOutputFiles(): void {
Expand Down

0 comments on commit 3027bda

Please sign in to comment.