From 0db48b5e7b519c2ed90a8e22a2a5197460c876c6 Mon Sep 17 00:00:00 2001 From: griest024 Date: Tue, 7 Jan 2025 17:08:24 -0500 Subject: [PATCH] feat(dgeni,docs-utils)!: extract out ToC type (#3397) BREAKING CHANGE: the ToC types have been trimmed up to only have fields used --- .../doc-viewer/doc-viewer.component.html | 2 +- .../table-of-contents.component.spec.ts | 10 +++++----- .../table-of-contents.component.ts | 4 ++-- .../docs/testing/factories/docs.factory.spec.ts | 6 +++--- .../app/docs/testing/factories/docs.factory.ts | 16 +++++++--------- libs/docs-utils/src/doc/guide.type.ts | 9 ++------- libs/docs-utils/src/doc/public_api.ts | 1 + libs/docs-utils/src/doc/toc.type.ts | 7 +++++++ .../reader/guide-file.reader.ts | 2 +- 9 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 libs/docs-utils/src/doc/toc.type.ts diff --git a/apps/daffio/src/app/docs/components/doc-viewer/doc-viewer.component.html b/apps/daffio/src/app/docs/components/doc-viewer/doc-viewer.component.html index 06df339fa7..afdc7c0fc9 100644 --- a/apps/daffio/src/app/docs/components/doc-viewer/doc-viewer.component.html +++ b/apps/daffio/src/app/docs/components/doc-viewer/doc-viewer.component.html @@ -31,7 +31,7 @@ @if (isGuideDoc) { + [tableOfContents]="doc.tableOfContents"> } diff --git a/apps/daffio/src/app/docs/components/table-of-contents/table-of-contents.component.spec.ts b/apps/daffio/src/app/docs/components/table-of-contents/table-of-contents.component.spec.ts index 6e0ece5f97..534601d4db 100644 --- a/apps/daffio/src/app/docs/components/table-of-contents/table-of-contents.component.spec.ts +++ b/apps/daffio/src/app/docs/components/table-of-contents/table-of-contents.component.spec.ts @@ -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(); }); @@ -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); }); }); diff --git a/apps/daffio/src/app/docs/components/table-of-contents/table-of-contents.component.ts b/apps/daffio/src/app/docs/components/table-of-contents/table-of-contents.component.ts index 7e7fb04ada..8a28e53b48 100644 --- a/apps/daffio/src/app/docs/components/table-of-contents/table-of-contents.component.ts +++ b/apps/daffio/src/app/docs/components/table-of-contents/table-of-contents.component.ts @@ -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', @@ -16,5 +16,5 @@ export class DaffioDocsTableOfContentsComponent { /** * The doc to render */ - @Input() tableOfContents: DaffGuideDoc['tableOfContents']['json']; + @Input() tableOfContents: DaffDocTableOfContents; } diff --git a/apps/daffio/src/app/docs/testing/factories/docs.factory.spec.ts b/apps/daffio/src/app/docs/testing/factories/docs.factory.spec.ts index 8a3e2e614f..9f58321c0a 100644 --- a/apps/daffio/src/app/docs/testing/factories/docs.factory.spec.ts +++ b/apps/daffio/src/app/docs/testing/factories/docs.factory.spec.ts @@ -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'); }); }); diff --git a/apps/daffio/src/app/docs/testing/factories/docs.factory.ts b/apps/daffio/src/app/docs/testing/factories/docs.factory.ts index 067ec9ea8a..09dcdd8d38 100644 --- a/apps/daffio/src/app/docs/testing/factories/docs.factory.ts +++ b/apps/daffio/src/app/docs/testing/factories/docs.factory.ts @@ -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({ diff --git a/libs/docs-utils/src/doc/guide.type.ts b/libs/docs-utils/src/doc/guide.type.ts index 2878ed8b81..937965c6e8 100644 --- a/libs/docs-utils/src/doc/guide.type.ts +++ b/libs/docs-utils/src/doc/guide.type.ts @@ -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; } diff --git a/libs/docs-utils/src/doc/public_api.ts b/libs/docs-utils/src/doc/public_api.ts index 5c67cdf4ac..c37e285cb4 100644 --- a/libs/docs-utils/src/doc/public_api.ts +++ b/libs/docs-utils/src/doc/public_api.ts @@ -1,4 +1,5 @@ export * from './api.type'; export * from './example.type'; export * from './guide.type'; +export * from './toc.type'; export * from './type'; diff --git a/libs/docs-utils/src/doc/toc.type.ts b/libs/docs-utils/src/doc/toc.type.ts new file mode 100644 index 0000000000..1079a91f19 --- /dev/null +++ b/libs/docs-utils/src/doc/toc.type.ts @@ -0,0 +1,7 @@ +export interface DaffDocTableOfContentsEntry { + content: string; + lvl: number; + slug: string; +} + +export type DaffDocTableOfContents = Array; diff --git a/tools/dgeni/src/transforms/daffodil-guides-package/reader/guide-file.reader.ts b/tools/dgeni/src/transforms/daffodil-guides-package/reader/guide-file.reader.ts index 11c8701c42..84d37b8cda 100644 --- a/tools/dgeni/src/transforms/daffodil-guides-package/reader/guide-file.reader.ts +++ b/tools/dgeni/src/transforms/daffodil-guides-package/reader/guide-file.reader.ts @@ -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, }] : [], };