Skip to content

Commit

Permalink
feat: ✨ add sub sections to section options
Browse files Browse the repository at this point in the history
  • Loading branch information
AllyMurray committed Dec 10, 2023
1 parent b5a93ff commit 9977226
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/sections/section.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
function createTestSection(options: DocumentSectionOptions) {
class TestSection extends DocumentSection {
protected synthesizeContent(): Array<string> {
return ['Test Section'];
return [options.title, ...this.subSections.map((s) => s.synthesize())];
}
}

Expand Down Expand Up @@ -87,4 +87,32 @@ describe('tryFindSection', () => {
tryFindSection([testSection], ['Test Section', 'Sub Section']),
).toStrictEqual(subSection);
});

it('should add single sub section passed in options', () => {
const testSection = createTestSection({
title: 'Test Section',
subSections: [createTestSection({ title: 'Sub Section' })],
});

expect(testSection.synthesize()).toMatchInlineSnapshot(`
"Test Section
Sub Section"
`);
});

it('should add multiple sub sections passed in options', () => {
const testSection = createTestSection({
title: 'Test Section',
subSections: [
createTestSection({ title: 'Sub Section 1' }),
createTestSection({ title: 'Sub Section 2' }),
],
});

expect(testSection.synthesize()).toMatchInlineSnapshot(`
"Test Section
Sub Section 1
Sub Section 2"
`);
});
});
2 changes: 2 additions & 0 deletions src/sections/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HeadingLevel } from '../elements/heading.js';
export interface DocumentSectionOptions {
title: string;
parent?: DocumentSection;
subSections?: Array<DocumentSection>;
}

/**
Expand All @@ -19,6 +20,7 @@ export abstract class DocumentSection {
this.title = options.title;
this.parent = options.parent;
this.parent?._subSections.push(this);
options.subSections?.forEach((section) => this.addSubSection(section));
}

get subSections(): ReadonlyArray<DocumentSection> {
Expand Down

0 comments on commit 9977226

Please sign in to comment.