set_hwp_paragraph_text
Replace the text of a specified paragraph in an .hwpx document while preserving its formatting. Collapses existing runs into a single run with the new text.
Instructions
Replace the entire text of the Nth paragraph (0-based) in an .hwpx body with new text. The paragraph attributes (paraPr/style refs) are preserved; runs are collapsed into a single hp:run with the new text. Args: file_path, index, text, output_path (optional).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| index | Yes | ||
| text | Yes | ||
| output_path | No |
Implementation Reference
- src/tools/edit.ts:165-178 (handler)The tool handler function 'setHwpParagraphText' that executes the logic for 'set_hwp_paragraph_text'. It validates input, delegates to the core mutator, and returns a result string.
export async function setHwpParagraphText(args: SetParaTextArgs): Promise<string> { const err = preflight(args.file_path); if (err) return err; const out = args.output_path && args.output_path.length > 0 ? args.output_path : defaultOutput(args.file_path, "para-set"); try { const r = await setHwpxParagraphText(args.file_path, out, args.index, args.text); if (r.replaced === 0) return `인덱스 범위 초과 (index out of range): ${args.index} (전체 ${r.total})`; return `문단 ${args.index} 텍스트 설정 (paragraph text set)\n저장 (saved): ${out}`; } catch (e) { return `문단 텍스트 설정 오류 (set paragraph text error): ${(e as Error).message}`; } } - src/tools/edit.ts:158-163 (schema)The TypeScript interface 'SetParaTextArgs' defining the input schema for the tool: file_path, index, text, and optional output_path.
export interface SetParaTextArgs { file_path: string; index: number; text: string; output_path?: string; } - src/core/hwpx-mutate.ts:333-359 (helper)The core implementation 'setHwpxParagraphText' that performs the actual XML mutation: loads the HWPX zip, finds the Nth <hp:p> paragraph, collapses its runs into a single <hp:run> with the new text, preserves character formatting via charPrIDRef, and writes back.
export async function setHwpxParagraphText( inputPath: string, outputPath: string, index: number, text: string ): Promise<{ replaced: number; total: number }> { const { zip, sectionName, xml } = await loadSection(inputPath); const matches = [...xml.matchAll(PARA_REGEX)]; if (index < 0 || index >= matches.length) { return { replaced: 0, total: matches.length }; } const para = matches[index][0]; // Replace every <hp:t>...</hp:t> with one carrying the new text; // collapse to a single <hp:run><hp:t>NEW</hp:t></hp:run> body inside the // paragraph wrapper to avoid duplicating runs. const open = para.match(/^<hp:p [^>]*>/)?.[0] ?? "<hp:p>"; const close = "</hp:p>"; const charPrMatch = para.match(/<hp:run [^>]*charPrIDRef="(\d+)"/); const charPrId = charPrMatch ? charPrMatch[1] : "0"; const newPara = open + `<hp:run charPrIDRef="${charPrId}"><hp:t>${xmlEscape(text)}</hp:t></hp:run>` + close; const newXml = xml.replace(para, newPara); await writeSection(zip, sectionName, newXml, outputPath); return { replaced: 1, total: matches.length }; } - src/server.ts:275-289 (registration)Tool registration entry in the TOOLS array: defines name 'set_hwp_paragraph_text', description, and input schema (file_path, index, text, output_path).
{ name: "set_hwp_paragraph_text", description: "Replace the entire text of the Nth paragraph (0-based) in an .hwpx body with new text. The paragraph attributes (paraPr/style refs) are preserved; runs are collapsed into a single <hp:run> with the new text. Args: file_path, index, text, output_path (optional).", inputSchema: { type: "object", properties: { file_path: { type: "string" }, index: { type: "number" }, text: { type: "string" }, output_path: { type: "string" }, }, required: ["file_path", "index", "text"], }, }, - src/server.ts:538-538 (registration)Handler mapping in the HANDLERS record linking the tool name 'set_hwp_paragraph_text' to the imported 'setHwpParagraphText' function.
set_hwp_paragraph_text: setHwpParagraphText,