@@ -10,11 +10,13 @@ import { IPresentationProps } from './interfaces/ipresentation-props';
1010import { PresTemplate } from './interfaces/pres-template' ;
1111import { RootPresTemplate } from './interfaces/root-pres-template' ;
1212import { Template } from './classes/template' ;
13- import { TemplateInfo } from './types/xml-types' ;
13+ import { ModifyXmlCallback , TemplateInfo } from './types/xml-types' ;
1414import { vd } from './helper/general-helper' ;
1515import { Master } from './classes/master' ;
1616import path from 'path' ;
1717import * as fs from 'fs' ;
18+ import { XmlHelper } from './helper/xml-helper' ;
19+ import ModifyPresentationHelper from './helper/modify-presentation-helper' ;
1820
1921/**
2022 * Automizer
@@ -40,12 +42,15 @@ export default class Automizer implements IPresentationProps {
4042 params : AutomizerParams ;
4143 status : StatusTracker ;
4244
45+ modifyPresentation : ModifyXmlCallback [ ] ;
46+
4347 /**
4448 * Creates an instance of `pptx-automizer`.
4549 * @param [params]
4650 */
4751 constructor ( params : AutomizerParams ) {
4852 this . templates = [ ] ;
53+ this . modifyPresentation = [ ] ;
4954 this . params = params ;
5055
5156 this . templateDir = params ?. templateDir ? params . templateDir + '/' : '' ;
@@ -157,7 +162,7 @@ export default class Automizer implements IPresentationProps {
157162 /**
158163 * Parses all loaded templates and collects creationIds for slides and
159164 * elements. This will make finding templates and elements independent
160- * from slide number and element name.
165+ * of slide number and element name.
161166 * @returns Promise<TemplateInfo[]>
162167 */
163168 public async setCreationIds ( ) : Promise < TemplateInfo [ ] > {
@@ -173,6 +178,11 @@ export default class Automizer implements IPresentationProps {
173178 return templateCreationId ;
174179 }
175180
181+ public modify ( cb : ModifyXmlCallback ) : this {
182+ this . modifyPresentation . push ( cb ) ;
183+ return this ;
184+ }
185+
176186 /**
177187 * Determines whether template is root or default template.
178188 * @param template
@@ -262,8 +272,24 @@ export default class Automizer implements IPresentationProps {
262272 * @returns summary object.
263273 */
264274 public async write ( location : string ) : Promise < AutomizerSummary > {
275+ await this . writeSlides ( ) ;
276+ await this . normalizePresentation ( ) ;
277+ await this . applyModifyPresentationCallbacks ( ) ;
278+
265279 const rootArchive = await this . rootTemplate . archive ;
280+ const content = await rootArchive . generateAsync ( { type : 'nodebuffer' } ) ;
281+
282+ return FileHelper . writeOutputFile (
283+ this . getLocation ( location , 'output' ) ,
284+ content ,
285+ this ,
286+ ) ;
287+ }
266288
289+ /**
290+ * Write all slides into archive.
291+ */
292+ public async writeSlides ( ) : Promise < void > {
267293 await this . rootTemplate . countExistingSlides ( ) ;
268294 this . status . max = this . rootTemplate . slides . length ;
269295
@@ -274,16 +300,32 @@ export default class Automizer implements IPresentationProps {
274300 if ( this . params . removeExistingSlides ) {
275301 await this . rootTemplate . truncate ( ) ;
276302 }
303+ }
277304
278- const content = await rootArchive . generateAsync ( { type : 'nodebuffer' } ) ;
279-
280- return FileHelper . writeOutputFile (
281- this . getLocation ( location , 'output' ) ,
282- content ,
283- this ,
305+ /**
306+ * Applies all callbacks in this.modifyPresentation-array.
307+ * The callback array can be pushed by this.modify()
308+ */
309+ async applyModifyPresentationCallbacks ( ) : Promise < void > {
310+ await XmlHelper . modifyXmlInArchive (
311+ this . rootTemplate . archive ,
312+ `ppt/presentation.xml` ,
313+ this . modifyPresentation ,
284314 ) ;
285315 }
286316
317+ /**
318+ * Apply some callbacks to restore archive/xml structure
319+ * and prevent corrupted pptx files.
320+ *
321+ * TODO: Remove unused parts (slides, related items) from archive.
322+ * TODO: Use every imported image only once
323+ * TODO: Check for lost relations
324+ */
325+ normalizePresentation ( ) : void {
326+ this . modify ( ModifyPresentationHelper . normalizeSlideIds ) ;
327+ }
328+
287329 /**
288330 * Applies path prefix to given location string.
289331 * @param location path and/or filename
0 commit comments