diff --git a/docx-core/src/documents/elements/instr_toc.rs b/docx-core/src/documents/elements/instr_toc.rs index 9fbb015b..c1e7d348 100644 --- a/docx-core/src/documents/elements/instr_toc.rs +++ b/docx-core/src/documents/elements/instr_toc.rs @@ -485,4 +485,18 @@ mod tests { .hyperlink() ); } + + #[test] + fn with_instr_text3() { + let s = r#"TOC \f abc \h \z \u"#; + let i = InstrToC::with_instr_text(s); + assert_eq!( + i, + InstrToC::new() + .tc_field_identifier(Some("abc".to_string())) + .use_applied_paragraph_line_level() + .hide_tab_and_page_numbers_in_webview() + .hyperlink() + ); + } } diff --git a/docx-wasm/js/run.ts b/docx-wasm/js/run.ts index 2a60e05f..bbe5ab59 100644 --- a/docx-wasm/js/run.ts +++ b/docx-wasm/js/run.ts @@ -192,7 +192,12 @@ export class Run { } run = run.add_image(pic); } else if (child instanceof Tc) { - run = run.add_tc(child._text, child._omitPageNumber, child._level); + run = run.add_tc( + child._text, + child._omitPageNumber, + child._level, + child._identifier + ); } }); diff --git a/docx-wasm/js/tc.ts b/docx-wasm/js/tc.ts index be8436e6..f4238dc2 100644 --- a/docx-wasm/js/tc.ts +++ b/docx-wasm/js/tc.ts @@ -2,6 +2,7 @@ export class Tc { _text: string; _level?: number | undefined; _omitPageNumber: boolean; + _identifier?: string | undefined; constructor(t: string) { this._text = t; @@ -16,4 +17,9 @@ export class Tc { this._omitPageNumber = true; return this; } + + identifier(id: string) { + this._identifier = id; + return this; + } } diff --git a/docx-wasm/src/run.rs b/docx-wasm/src/run.rs index 08b86e8a..4fb3f9f7 100644 --- a/docx-wasm/src/run.rs +++ b/docx-wasm/src/run.rs @@ -51,12 +51,18 @@ impl Run { self } - pub fn add_tc(mut self, text: &str, omits_page_number: bool, level: Option) -> Run { + pub fn add_tc( + mut self, + text: &str, + omits_page_number: bool, + level: Option, + id: Option, + ) -> Run { self.0 = self.0.add_tc(docx_rs::InstrTC { text: text.into(), omits_page_number, level, - item_type_identifier: None, + item_type_identifier: id, }); self } diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index 167a32c9..2e14d5d5 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -171045,6 +171045,10 @@ exports[`writer should write ToC with instrText TC 1`] = `"TOC \\\\f \\\\h \\\\u \\\\zHello!!TC \\"Hello!!TC\\" \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; +exports[`writer should write ToC with instrText TC with id 1`] = `""`; + +exports[`writer should write ToC with instrText TC with id 2`] = `"TOC \\\\f "abc" \\\\h \\\\u \\\\zHello!!TC \\"Hello!!TC\\" \\\\f abc \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; + exports[`writer should write ToC with items 1`] = `""`; exports[`writer should write ToC with items 2`] = `"TOCHello!!PAGEREF _Toc00000000 \\\\h2WorldPAGEREF _Toc00000001 \\\\h3Hello!!World"`; diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index 954d41ee..aaa353c9 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -1216,4 +1216,34 @@ describe("writer", () => { } } }); + + test("should write ToC with instrText TC with id", () => { + const p1 = new w.Paragraph() + .addRun(new w.Run().addText("Hello!!")) + .addRun( + new w.Run().addTc(new w.Tc("Hello!!TC").level(1).identifier("abc")) + ) + .pageBreakBefore(true); + const p2 = new w.Paragraph() + .addRun(new w.Run().addText("World")) + .addRun(new w.Run().addTc(new w.Tc("World!!TC").level(1))) + .pageBreakBefore(true); + const buffer = new w.Docx() + .addTableOfContents( + new w.TableOfContents("TOC \\f abc \\h \\z \\u") + .alias("Table of contents") + .dirty() + .paragraphProperty(new w.ParagraphProperty().style("11")) + ) + .addParagraph(p1) + .addParagraph(p2) + .build(); + writeFileSync("../output/js/toc_with_instrtext_tc_with_id.docx", buffer); + const z = new Zip(Buffer.from(buffer)); + for (const e of z.getEntries()) { + if (e.entryName.match(/document.xml/)) { + expect(z.readAsText(e)).toMatchSnapshot(); + } + } + }); });