placeholdersUtils added. see #146

This commit is contained in:
mic 2024-10-13 21:04:57 +02:00
parent 0c40dc4a12
commit 97faed5ac9

View file

@ -107,4 +107,31 @@ export async function setDefaultPlaceholdersProperties(placeholders) {
export async function setCustomPlaceholders(placeholders) {
await browser.storage.local.set({_custom_placeholder: placeholders});
}
export const placeholdersUtils = {
extractPlacehodlers(text) {
// Regular expression to match patterns like {%...%}
const regex = /{%\s*(.*?)\s*%}/g;
let matches = [];
let match;
// Use exec to find all matches
while ((match = regex.exec(text)) !== null) {
matches.push(match[1]); // Push only the string inside {%...%}
}
return matches;
},
replacePlaceholders(text, replacements) {
// Regular expression to match patterns like {%...%}
return text.replace(/{%\s*(.*?)\s*%}/g, function(match, p1) {
// p1 contains the key inside {% %}
return replacements[p1] || match; // Replace if found, otherwise keep the original
});
},
}