A standard help authoring tool such as HelpNDoc is a software where you generally write and organize content in order to produce documentation files for the end-user. As revisions are needed, the content needs to stay organized, and it usually involves a lot of laborious and error-prone tasks such as copying / pasting content all over the project, deleting content, moving and merging topics… Fortunately, HelpNDoc includes a powerful scripting processor which can help automate documentation creation, maintenance, reorganization… Let’s see how we can leverage HelpNDoc’s scripting capabilities to merge multiple children topics into a parent topic.
Using HelpNDoc’s scripting capabilities
HelpNDoc’s bundled script editor can be used to create and run custom scripts to automate various aspects of HelpNDoc.

The purpose of this script is to merge the content of children topics into a parent topic. This means that the script needs to:
- Check that a topic is currently selected in HelpNDoc’s table of content. This will be the parent topic;
- Create a temporary editor where the content of all the topics will be placed
- Place the content of the parent topic into the temporary editor and get a list of its children topics
- Iterate through all children topics in order, and place the content in the temporary editor
- Place the content of the temporary editor into the parent topic
- Delete all children of the parent topic
Warning: as some scripting actions are not reversible, we always recommend that you backup your HelpNDoc project before running a script in case of an error forcing you to go back to an earlier version.
1. Retrieve the parent topic
The currently selected topic in HelpNDoc’s user interface is the parent topic. We need to make sure that a topic is selected, and get its ID:
// Get the currently selected topic
aParentTopicId := HndUi.GetCurrentTopic();
// Make sure that we have a valid selection
if aParentTopicId = '' then
Exit;
2. Create a temporary editor
The temporary editor is a placeholder for the content of all merged topics. Here is how we can create and free that editor:
// We need a temporary editor
oEditor := HndEditor.CreateTemporaryEditor();
try
// Here we can insert content into oEditor
finally
oEditor.Free;
end;
3. Insert the parent topic’s content and get its children
We can now place the parent topic’s content in the topic editor and get a list of its direct children, meaning that only first level children are placed in the list.
// Add the parent topic's content
HndEditor.InsertTopicContent(oEditor, aParentTopicId);
// Get a list of direct children
aFirstLevelChildren := HndTopics.GetTopicDirectChildrenList(aParentTopicId);
4. Iterate through all children topics recursively and add their content
A recursive method can be created to handle children, grand-children and so on… in the right order. We can then call this method for the parent topic’s direct children list:
// Recursive method
procedure DoInsertChildrenContent(aList: THndTopicsInfoArray; anEditor: TObject);
var
nTopic: Integer;
begin
// Iterate through children
for nTopic := 0 to High(aList) do
begin
// Insert content
HndEditor.InsertTopicContent(anEditor, aList[nTopic].Id);
// Handle children
DoInsertChildrenContent(
HndTopics.GetTopicDirectChildrenList(aList[nTopic].Id),
anEditor
);
end;
end;
// Add parent topic's children content
DoInsertChildrenContent(aFirstLevelChildren, oEditor);
5. Replace the parent topic’s content with the temporary editor’s content
The merged content is now ready. We can now replace the parent topic’s content.
// Set the final content to the initial topic
HndEditor.SetAsTopicContent(oEditor, aParentTopicId);
6. Delete children topics
The content from the children topics is now merged in the parent topic. We can finally delete all those children topics as they are not needed anymore.
// Delete children topics
for nTopic := 0 to High(aFirstLevelChildren) do
begin
HndTopics.DeleteTopic(aFirstLevelChildren[nTopic].Id);
end;
The whole script and going forward
The whole script is available below and will be part of an upcoming release of HelpNDoc.
procedure DoInsertChildrenContent(aList: THndTopicsInfoArray; anEditor: TObject);var
nTopic:Integer;
begin
// Iterate through children
for nTopic := 0 to High(aList) do
begin
// Insert content
HndEditor.InsertTopicContent(anEditor, aList[nTopic].Id);
// Handle children
DoInsertChildrenContent(
HndTopics.GetTopicDirectChildrenList(aList[nTopic].Id),
anEditor
);
end;
end;
var aParentTopicId: string;
var aFirstLevelChildren: THndTopicsInfoArray;
var aCurrentTopicId: string;
var oEditor: TObject;
var nTopic: Integer;
begin
// Get the currently selected topic
aParentTopicId := HndUi.GetCurrentTopic();
// Make sure that we have a valid selection
if aParentTopicId = '' then
Exit;
// We need a temporary editor
oEditor := HndEditor.CreateTemporaryEditor();
try
// Add the parent topic's content
HndEditor.InsertTopicContent(oEditor, aParentTopicId);
// Get a list of direct children
aFirstLevelChildren := HndTopics.GetTopicDirectChildrenList(aParentTopicId);
// Handle them
DoInsertChildrenContent(aFirstLevelChildren, oEditor);
// Set the final content to the initial topic
HndEditor.SetAsTopicContent(oEditor, aParentTopicId);
// Delete children topics
for nTopic := 0 to High(aFirstLevelChildren) do
begin
HndTopics.DeleteTopic(aFirstLevelChildren[nTopic].Id);
end;
// Current topic is not automatically updated: focus the project topic to change the selection
HndUi.SetCurrentTopic(HndTopics.GetProjectTopic);
finally
oEditor.Free;
end;
end.
More than a straightforward help authoring software, HelpNDoc includes various advanced tools to simplify and speed up the documentation creation process, such as its amazing script editor and the various API it can access to. Feel free to download your free version of HelpNDoc now to check it out and play with the various API methods available.
See also...
![Converting Legacy WinHelp HLP to Modern Documentation [converting] [Featured]](/news-and-articles/2023-10-31-revitalize-your-help-files-converting-legacy-winhelp-hlp-to-modern-documentation-with-helpndoc/images/_huebbfad1edfa8e198fe68a27ac6a615ca_422230_8d1bdb26d0c0e24a9ba8baded3ca5c23.jpg)
Revitalize Your Help Files: Converting Legacy WinHelp HLP to Modern Documentation with HelpNDoc
In an age where information is consumed on a plethora of devices and platforms, the classic WinHelp HLP files — once the standard in Windows-based help documentation — are now relics of a bygone era. …
Read More →![HelpNDoc vs WordPress [versus] [Featured]](/news-and-articles/2023-08-15-mastering-multi-channel-publishing-why-helpndoc-leaves-wordpress-in-the-dust/images/helpndoc-vs-wordpress_hu0bdadae9c10011a35eade903242deaee_146355_500x280_fit_box_3.png)
Mastering Multi-Channel Publishing: Why HelpNDoc Leaves WordPress in the Dust
In today’s interconnected digital world, the concept of multi-channel publishing has become more critical than ever. As audiences seek information through various platforms and formats, content …
Read More →![HelpNDoc's Generate documentation dialog [generate] [Featured]](/news-and-articles/2023-06-27-empowering-technical-writers-harnessing-helpndocs-override-features-for-unlimited-documentation-variations/images/styles-overrides_hufaff65b6a789a27d1888f2d6d9bce16a_51000_500x280_fit_box_3.png)
Empowering Technical Writers: Harnessing HelpNDoc's Override Features for Unlimited Documentation Variations
In the ever-evolving landscape of technical writing, the need for flexibility, customization, and efficiency is more crucial than ever before. As technical writers, we often juggle multiple …
Read More →The Future of Documentation: How Help Authoring Tools are Empowering Technical Writers and Organizations
The documentation landscape is constantly evolving, and HelpNDoc has emerged as a leading help authoring tool that’s changing the way we create, maintain, and share information. With its ability to …
Read More →