Optimise formatting to not over-format the same files
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
/* tslint:disable:typedef space-before-function-paren */
|
||||
import { expect } from "chai";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import { File, Footer, Header } from "file";
|
||||
import { File, Footer, Header, Paragraph } from "file";
|
||||
|
||||
import { Compiler } from "./next-compiler";
|
||||
|
||||
@ -72,5 +73,22 @@ describe("Compiler", () => {
|
||||
expect(fileNames).to.include("word/footer2.xml");
|
||||
expect(fileNames).to.include("word/_rels/footer2.xml.rels");
|
||||
});
|
||||
|
||||
it("should call the format method X times equalling X files to be formatted", () => {
|
||||
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
|
||||
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
|
||||
const paragraph = new Paragraph("");
|
||||
const doc = new File();
|
||||
|
||||
doc.addSection({
|
||||
properties: {},
|
||||
children: [paragraph],
|
||||
});
|
||||
// tslint:disable-next-line: no-string-literal
|
||||
const spy = sinon.spy(compiler["formatter"], "format");
|
||||
|
||||
compiler.compile(file);
|
||||
expect(spy.callCount).to.equal(10);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -68,13 +68,13 @@ export class Compiler {
|
||||
file.verifyUpdateFields();
|
||||
const documentRelationshipCount = file.DocumentRelationships.RelationshipCount + 1;
|
||||
|
||||
const documentXmlData = xml(this.formatter.format(file.Document), prettify);
|
||||
const documentMediaDatas = this.imageReplacer.getMediaData(documentXmlData, file.Media);
|
||||
|
||||
return {
|
||||
Relationships: {
|
||||
data: (() => {
|
||||
const xmlData = xml(this.formatter.format(file.Document), prettify);
|
||||
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
|
||||
|
||||
mediaDatas.forEach((mediaData, i) => {
|
||||
documentMediaDatas.forEach((mediaData, i) => {
|
||||
file.DocumentRelationships.createRelationship(
|
||||
documentRelationshipCount + i,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||
@ -88,9 +88,7 @@ export class Compiler {
|
||||
},
|
||||
Document: {
|
||||
data: (() => {
|
||||
const tempXmlData = xml(this.formatter.format(file.Document), prettify);
|
||||
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
|
||||
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, documentRelationshipCount);
|
||||
const xmlData = this.imageReplacer.replace(documentXmlData, documentMediaDatas, documentRelationshipCount);
|
||||
|
||||
return xmlData;
|
||||
})(),
|
||||
|
Reference in New Issue
Block a user