Skip to content

Commit

Permalink
fix: Support id
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Jan 20, 2025
1 parent 5095e3d commit 6b75517
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
14 changes: 14 additions & 0 deletions docx-core/src/documents/elements/instr_toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
7 changes: 6 additions & 1 deletion docx-wasm/js/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
});

Expand Down
6 changes: 6 additions & 0 deletions docx-wasm/js/tc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export class Tc {
_text: string;
_level?: number | undefined;
_omitPageNumber: boolean;
_identifier?: string | undefined;

constructor(t: string) {
this._text = t;
Expand All @@ -16,4 +17,9 @@ export class Tc {
this._omitPageNumber = true;
return this;
}

identifier(id: string) {
this._identifier = id;
return this;
}
}
10 changes: 8 additions & 2 deletions docx-wasm/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ impl Run {
self
}

pub fn add_tc(mut self, text: &str, omits_page_number: bool, level: Option<usize>) -> Run {
pub fn add_tc(
mut self,
text: &str,
omits_page_number: bool,
level: Option<usize>,
id: Option<String>,
) -> 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
}
Expand Down
4 changes: 4 additions & 0 deletions docx-wasm/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171045,6 +171045,10 @@ exports[`writer should write ToC with instrText TC 1`] = `"<?xml version=\\"1.0\

exports[`writer should write ToC with instrText TC 2`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\"><w:body><w:sdt><w:sdtPr><w:rPr /><w:alias w:val=\\"Table of contents\\" /></w:sdtPr><w:sdtContent><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"true\\" /><w:instrText>TOC \\\\f \\\\h \\\\u \\\\z</w:instrText><w:fldChar w:fldCharType=\\"separate\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p></w:sdtContent></w:sdt><w:p w14:paraId=\\"00000003\\"><w:pPr><w:rPr /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello!!</w:t></w:r><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"false\\" /><w:instrText>TC \\"Hello!!TC\\" \\\\l 1</w:instrText><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000004\\"><w:pPr><w:rPr /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">World</w:t></w:r><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"false\\" /><w:instrText>TC \\"World!!TC\\" \\\\l 1</w:instrText><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" w:num=\\"1\\" /></w:sectPr></w:body></w:document>"`;

exports[`writer should write ToC with instrText TC with id 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\"><Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" /><Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" /><Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" /><Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" /></Relationships>"`;

exports[`writer should write ToC with instrText TC with id 2`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\"><w:body><w:sdt><w:sdtPr><w:rPr /><w:alias w:val=\\"Table of contents\\" /></w:sdtPr><w:sdtContent><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"true\\" /><w:instrText>TOC \\\\f &quot;abc&quot; \\\\h \\\\u \\\\z</w:instrText><w:fldChar w:fldCharType=\\"separate\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p></w:sdtContent></w:sdt><w:p w14:paraId=\\"00000003\\"><w:pPr><w:rPr /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello!!</w:t></w:r><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"false\\" /><w:instrText>TC \\"Hello!!TC\\" \\\\f abc \\\\l 1</w:instrText><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000004\\"><w:pPr><w:rPr /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">World</w:t></w:r><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"false\\" /><w:instrText>TC \\"World!!TC\\" \\\\l 1</w:instrText><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" w:num=\\"1\\" /></w:sectPr></w:body></w:document>"`;

exports[`writer should write ToC with items 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\"><Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" /><Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" /><Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" /><Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" /></Relationships>"`;

exports[`writer should write ToC with items 2`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\"><w:body><w:sdt><w:sdtPr><w:rPr /><w:alias w:val=\\"Table of contents\\" /></w:sdtPr><w:sdtContent><w:sdt><w:sdtPr /><w:sdtContent><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"ToC1\\" /><w:tabs><w:tab w:val=\\"right\\" w:leader=\\"dot\\" w:pos=\\"80000\\" /></w:tabs></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"false\\" /><w:instrText>TOC</w:instrText><w:fldChar w:fldCharType=\\"separate\\" w:dirty=\\"false\\" /></w:r><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello!!</w:t></w:r><w:r><w:rPr /><w:tab /></w:r><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"false\\" /><w:instrText>PAGEREF _Toc00000000 \\\\h</w:instrText><w:fldChar w:fldCharType=\\"separate\\" w:dirty=\\"false\\" /><w:t xml:space=\\"preserve\\">2</w:t><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"ToC2\\" /><w:tabs><w:tab w:val=\\"right\\" w:leader=\\"dot\\" w:pos=\\"80000\\" /></w:tabs></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">World</w:t></w:r><w:r><w:rPr /><w:tab /></w:r><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"false\\" /><w:instrText>PAGEREF _Toc00000001 \\\\h</w:instrText><w:fldChar w:fldCharType=\\"separate\\" w:dirty=\\"false\\" /><w:t xml:space=\\"preserve\\">3</w:t><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000003\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"ToC2\\" /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p></w:sdtContent></w:sdt></w:sdtContent></w:sdt><w:p w14:paraId=\\"00000004\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"Heading1\\" /><w:pageBreakBefore /></w:pPr><w:bookmarkStart w:id=\\"1\\" w:name=\\"_Toc00000000\\" /><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello!!</w:t></w:r><w:bookmarkEnd w:id=\\"1\\" /></w:p><w:p w14:paraId=\\"00000005\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"Heading2\\" /><w:pageBreakBefore /></w:pPr><w:bookmarkStart w:id=\\"2\\" w:name=\\"_Toc00000001\\" /><w:r><w:rPr /><w:t xml:space=\\"preserve\\">World</w:t></w:r><w:bookmarkEnd w:id=\\"2\\" /></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" w:num=\\"1\\" /></w:sectPr></w:body></w:document>"`;
Expand Down
30 changes: 30 additions & 0 deletions docx-wasm/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
});
});

0 comments on commit 6b75517

Please sign in to comment.