Patching official Word templates
TiVA has to export into official Word templates. Rebuilding those templates from scratch would be a mistake: they are too complex, too specific, and too likely to change.
The better approach is patching. TiVA keeps the authority template and inserts content from its own editor format into the right places.
The core of that exporter is a complex recursive function that turns TiVA’s custom TipTap-based HTML into Word’s document structure. It grew to handle edge cases the editor produces freely but Word only accepts in very specific shapes.
The difficult part is that Word documents can become damaged in ways that only appear when the file is opened in Word. Lists, nested tables, empty elements, and library edge cases all had to be isolated by experiment.
The most useful debugging method was binary search over form fields: remove half the patched fields, open the document, repeat until the damaging construct is found. Over time this produced a practical map of what Word tolerates and which replacement procedures TiVA needs to use.