Skip to content
This repository was archived by the owner on Feb 13, 2026. It is now read-only.
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
16 changes: 8 additions & 8 deletions src/routes/wordGenerator/wordGeneratorModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@ const ContentSchema = z.object({
});

// Define the base schema for a section
const BaseSectionSchema = z.object({
const SectionSchema = z.object({
sectionId: z.string().openapi({
description: 'A unique identifier for the section.',
}),
heading: z.string().optional().openapi({
description: 'Heading of the section.',
}),
headingLevel: z.number().int().min(1).optional().openapi({
description: 'Level of the heading (e.g., 1 for main heading, 2 for subheading).',
}),
parentSectionId: z.string().optional().openapi({
description:
'The unique identifier of the parent section, if this section is a child of another. Leave empty if this section has no parent.',
}),
content: z.array(ContentSchema).optional().openapi({
description: 'Content contained within the section, including paragraphs, tables, etc.',
}),
});

// Extend the base schema with subSections
const SectionSchema = BaseSectionSchema.extend({
subSections: z.array(BaseSectionSchema).optional().openapi({
description: 'Subsections within the main section.',
}),
});

// Request Body Schema
export const WordGeneratorRequestBodySchema = z.object({
title: z.string().openapi({
Expand Down
35 changes: 34 additions & 1 deletion src/routes/wordGenerator/wordGeneratorRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,36 @@ const generateSectionContent = (section: any, config: any) => {
return sectionContent;
};

// Function to build a hierarchical structure from a flat list of sections
const buildSectionsHierarchy = (sections: any[]) => {
const sectionMap = new Map();

// Create a map of sections by ID
sections.forEach((section) => {
sectionMap.set(section.sectionId, { ...section, subSections: [] });
});

const rootSections: any[] = [];

// Organize sections into a hierarchy
sections.forEach((section) => {
if (section.parentSectionId) {
// If the section has a parent, add it as a subSection
const parent = sectionMap.get(section.parentSectionId);
if (parent) {
parent.subSections.push(sectionMap.get(section.sectionId));
} else {
console.warn(`Parent section with ID ${section.parentSectionId} not found.`);
}
} else {
// If no parent, it's a root section
rootSections.push(sectionMap.get(section.sectionId));
}
});

return rootSections;
};

async function execGenWordFuncs(
data: {
title: string;
Expand Down Expand Up @@ -491,6 +521,9 @@ async function execGenWordFuncs(
);
}

// Build sections hierarchy
const sectionsHierarchy = buildSectionsHierarchy(data.sections);

// Create the document based on JSON data
const doc = new Document({
styles: {
Expand Down Expand Up @@ -533,7 +566,7 @@ async function execGenWordFuncs(
}),
...tableOfContentConfigs,
// Generate all sections and sub-sections
...data.sections.flatMap((section) =>
...sectionsHierarchy.flatMap((section) =>
generateSectionContent(section, { ...config, numberingReference: selectedNumberingOption?.reference })
),
],
Expand Down
Loading