From 97faed5ac9c2e9b6857fc216949cfe5d86bdd446 Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 13 Oct 2024 21:04:57 +0200 Subject: [PATCH] placeholdersUtils added. see #146 --- js/mzta-placeholders.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/js/mzta-placeholders.js b/js/mzta-placeholders.js index 7bec5f68..52856fff 100644 --- a/js/mzta-placeholders.js +++ b/js/mzta-placeholders.js @@ -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 + }); + }, + } \ No newline at end of file