feat: Support tc (#793)

* feat: Support tc

* fix: f option

* rc29

* rc30
main
bokuweb 2025-01-17 10:21:40 +00:00 committed by GitHub
parent 1fd0b8f7f3
commit 1e6227e4f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 181 additions and 12 deletions

View File

@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
<!--
- Support `TC`
-->
## @0.4.17 (26. Apr, 2024)
- Floating images cause docx generation to fail with error `should end: LastElementNameNotAvailable`

View File

@ -48,7 +48,7 @@ impl BuildXML for InstrTC {
let mut b = XMLBuilder::from(stream);
let raw = b.inner_mut()?;
write!(raw, "TC {}", self.text)?;
write!(raw, "TC \"{}\"", self.text)?;
if let Some(ref t) = self.item_type_identifier {
write!(raw, " \\f {}", t)?;

View File

@ -57,7 +57,7 @@ pub struct InstrToC {
pub seq_field_identifier_for_prefix: Option<String>,
// \f
#[serde(skip_serializing_if = "Option::is_none")]
pub tc_field_identifier: Option<String>,
pub tc_field_identifier: Option<Option<String>>,
// \h
pub hyperlink: bool,
// \w
@ -89,8 +89,8 @@ impl InstrToC {
self
}
pub fn tc_field_identifier(mut self, t: impl Into<String>) -> Self {
self.tc_field_identifier = Some(t.into());
pub fn tc_field_identifier(mut self, t: Option<String>) -> Self {
self.tc_field_identifier = Some(t);
self
}
@ -192,7 +192,11 @@ impl BuildXML for InstrToC {
// \f
if let Some(ref t) = self.tc_field_identifier {
write!(raw, " \\f &quot;{}&quot;", t)?;
if let Some(ref t) = t {
write!(raw, " \\f &quot;{}&quot;", t)?;
} else {
write!(raw, " \\f")?;
}
}
// \l
@ -279,7 +283,7 @@ impl std::str::FromStr for InstrToC {
type Err = ();
fn from_str(instr: &str) -> Result<Self, Self::Err> {
let mut s = instr.split(' ');
let mut s = instr.split(' ').peekable();
let mut toc = InstrToC::new();
loop {
if let Some(i) = s.next() {
@ -309,9 +313,19 @@ impl std::str::FromStr for InstrToC {
}
}
"\\f" => {
if let Some(r) = s.next() {
let r = r.replace("&quot;", "").replace('\"', "");
toc = toc.tc_field_identifier(r);
if let Some(n) = s.peek() {
if !n.starts_with("\\") {
if let Some(r) = s.next() {
let r = r.replace("&quot;", "").replace('\"', "");
if r.is_empty() {
toc = toc.tc_field_identifier(None);
} else {
toc = toc.tc_field_identifier(Some(r));
}
}
} else {
toc = toc.tc_field_identifier(None);
}
}
}
"\\h" => toc = toc.hyperlink(),
@ -457,4 +471,18 @@ mod tests {
.hyperlink()
);
}
#[test]
fn with_instr_text2() {
let s = r#"TOC \f \h \z \u"#;
let i = InstrToC::with_instr_text(s);
assert_eq!(
i,
InstrToC::new()
.tc_field_identifier(None)
.use_applied_paragraph_line_level()
.hide_tab_and_page_numbers_in_webview()
.hyperlink()
);
}
}

View File

@ -194,6 +194,13 @@ impl Run {
self
}
pub fn add_tc(mut self, tc: InstrTC) -> Run {
self = self.add_field_char(crate::types::FieldCharType::Begin, false);
self = self.add_instr_text(InstrText::TC(tc));
self = self.add_field_char(crate::types::FieldCharType::End, false);
self
}
pub fn add_instr_text(mut self, i: InstrText) -> Run {
self.children.push(RunChild::InstrText(Box::new(i)));
self

View File

@ -83,6 +83,11 @@ impl TableOfContents {
self
}
pub fn tc_field_identifier(mut self, f: Option<String>) -> Self {
self.instr = self.instr.tc_field_identifier(f);
self
}
pub fn add_style_with_level(mut self, s: StyleWithLevel) -> Self {
self.instr = self.instr.add_style_with_level(s);
self

View File

@ -654,6 +654,7 @@ export * from "./paragraph-property";
export * from "./insert";
export * from "./delete";
export * from "./border";
export * from "./tc";
export * from "./table";
export * from "./table-cell";
export * from "./table-cell-border";

View File

@ -1,3 +1,3 @@
import type { StyleWithLevel } from "./StyleWithLevel";
export interface InstrToC { headingStylesRange?: [number, number], tcFieldLevelRange?: [number, number], omitPageNumbersLevelRange?: [number, number], entryBookmarkName?: string, stylesWithLevels: Array<StyleWithLevel>, entryAndPageNumberSeparator?: string, sequenceAndPageNumbersSeparator?: string, captionLabel: string | null, captionLabelIncludingNumbers?: string, seqFieldIdentifierForPrefix?: string, tcFieldIdentifier?: string, hyperlink: boolean, preserveTab: boolean, preserveNewLine: boolean, useAppliedParagraphLineLevel: boolean, hideTabAndPageNumbersInWebview: boolean, }
export interface InstrToC { headingStylesRange?: [number, number], tcFieldLevelRange?: [number, number], omitPageNumbersLevelRange?: [number, number], entryBookmarkName?: string, stylesWithLevels: Array<StyleWithLevel>, entryAndPageNumberSeparator?: string, sequenceAndPageNumbersSeparator?: string, captionLabel: string | null, captionLabelIncludingNumbers?: string, seqFieldIdentifierForPrefix?: string, tcFieldIdentifier?: string | null, hyperlink: boolean, preserveTab: boolean, preserveNewLine: boolean, useAppliedParagraphLineLevel: boolean, hideTabAndPageNumbersInWebview: boolean, }

View File

@ -14,8 +14,16 @@ import {
setRunProperty,
VertAlignType,
} from "./run-property";
import { Tc } from "./tc";
export type RunChild = Text | DeleteText | Tab | Break | Image | PositionalTab;
export type RunChild =
| Text
| DeleteText
| Tab
| Break
| Image
| PositionalTab
| Tc;
export class Run {
children: RunChild[] = [];
@ -51,6 +59,11 @@ export class Run {
return this;
}
addTc(tc: Tc) {
this.children.push(tc);
return this;
}
style(style: string) {
this.property ??= createDefaultRunProperty();
this.property.style(style);
@ -178,6 +191,8 @@ export class Run {
pic = pic.rotate(child.rot);
}
run = run.add_image(pic);
} else if (child instanceof Tc) {
run = run.add_tc(child._text, child._omitPageNumber, child._level);
}
});

View File

@ -23,6 +23,7 @@ export class TableOfContents {
_afterContents: (Paragraph | Table)[] = [];
_delete: { author: string; date: string } | null = null;
_paragraphProperty: ParagraphProperty | null = null;
_tcFieldIdentifier?: string | null;
constructor(instrText?: string) {
this._instrText = instrText;
@ -53,6 +54,11 @@ export class TableOfContents {
return this;
};
tcFieldIdentifier = (f?: string) => {
this._tcFieldIdentifier = f ?? null;
return this;
};
addStyleWithLevel = (styleId: string, level: number) => {
this._styleWithLevels.push({ styleId, level });
return this;
@ -142,6 +148,10 @@ export class TableOfContents {
toc = toc.delete(this._delete.author, this._delete.date);
}
if (this._tcFieldIdentifier !== undefined) {
toc = toc.tc_field_identifier(this._tcFieldIdentifier ?? undefined);
}
for (const sl of this._styleWithLevels) {
toc = toc.add_style_with_level(sl.styleId, sl.level);
}

19
docx-wasm/js/tc.ts 100644
View File

@ -0,0 +1,19 @@
export class Tc {
_text: string;
_level?: number | undefined;
_omitPageNumber: boolean;
constructor(t: string) {
this._text = t;
}
level(l: number) {
this._level = l;
return this;
}
omitPageNumber() {
this._omitPageNumber = true;
return this;
}
}

View File

@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.4.18-rc27",
"version": "0.4.18-rc30",
"main": "dist/node/index.js",
"browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -51,6 +51,16 @@ impl Run {
self
}
pub fn add_tc(mut self, text: &str, omits_page_number: bool, level: Option<usize>) -> Run {
self.0 = self.0.add_tc(docx_rs::InstrTC {
text: text.into(),
omits_page_number,
level,
item_type_identifier: None,
});
self
}
pub fn style(mut self, style: &str) -> Run {
self.0.run_property = self.0.run_property.style(style);
self

View File

@ -29,6 +29,11 @@ impl TableOfContents {
self
}
pub fn tc_field_identifier(mut self, f: Option<String>) -> Self {
self.0.instr = self.0.instr.tc_field_identifier(f);
self
}
pub fn add_style_with_level(mut self, style: &str, level: usize) -> Self {
self.0.instr = self
.0

View File

@ -171033,10 +171033,18 @@ Object {
}
`;
exports[`writer should write ToC with TC 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 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</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 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 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:t xml:space=\\"preserve\\">Before contents</w:t></w:r></w:p><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 \\\\u</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:r><w:rPr /><w:t xml:space=\\"preserve\\">After contents</w:t></w:r></w:p></w:sdtContent></w:sdt><w:p w14:paraId=\\"00000003\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"Heading1\\" /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello!!</w:t></w:r></w:p><w:p w14:paraId=\\"00000004\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"Heading2\\" /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">World</w:t></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 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 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 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>"`;

View File

@ -1159,4 +1159,61 @@ describe("writer", () => {
}
}
});
test("should write ToC with TC", () => {
const p1 = new w.Paragraph()
.addRun(new w.Run().addText("Hello!!"))
.addRun(new w.Run().addTc(new w.Tc("Hello!!TC").level(1)))
.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()
.alias("Table of contents")
.dirty()
.tcFieldIdentifier()
.paragraphProperty(new w.ParagraphProperty().style("11"))
)
.addParagraph(p1)
.addParagraph(p2)
.build();
writeFileSync("../output/js/toc_with_tc.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();
}
}
});
test("should write ToC with instrText TC", () => {
const p1 = new w.Paragraph()
.addRun(new w.Run().addText("Hello!!"))
.addRun(new w.Run().addTc(new w.Tc("Hello!!TC").level(1)))
.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 \\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.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();
}
}
});
});