Skip to content

feat(dgeni,docs-utils)!: extract out ToC type #3397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@if (isGuideDoc) {
<daffio-docs-table-of-contents
class="daffio-doc-viewer__table-of-contents"
[tableOfContents]="doc.tableOfContents.json">
[tableOfContents]="doc.tableOfContents">
</daffio-docs-table-of-contents>
}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('DaffioDocsTableOfContentsComponent', () => {
fixture = TestBed.createComponent(DaffioDocsTableOfContentsComponent);
component = fixture.componentInstance;
stubDaffioDoc = new DaffioDocsFactory().create();
component.tableOfContents = stubDaffioDoc.tableOfContents.json;
component.tableOfContents = stubDaffioDoc.tableOfContents;
fixture.detectChanges();
});

Expand All @@ -42,15 +42,15 @@ describe('DaffioDocsTableOfContentsComponent', () => {

it('should render a .daffio-docs-table-of-contents__item for each entry in the table of contents', () => {
const tocItems = fixture.debugElement.queryAll(By.css('.daffio-docs-table-of-contents__item'));
expect(tocItems.length).toEqual(stubDaffioDoc.tableOfContents.json.length);
expect(tocItems.length).toEqual(stubDaffioDoc.tableOfContents.length);
});

it('should label each item with an indent level based on its toc level', () => {
const tocLevel1 = fixture.debugElement.queryAll(By.css('.daffio-docs-table-of-contents__item--level-1'));
const tocLevel2 = fixture.debugElement.queryAll(By.css('.daffio-docs-table-of-contents__item--level-2'));
const tocLevel3 = fixture.debugElement.queryAll(By.css('.daffio-docs-table-of-contents__item--level-3'));
expect(tocLevel1.length).toEqual(stubDaffioDoc.tableOfContents.json.filter(content => content.lvl === 1).length);
expect(tocLevel2.length).toEqual(stubDaffioDoc.tableOfContents.json.filter(content => content.lvl === 2).length);
expect(tocLevel3.length).toEqual(stubDaffioDoc.tableOfContents.json.filter(content => content.lvl === 3).length);
expect(tocLevel1.length).toEqual(stubDaffioDoc.tableOfContents.filter(content => content.lvl === 1).length);
expect(tocLevel2.length).toEqual(stubDaffioDoc.tableOfContents.filter(content => content.lvl === 2).length);
expect(tocLevel3.length).toEqual(stubDaffioDoc.tableOfContents.filter(content => content.lvl === 3).length);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Input,
} from '@angular/core';

import { DaffGuideDoc } from '@daffodil/docs-utils';
import { DaffDocTableOfContents } from '@daffodil/docs-utils';

@Component({
selector: 'daffio-docs-table-of-contents',
Expand All @@ -16,5 +16,5 @@ export class DaffioDocsTableOfContentsComponent {
/**
* The doc to render
*/
@Input() tableOfContents: DaffGuideDoc['tableOfContents']['json'];
@Input() tableOfContents: DaffDocTableOfContents;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('DaffioDocsFactory', () => {
expect(Object.keys(doc)).toContain('id');
expect(Object.keys(doc)).toContain('title');
expect(Object.keys(doc)).toContain('contents');
expect(Object.keys(doc.tableOfContents.json[0])).toContain('content');
expect(Object.keys(doc.tableOfContents.json[0])).toContain('lvl');
expect(Object.keys(doc.tableOfContents.json[0])).toContain('slug');
expect(Object.keys(doc.tableOfContents[0])).toContain('content');
expect(Object.keys(doc.tableOfContents[0])).toContain('lvl');
expect(Object.keys(doc.tableOfContents[0])).toContain('slug');
});
});
16 changes: 7 additions & 9 deletions apps/daffio/src/app/docs/testing/factories/docs.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ export class MockDoc implements DaffGuideDoc {
contents = faker.lorem.paragraph();
// TODO: implement child models
breadcrumbs = [];
tableOfContents = {
json: [
{
content: faker.lorem.paragraph(),
lvl: faker.datatype.number(),
slug: faker.random.alphaNumeric(),
},
],
};
tableOfContents = [
{
content: faker.lorem.paragraph(),
lvl: faker.datatype.number(),
slug: faker.random.alphaNumeric(),
},
];
};

@Injectable({
Expand Down
9 changes: 2 additions & 7 deletions libs/docs-utils/src/doc/guide.type.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { DaffDocTableOfContents } from './toc.type';
import { DaffDoc } from './type';

/**
* A guide doc. Includes a table of contents.
*/
export interface DaffGuideDoc extends DaffDoc {
tableOfContents: {
json: Array<{
content: string;
lvl: number;
slug: string;
}>;
};
tableOfContents: DaffDocTableOfContents;
}
1 change: 1 addition & 0 deletions libs/docs-utils/src/doc/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './api.type';
export * from './example.type';
export * from './guide.type';
export * from './toc.type';
export * from './type';
7 changes: 7 additions & 0 deletions libs/docs-utils/src/doc/toc.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface DaffDocTableOfContentsEntry {
content: string;
lvl: number;
slug: string;
}

export type DaffDocTableOfContents = Array<DaffDocTableOfContentsEntry>;
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function guideFileReaderFactory() {
getDocs: (fileInfo) => fileInfo.content ? [{
docType: 'guide',
title: extractTitle(fileInfo),
tableOfContents: toc(fileInfo.content),
tableOfContents: toc(fileInfo.content).json,
content: fileInfo.content,
}] : [],
};
Expand Down
Loading