Skip to content

Commit

Permalink
feat(dgeni,docs-utils)!: extract out ToC type (#3397)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the ToC types have been trimmed up to only have fields used
  • Loading branch information
griest024 authored Jan 7, 2025
1 parent bd26e3d commit 0db48b5
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 28 deletions.
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 @@ -15,15 +15,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

0 comments on commit 0db48b5

Please sign in to comment.