/** * Document Replace API * * Uses the unified walker from core/walker.ts for consistent traversal * across body, headers, footers, footnotes, endnotes, comments, and SDTs. * * Note: This API mutates the document in place for backward compatibility. * * Implementation notes: * We always operate on the paragraph's concatenated plain text and the * ordered list of text-bearing run-content nodes ("text segments"). * This guarantees consistent semantics regardless of whether matches sit * inside a single run or straddle multiple runs: * 1. Build segment table [seg0, seg1, ...] with absolute offsets. * 2. Compute every match using a global regex (string searches are * promoted to a global regex with the literal escaped). * 3. Walk segments and matches together. For each segment we emit: * - the literal (un-matched) prefix/middle/suffix slices, and * - the replacement string the first time we see each match. * Subsequent segments overlapping the same match contribute the empty * string (so the replacement is not duplicated) but otherwise keep * their original formatting (RunProperties remain on the parent run). * The returned count equals the number of matches actually replaced. */ import type { DocxDocument } from "../types.js"; /** * Replace text in a document (mutates in place). * * Performs document-wide text replacement within run content. Matches are * applied uniformly whether they fall inside a single run or span several * runs; the formatting of each run that survives the replacement is preserved. * * Traverses body, headers, footers, footnotes, endnotes, comments, tables, and * SDTs using the unified document walker. SDTs may carry inline runs without * an enclosing paragraph (content controls wrapping a single Run); those * inline runs are treated as a single virtual paragraph for replacement. * * @param doc - The document model to modify (mutated in place). * @param search - String or RegExp to find. Both are treated as global * (every occurrence is replaced); the `g` flag on a RegExp * is therefore optional. * @param replacement - Replacement string. When `search` is a RegExp the * replacement supports the standard `$1`/`$&`/`$$` etc. * backreferences. * @returns The exact number of replacements made. */ export declare function replaceText(doc: DocxDocument, search: string | RegExp, replacement: string): number;