From 34ebfc52909dd41735181b2a0e0079cd24a69751 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Mon, 19 Dec 2022 17:08:44 +0900 Subject: [PATCH] fix: after contents in toc (#594) * fix: after contents in toc * fix --- .../documents/elements/table_of_contents.rs | 34 ++++++++++++++----- docx-wasm/package.json | 2 +- .../test/__snapshots__/index.test.js.snap | 2 +- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/docx-core/src/documents/elements/table_of_contents.rs b/docx-core/src/documents/elements/table_of_contents.rs index ac4b4d8..91567ec 100644 --- a/docx-core/src/documents/elements/table_of_contents.rs +++ b/docx-core/src/documents/elements/table_of_contents.rs @@ -139,7 +139,6 @@ impl BuildXML for TableOfContents { .add_instr_text(InstrText::TOC(self.instr.clone())) .add_field_char(FieldCharType::Separate, false), ); - let p2 = Paragraph::new().add_run(Run::new().add_field_char(FieldCharType::End, false)); let mut b = XMLBuilder::new() .open_structured_tag() @@ -157,15 +156,32 @@ impl BuildXML for TableOfContents { } } - b = b.add_child(&p1).add_child(&p2); + b = b.add_child(&p1); - for c in self.after_contents.iter() { - match c { - TocContent::Paragraph(p) => { - b = b.add_child(p); - } - TocContent::Table(t) => { - b = b.add_child(t); + let p2 = Paragraph::new().add_run(Run::new().add_field_char(FieldCharType::End, false)); + if self.after_contents.is_empty() { + b = b.add_child(&p2); + } else { + for (i, c) in self.after_contents.iter().enumerate() { + match c { + TocContent::Paragraph(p) => { + // Merge paragraph + if i == 0 { + let mut new_p = p.clone(); + new_p.children.insert( + 0, + ParagraphChild::Run(Box::new( + Run::new().add_field_char(FieldCharType::End, false), + )), + ); + b = b.add_child(&new_p) + } else { + b = b.add_child(p); + } + } + TocContent::Table(t) => { + b = b.add_child(t); + } } } } diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 03294af..c9e031d 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.276", + "version": "0.0.277-rc0", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index 31d86c6..d407252 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -151930,7 +151930,7 @@ exports[`writer should write ToC with instrText 2`] = ` " -Before contentsTOC \\\\uAfter contents +Before contentsTOC \\\\uAfter contents Hello!! World