Skip to content

fix: persist header/footer images on save (fixes #251)#264

Merged
jedrazb merged 2 commits intomainfrom
fix/header-image-rezip-251
Apr 16, 2026
Merged

fix: persist header/footer images on save (fixes #251)#264
jedrazb merged 2 commits intomainfrom
fix/header-image-rezip-251

Conversation

@jedrazb
Copy link
Copy Markdown
Contributor

@jedrazb jedrazb commented Apr 15, 2026

Summary

  • Images inserted into a header or footer were silently dropped on save. collectNewImages only walked the document body, so the image binary never landed in word/media/ and no relationship was added to the header/footer's rels file — Word then showed "cannot display image".
  • Refactor image + hyperlink processing in rezip.ts to be part-aware: enumerate the body and every header/footer as Parts with their own rels file, share the word/media/ image counter (package-wide namespace), and allocate rIds per rels file. Creates a minimal rels file on demand for a header/footer that had no prior relationships.
  • Same fix applied to hyperlinks in headers/footers (same latent bug).

Test plan

  • New unit test packages/core/src/docx/__tests__/header-image-repack.test.ts — inserts an image into a real header (fixture: titlePg-header-footer.docx), repacks, and asserts: binary in word/media/, relationship in word/_rels/header*.xml.rels, no stray entry in document.xml.rels, r:embed wired up in the header XML, png registered in [Content_Types].xml.
  • Symmetric footer test (same flow on the first footer).
  • bun run typecheck — clean.
  • Existing bun test packages/core/src/docx/__tests__/ — 39/39 pass, no regressions.
  • Manual: pasted an image into a header, saved, confirmed word/media/imageN.png + matching relationship in word/_rels/header*.xml.rels.

Follow-up issues surfaced by manual testing

These are pre-existing bugs in the display/edit layers (not the save path this PR fixes). Filed for separate fixes — they need live DOM inspection to root-cause:

Fixes #251.

🤖 Generated with Claude Code

Images inserted into headers or footers were silently dropped on save:
`collectNewImages` only walked the document body, so the image binary
was never written to `word/media/` and no relationship was added to
the header/footer's rels file. Word then showed "cannot display image".

Refactor image + hyperlink processing to be part-aware: enumerate the
body and every header/footer as parts with their own rels file, share
the `word/media/` image counter across parts (it's a package-wide
namespace), and allocate rIds per rels file.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docx-editor Ready Ready Preview, Comment Apr 15, 2026 3:52pm

Request Review

- readRelsOrStub: normalize `<Relationships .../>` (Word's empty form)
  to `<Relationships ...></Relationships>` so `.replace` appends correctly.
  Without this, footers with no prior relationships silently dropped
  newly inserted images.
- Add symmetric footer image round-trip test. Caught the bug above.
- Use headerFooterFilename() helper in collectHeaderFooterUpdates
  instead of duplicating the prefix/slice logic.
- Extract FIXTURE_PATH + loadFixture() to deduplicate test setup.
- Remove one narrating comment.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jedrazb jedrazb merged commit 6c08395 into main Apr 16, 2026
4 checks passed
@jedrazb jedrazb deleted the fix/header-image-rezip-251 branch April 16, 2026 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Images in Header not displaying

1 participant