docxtemplater/es6/join-uncorrupt.js
2021-09-04 10:15:02 +05:00

46 lines
1.4 KiB
JavaScript

function joinUncorrupt(parts, contains) {
// Before doing this "uncorruption" method here, this was done with the `part.emptyValue` trick, however, there were some corruptions that were not handled, for example with a template like this :
//
// ------------------------------------------------
// | {-w:p falsy}My para{/falsy} | |
// | {-w:p falsy}My para{/falsy} | |
// ------------------------------------------------
let collecting = "";
let currentlyCollecting = -1;
return parts.reduce(function (full, part) {
for (let i = 0, len = contains.length; i < len; i++) {
const { tag, shouldContain, value } = contains[i];
const startTagRegex = new RegExp(`^(<(${tag})[^>]*>)$`, "g");
if (currentlyCollecting === i) {
if (part === `</${tag}>`) {
currentlyCollecting = -1;
return full + collecting + value + part;
}
collecting += part;
for (let j = 0, len2 = shouldContain.length; j < len2; j++) {
const sc = shouldContain[j];
if (
part.indexOf(`<${sc} `) !== -1 ||
part.indexOf(`<${sc}>`) !== -1
) {
currentlyCollecting = -1;
return full + collecting;
}
}
return full;
}
if (currentlyCollecting === -1 && startTagRegex.test(part)) {
if (part[part.length - 2] === "/") {
return full;
}
currentlyCollecting = i;
collecting = part;
return full;
}
}
return full + part;
}, "");
}
module.exports = joinUncorrupt;