Skip to content

Commit

Permalink
fix: handling of placeholder matching
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Mar 8, 2024
1 parent 4478c11 commit b80be78
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,10 @@ async function render({ source, element, selector, data, key, index, currentInde

await renderTemplate(element[i], data, key, index);
}
} else
await renderValues(element[i], data);
} else {
let renderAs = element[i].getAttribute('render-as') || key;
await renderValues(element[i], data, key, renderAs);
}

}

Expand Down Expand Up @@ -583,6 +585,8 @@ async function renderValues(node, data, key, renderAs, keyPath, parent) {
async function renderValue(node, data, placeholder, renderAs, renderedNode) {
let output = placeholder;
let regex = /\{([^{}]+)\}/
let omitted = {}

let match;
do {
match = output.match(regex);
Expand Down Expand Up @@ -633,19 +637,25 @@ async function renderValue(node, data, placeholder, renderAs, renderedNode) {
if (typeof value === "object") {
value = JSON.stringify(value, null, 2);
}
output = output.replace(match[0], value);
output = output.replaceAll(match[0], value);
} else if (renderAs) {
if (match[0].startsWith(`{{${renderAs}.`)) {
output = output.replace(match[0], "");
output = output.replaceAll(match[0], "");
} else {
match = null;
output = output.replaceAll(match[0], `<|${match[1]}|>`);
omitted[`<|${match[1]}|>`] = match[0]
}
} else {
// output = output.replace(match[0], `<|${match[1]}|>`);
match = null;
}
}
} while (match);

for (let key of Object.keys(omitted)) {
output = output.replaceAll(key, omitted[key]);
}

return output;
}

Expand Down

0 comments on commit b80be78

Please sign in to comment.