Skip to content

Scan bookmarks in headers and footers (original idea by @bramj in #22)#176

Merged
satoryu merged 1 commit into
masterfrom
add-bookmarks-in-headers-footers
May 31, 2026
Merged

Scan bookmarks in headers and footers (original idea by @bramj in #22)#176
satoryu merged 1 commit into
masterfrom
add-bookmarks-in-headers-footers

Conversation

@satoryu
Copy link
Copy Markdown
Member

@satoryu satoryu commented May 31, 2026

Summary

Extends Document#bookmarks to also scan headers and footers. Until now bookmarks were only collected from the document body (document.xml), so any bookmark placed in a header or footer was invisible to the API and could not be edited.

doc = Docx::Document.open("template.docx")

# previously: only body bookmarks; header/footer bookmarks were missing
doc.bookmarks["header_bookmark"].insert_text_after("ACME Corp ")
doc.bookmarks["footer_bookmark"].insert_text_after("Page footer ")
doc.save("filled.docx")   # edits persist (see #175)

Why this works

The existing Bookmark insertion API (insert_text_before / insert_text_after / insert_multiple_lines) is not body-specific — it operates on the bookmark's own Nokogiri node and creates new nodes in that node's document (element.node.document). Since #173 / #174 expose headers/footers as Nokogiri documents and #175 writes them back on save, scanning their bookmarks is enough to make header/footer bookmark editing fully round-trip. Verified end to end with a save/reopen test.

Changes

  • bookmarks now appends w:bookmarkStart nodes found in headers.values and footers.values (keyed by name, same as the body). _GoBack filtering and name-keyed Hash semantics are unchanged.
  • Adds fixture spec/fixtures/multi_doc_bookmarks.docx (a header_bookmark in the header and a footer_bookmark in the footer).
  • Adds specs: scanning finds header/footer bookmarks, text insertion into both, and a save/reopen round-trip.

Note on behavior change

bookmarks now returns header/footer bookmarks in addition to body ones. Word bookmark names are document-unique, so collisions are unlikely; if a name does collide, last-write-wins in the order body → headers → footers. This is effectively a completeness fix.

Credit

Based on the original header-bookmark idea by @bramj in #22, extended here to also cover footers. They are credited as a co-author on the commit and should be credited in the release notes when this ships.

Tests

  • Full suite green locally (145 examples, 0 failures).

Closes #22

🤖 Generated with Claude Code

Document#bookmarks previously only scanned the document body, so bookmarks
placed in headers/footers were invisible and couldn't be edited. This extends
the scan to headers and footers (keyed by name, like the body). Combined with
header/footer write-back (#175), the existing Bookmark insertion API now works
for headers and footers and persists on save.

Based on the original header-bookmark idea by @bramj in #22 (extended here to
cover footers as well).

Co-authored-by: bramj <bramj@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@satoryu satoryu mentioned this pull request May 31, 2026
@satoryu satoryu merged commit e2f8745 into master May 31, 2026
6 checks passed
@satoryu satoryu deleted the add-bookmarks-in-headers-footers branch May 31, 2026 11:44
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.

1 participant