Skip to content

Commit 362142e

Browse files
committed
Merge branch 'main' into excel-generator
2 parents 4054b41 + 473534d commit 362142e

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/routes/wordGenerator/wordGeneratorModel.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,25 @@ const ContentSchema = z.object({
4545
});
4646

4747
// Define the base schema for a section
48-
const BaseSectionSchema = z.object({
48+
const SectionSchema = z.object({
49+
sectionId: z.string().openapi({
50+
description: 'A unique identifier for the section.',
51+
}),
4952
heading: z.string().optional().openapi({
5053
description: 'Heading of the section.',
5154
}),
5255
headingLevel: z.number().int().min(1).optional().openapi({
5356
description: 'Level of the heading (e.g., 1 for main heading, 2 for subheading).',
5457
}),
58+
parentSectionId: z.string().optional().openapi({
59+
description:
60+
'The unique identifier of the parent section, if this section is a child of another. Leave empty if this section has no parent.',
61+
}),
5562
content: z.array(ContentSchema).optional().openapi({
5663
description: 'Content contained within the section, including paragraphs, tables, etc.',
5764
}),
5865
});
5966

60-
// Extend the base schema with subSections
61-
const SectionSchema = BaseSectionSchema.extend({
62-
subSections: z.array(BaseSectionSchema).optional().openapi({
63-
description: 'Subsections within the main section.',
64-
}),
65-
});
66-
6767
// Request Body Schema
6868
export const WordGeneratorRequestBodySchema = z.object({
6969
title: z.string().openapi({

src/routes/wordGenerator/wordGeneratorRouter.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,36 @@ const generateSectionContent = (section: any, config: any) => {
393393
return sectionContent;
394394
};
395395

396+
// Function to build a hierarchical structure from a flat list of sections
397+
const buildSectionsHierarchy = (sections: any[]) => {
398+
const sectionMap = new Map();
399+
400+
// Create a map of sections by ID
401+
sections.forEach((section) => {
402+
sectionMap.set(section.sectionId, { ...section, subSections: [] });
403+
});
404+
405+
const rootSections: any[] = [];
406+
407+
// Organize sections into a hierarchy
408+
sections.forEach((section) => {
409+
if (section.parentSectionId) {
410+
// If the section has a parent, add it as a subSection
411+
const parent = sectionMap.get(section.parentSectionId);
412+
if (parent) {
413+
parent.subSections.push(sectionMap.get(section.sectionId));
414+
} else {
415+
console.warn(`Parent section with ID ${section.parentSectionId} not found.`);
416+
}
417+
} else {
418+
// If no parent, it's a root section
419+
rootSections.push(sectionMap.get(section.sectionId));
420+
}
421+
});
422+
423+
return rootSections;
424+
};
425+
396426
async function execGenWordFuncs(
397427
data: {
398428
title: string;
@@ -491,6 +521,9 @@ async function execGenWordFuncs(
491521
);
492522
}
493523

524+
// Build sections hierarchy
525+
const sectionsHierarchy = buildSectionsHierarchy(data.sections);
526+
494527
// Create the document based on JSON data
495528
const doc = new Document({
496529
styles: {
@@ -533,7 +566,7 @@ async function execGenWordFuncs(
533566
}),
534567
...tableOfContentConfigs,
535568
// Generate all sections and sub-sections
536-
...data.sections.flatMap((section) =>
569+
...sectionsHierarchy.flatMap((section) =>
537570
generateSectionContent(section, { ...config, numberingReference: selectedNumberingOption?.reference })
538571
),
539572
],

0 commit comments

Comments
 (0)