docxtemplater
Advanced tools
Comparing version 3.51.1 to 3.51.2
@@ -23,2 +23,4 @@ declare namespace DXT { | ||
subparsed?: Part[]; | ||
position?: string; | ||
tag?: string; | ||
} | ||
@@ -54,3 +56,3 @@ | ||
getTraits?(traitName: string, parsed: any): any; | ||
getFileType?(opts: any): string; | ||
getFileType?(opts: any): string | void; | ||
nullGetter?(part: Part, scopeManager: any): any; | ||
@@ -128,2 +130,3 @@ optionsTransformer?(options: Options): Options; | ||
getFullText(path?: string): string; | ||
targets: string[]; // used to know which files are templated | ||
replaceFirstSection?: boolean; // used for the subsection module | ||
@@ -130,0 +133,0 @@ replaceLastSection?: boolean; // used for the subsection module |
@@ -224,1 +224,95 @@ import Docxtemplater, { DXT } from "./docxtemplater"; | ||
}); | ||
// Define the parameter type for getFileType | ||
interface FileTypeParams { | ||
doc: Docxtemplater; | ||
} | ||
const avoidRenderingCoreXMLModule = { | ||
name: "avoidRenderingCoreXMLModule", | ||
getFileType({ doc }: FileTypeParams): void { | ||
doc.targets = doc.targets.filter(function (file: string) { | ||
if ( | ||
file === "docProps/core.xml" || | ||
file === "docProps/app.xml" || | ||
file === "docProps/custom.xml" | ||
) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
}, | ||
}; | ||
new Docxtemplater(new PizZip("hello"), { | ||
modules: [avoidRenderingCoreXMLModule], | ||
paragraphLoop: true, | ||
linebreaks: true, | ||
}); | ||
interface SetOptions { | ||
Lexer: any; | ||
zip: any; | ||
} | ||
const fixDocPrCorruptionModule: DXT.Module = { | ||
set(options: SetOptions) { | ||
if (options.Lexer) { | ||
this.Lexer = options.Lexer; | ||
} | ||
if (options.zip) { | ||
this.zip = options.zip; | ||
} | ||
}, | ||
on(event) { | ||
if (event === "attached") { | ||
this.attached = false; | ||
} | ||
if (event !== "syncing-zip") { | ||
return; | ||
} | ||
const zip = this.zip; | ||
const Lexer = this.Lexer; | ||
let prId = 1; | ||
function setSingleAttribute( | ||
partValue: string, | ||
attr: string, | ||
attrValue: string | number | ||
) { | ||
const regex = new RegExp(`(<.* ${attr}=")([^"]+)(".*)$`); | ||
if (regex.test(partValue)) { | ||
return partValue.replace(regex, `$1${attrValue}$3`); | ||
} | ||
let end = partValue.lastIndexOf("/>"); | ||
if (end === -1) { | ||
end = partValue.lastIndexOf(">"); | ||
} | ||
return ( | ||
partValue.substr(0, end) + | ||
` ${attr}="${attrValue}"` + | ||
partValue.substr(end) | ||
); | ||
} | ||
zip.file(/\.xml$/).forEach(function (f: any) { | ||
let text = f.asText(); | ||
const xmllexed = Lexer.xmlparse(text, { | ||
text: [], | ||
other: ["wp:docPr"], | ||
}); | ||
if (xmllexed.length > 1) { | ||
text = xmllexed.reduce(function (fullText: string, part: DXT.Part) { | ||
if ( | ||
part.tag === "wp:docPr" && | ||
part.position && | ||
["start", "selfclosing"].indexOf(part.position) !== -1 | ||
) { | ||
return fullText + setSingleAttribute(part.value, "id", prId++); | ||
} | ||
return fullText + part.value; | ||
}, ""); | ||
} | ||
zip.file(f.name, text); | ||
}); | ||
}, | ||
}; | ||
new Docxtemplater(new PizZip("hello"), { | ||
modules: [fixDocPrCorruptionModule], | ||
}); |
@@ -23,2 +23,4 @@ declare namespace DXT { | ||
subparsed?: Part[]; | ||
position?: string; | ||
tag?: string; | ||
} | ||
@@ -54,3 +56,3 @@ | ||
getTraits?(traitName: string, parsed: any): any; | ||
getFileType?(opts: any): string; | ||
getFileType?(opts: any): string | void; | ||
nullGetter?(part: Part, scopeManager: any): any; | ||
@@ -128,2 +130,3 @@ optionsTransformer?(options: Options): Options; | ||
getFullText(path?: string): string; | ||
targets: string[]; // used to know which files are templated | ||
replaceFirstSection?: boolean; // used for the subsection module | ||
@@ -130,0 +133,0 @@ replaceLastSection?: boolean; // used for the subsection module |
@@ -224,1 +224,95 @@ import Docxtemplater, { DXT } from "./docxtemplater"; | ||
}); | ||
// Define the parameter type for getFileType | ||
interface FileTypeParams { | ||
doc: Docxtemplater; | ||
} | ||
const avoidRenderingCoreXMLModule = { | ||
name: "avoidRenderingCoreXMLModule", | ||
getFileType({ doc }: FileTypeParams): void { | ||
doc.targets = doc.targets.filter(function (file: string) { | ||
if ( | ||
file === "docProps/core.xml" || | ||
file === "docProps/app.xml" || | ||
file === "docProps/custom.xml" | ||
) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
}, | ||
}; | ||
new Docxtemplater(new PizZip("hello"), { | ||
modules: [avoidRenderingCoreXMLModule], | ||
paragraphLoop: true, | ||
linebreaks: true, | ||
}); | ||
interface SetOptions { | ||
Lexer: any; | ||
zip: any; | ||
} | ||
const fixDocPrCorruptionModule: DXT.Module = { | ||
set(options: SetOptions) { | ||
if (options.Lexer) { | ||
this.Lexer = options.Lexer; | ||
} | ||
if (options.zip) { | ||
this.zip = options.zip; | ||
} | ||
}, | ||
on(event) { | ||
if (event === "attached") { | ||
this.attached = false; | ||
} | ||
if (event !== "syncing-zip") { | ||
return; | ||
} | ||
const zip = this.zip; | ||
const Lexer = this.Lexer; | ||
let prId = 1; | ||
function setSingleAttribute( | ||
partValue: string, | ||
attr: string, | ||
attrValue: string | number | ||
) { | ||
const regex = new RegExp(`(<.* ${attr}=")([^"]+)(".*)$`); | ||
if (regex.test(partValue)) { | ||
return partValue.replace(regex, `$1${attrValue}$3`); | ||
} | ||
let end = partValue.lastIndexOf("/>"); | ||
if (end === -1) { | ||
end = partValue.lastIndexOf(">"); | ||
} | ||
return ( | ||
partValue.substr(0, end) + | ||
` ${attr}="${attrValue}"` + | ||
partValue.substr(end) | ||
); | ||
} | ||
zip.file(/\.xml$/).forEach(function (f: any) { | ||
let text = f.asText(); | ||
const xmllexed = Lexer.xmlparse(text, { | ||
text: [], | ||
other: ["wp:docPr"], | ||
}); | ||
if (xmllexed.length > 1) { | ||
text = xmllexed.reduce(function (fullText: string, part: DXT.Part) { | ||
if ( | ||
part.tag === "wp:docPr" && | ||
part.position && | ||
["start", "selfclosing"].indexOf(part.position) !== -1 | ||
) { | ||
return fullText + setSingleAttribute(part.value, "id", prId++); | ||
} | ||
return fullText + part.value; | ||
}, ""); | ||
} | ||
zip.file(f.name, text); | ||
}); | ||
}, | ||
}; | ||
new Docxtemplater(new PizZip("hello"), { | ||
modules: [fixDocPrCorruptionModule], | ||
}); |
{ | ||
"name": "docxtemplater", | ||
"version": "3.51.1", | ||
"version": "3.51.2", | ||
"author": "Edgar Hipp", | ||
@@ -5,0 +5,0 @@ "description": "Generate docx, pptx, and xlsx from templates (Word, Powerpoint and Excel documents), from Node.js, the Browser and the command line", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1121967
23451