Skip to content

SD-2440 - fix: toc not loading on paragraphs#2706

Merged
caio-pizzol merged 11 commits intomainfrom
gabriel/sd-2440-bug-import-fails-on-field-based-list-of-tablesfigures-toc
Apr 9, 2026
Merged

SD-2440 - fix: toc not loading on paragraphs#2706
caio-pizzol merged 11 commits intomainfrom
gabriel/sd-2440-bug-import-fails-on-field-based-list-of-tablesfigures-toc

Conversation

@chittolinag
Copy link
Copy Markdown
Contributor

@chittolinag chittolinag commented Apr 2, 2026

Issue

Importing documents where the TOC field sits inline inside a paragraph crashed because the converter always emitted block tableOfContents nodes that expect paragraph parents. When that type of document is imported, SuperDoc crashes completely.

Solution

Detect when the parent disallows block nodes and encode the TOC within a paragraph block.

@linear
Copy link
Copy Markdown

linear bot commented Apr 2, 2026

@chittolinag chittolinag changed the title fix: toc not loading on paragraphs SD-2440 - fix: toc not loading on paragraphs Apr 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

Status: PASS

The OOXML field character handling in this PR is spec-compliant.

The inline TOC decodes to the standard complex field structure defined in ECMA-376 §17.16.2:

w:fldChar (begin) → w:instrText (preserve) → w:fldChar (separate) → [content] → w:fldChar (end)

All three pieces check out:

  • w:fldCharType values begin, separate, end are the exact three valid values per §17.16.18
  • xml:space="preserve" on w:instrText matches the spec's own examples (§17.16.23) — preserving whitespace in field codes like TOC \h is correct
  • Field sequence is valid — separate is optional per spec, but including it (as the code does) is correct when there's result content following it

The only difference from the block variant is that the runs are returned as a flat array for injection into an existing paragraph rather than being wrapped in w:p elements. That's the right behavior for inline fields — the surrounding paragraph context is owned by the parent, not this node.

@chittolinag chittolinag marked this pull request as ready for review April 2, 2026 19:03
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ac0874bd59

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copy link
Copy Markdown
Contributor

@caio-pizzol caio-pizzol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chittolinag tested with the original bug file + 13 other Word files. no crashes.

block TOCs decode children twice and the second pass can lose data — fix in the inline comment.

tableOfContentsInline was never triggered during testing, even with the original file. if no file produces it, the wrapping logic alone is the fix and the inline node type can be removed.

if (!exported) return [];
return Array.isArray(exported) ? exported : [exported];
});
const blockContentNodes = isInlineNode ? [] : tocContent.map((n) => exportSchemaToJson({ ...params, node: n }));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two things:

  1. block TOCs decode every child twice — once for inlineContentNodes (thrown away) and once for blockContentNodes (kept). some decoders modify data as they go, so the second pass sees corrupted input. hyperlinks can get dropped. fix:
Suggested change
const blockContentNodes = isInlineNode ? [] : tocContent.map((n) => exportSchemaToJson({ ...params, node: n }));
const inlineContentNodes = isInlineNode
? tocContent.flatMap((n) => {
const exported = exportSchemaToJson({ ...params, node: n });
if (!exported) return [];
return Array.isArray(exported) ? exported : [exported];
})
: [];
const blockContentNodes = isInlineNode ? [] : tocContent.map((n) => exportSchemaToJson({ ...params, node: n }));
  1. loaded the original Template_Test_Report.docx with debug logs — tableOfContentsInline was never produced. the paragraph wrapping at lines 42-49 is what prevents the crash. do you have a file that actually triggers the inline path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catches. just fixed the 1st issue. I am now checking the 2nd issue you mentioned, it seems like there's also a problem when exporting the file (the opening in Word). I'll fix it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've been trying to fix the export issue for a while now but no luck. I am pretty sure the export issue is unrelated though - after removing the TOC & re-exporting the document, the error persists.

If you agree, I'll file a separate ticket to handle it, but in the mean time I'll make sure to fix the issues you mentioned here in the review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ✅

chittolinag and others added 2 commits April 8, 2026 14:41
Co-authored-by: Caio Pizzol <97641911+caio-pizzol@users.noreply.github.com>
@chittolinag chittolinag requested a review from caio-pizzol April 9, 2026 15:54
Copy link
Copy Markdown
Contributor

@caiopizzol caiopizzol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chittolinag fix works, tested with the original file — document loads fine. nice simplification on the latest push.

added a few tests and uploaded the bug file to the test corpus so it's covered going forward.

@caio-pizzol caio-pizzol added this pull request to the merge queue Apr 9, 2026
Merged via the queue into main with commit 90def7f Apr 9, 2026
54 checks passed
@caio-pizzol caio-pizzol deleted the gabriel/sd-2440-bug-import-fails-on-field-based-list-of-tablesfigures-toc branch April 9, 2026 18:08
@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 9, 2026

🎉 This PR is included in vscode-ext v1.1.0-next.79

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 9, 2026

🎉 This PR is included in @superdoc-dev/react v1.0.0-next.33

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 9, 2026

🎉 This PR is included in esign v2.2.0-next.37

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 9, 2026

🎉 This PR is included in template-builder v1.3.0-next.39

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 9, 2026

🎉 This PR is included in superdoc-cli v0.5.0-next.77

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 9, 2026

🎉 This PR is included in superdoc v1.24.0-next.76

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 9, 2026

🎉 This PR is included in superdoc-sdk v1.3.0-next.78

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 10, 2026

🎉 This PR is included in superdoc-sdk v1.4.0

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 10, 2026

🎉 This PR is included in superdoc v1.25.0

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 10, 2026

🎉 This PR is included in superdoc-cli v0.6.0

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 10, 2026

🎉 This PR is included in vscode-ext v2.3.0-next.1

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 10, 2026

🎉 This PR is included in template-builder v1.5.0-next.1

The release is available on GitHub release

@superdoc-bot
Copy link
Copy Markdown
Contributor

superdoc-bot bot commented Apr 10, 2026

🎉 This PR is included in esign v2.3.0-next.1

The release is available on GitHub release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants