detect if page title is defined in tempalte

This commit is contained in:
amitm02
2018-10-02 17:52:55 +03:00
parent ffdcc7baca
commit f3aa6a9203
2 changed files with 19 additions and 5 deletions

View File

@ -12,7 +12,7 @@ fs.readFile(filePath, (err, data) => {
importDotx.extract(data).then((templateDocument) => { importDotx.extract(data).then((templateDocument) => {
// This any needs fixing // This any needs fixing
const sectionProps = { const sectionProps = {
titlePage: true, titlePage: templateDocument.titlePageIsDefined,
} as any; } as any;
const doc = new Document(undefined, sectionProps, { const doc = new Document(undefined, sectionProps, {

View File

@ -41,6 +41,7 @@ export interface IDocumentTemplate {
headers: IDocumentHeader[]; headers: IDocumentHeader[];
footers: IDocumentFooter[]; footers: IDocumentFooter[];
styles: Styles; styles: Styles;
titlePageIsDefined: boolean;
} }
export class ImportDotx { export class ImportDotx {
@ -59,6 +60,7 @@ export class ImportDotx {
const documentContent = zipContent.files["word/document.xml"]; const documentContent = zipContent.files["word/document.xml"];
const documentRefs: IDocumentRefs = this.extractDocumentRefs(await documentContent.async("text")); const documentRefs: IDocumentRefs = this.extractDocumentRefs(await documentContent.async("text"));
const titlePageIsDefined = this.titlePageIsDefined(await documentContent.async("text"));
const relationshipContent = zipContent.files["word/_rels/document.xml.rels"]; const relationshipContent = zipContent.files["word/_rels/document.xml.rels"];
const documentRelationships: IRelationshipFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text")); const documentRelationships: IRelationshipFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text"));
@ -77,7 +79,7 @@ export class ImportDotx {
const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent; const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent;
const header = new HeaderWrapper(this.currentRelationshipId++, importedComp); const header = new HeaderWrapper(this.currentRelationshipId++, importedComp);
await this.addImagesToWrapper(relationFileInfo, zipContent, header); await this.addRelationToWrapper(relationFileInfo, zipContent, header);
headers.push({ type: headerRef.type, header }); headers.push({ type: headerRef.type, header });
} }
@ -93,15 +95,21 @@ export class ImportDotx {
const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent; const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent;
const footer = new FooterWrapper(this.currentRelationshipId++, importedComp); const footer = new FooterWrapper(this.currentRelationshipId++, importedComp);
await this.addImagesToWrapper(relationFileInfo, zipContent, footer); await this.addRelationToWrapper(relationFileInfo, zipContent, footer);
footers.push({ type: footerRef.type, footer }); footers.push({ type: footerRef.type, footer });
} }
const templateDocument: IDocumentTemplate = { headers, footers, currentRelationshipId: this.currentRelationshipId, styles }; const templateDocument: IDocumentTemplate = {
headers,
footers,
currentRelationshipId: this.currentRelationshipId,
styles,
titlePageIsDefined,
};
return templateDocument; return templateDocument;
} }
public async addImagesToWrapper( public async addRelationToWrapper(
relationFile: IRelationshipFileInfo, relationFile: IRelationshipFileInfo,
zipContent: JSZip, zipContent: JSZip,
wrapper: HeaderWrapper | FooterWrapper, wrapper: HeaderWrapper | FooterWrapper,
@ -183,6 +191,12 @@ export class ImportDotx {
return { headers, footers }; return { headers, footers };
} }
public titlePageIsDefined(xmlData: string): boolean {
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
const sectionProp = xmlObj["w:document"]["w:body"]["w:sectPr"];
return sectionProp["w:titlePg"] !== undefined;
}
public parseRefId(str: string): number { public parseRefId(str: string): number {
const match = /^rId(\d+)$/.exec(str); const match = /^rId(\d+)$/.exec(str);
if (match === null) { if (match === null) {