template function for base64

Signed-off-by: Ronni Skansing <rskansing@gmail.com>
This commit is contained in:
Ronni Skansing
2025-09-04 10:41:35 +02:00
parent 96ce320e4d
commit 32ad02f4d8
2 changed files with 14 additions and 1 deletions
+4
View File
@@ -2,6 +2,7 @@ package service
import (
"bytes"
"encoding/base64"
"fmt"
"html"
"html/template"
@@ -431,6 +432,9 @@ func TemplateFuncs() template.FuncMap {
goFormat := convertDateFormat(format)
return targetTime.Format(goFormat)
},
"base64": func(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
},
}
}
@@ -60,7 +60,8 @@
{ label: 'URL escape', text: '{{urlEscape "content" }}' },
{ label: 'Random alphanumeric', text: '{{randAlpha 8}}' },
{ label: 'Random number', text: '{{randInt 1 4}}' },
{ label: 'Date', text: '{{date "Y-m-d H:i:s" 0}}' }
{ label: 'Date', text: '{{date "Y-m-d H:i:s" 0}}' },
{ label: 'Base64', text: '{{base64 "text"}}' }
]
};
@@ -228,6 +229,14 @@
});
}
// handle base64 function
if (text.includes('{{base64')) {
const r = /{{base64\s+"([^"]+)"}}/g;
text = text.replace(r, (match, input) => {
return btoa(input);
});
}
// handle date function with format and optional offset
if (text.includes('{{date')) {
const r = /{{date\s+"([^"]+)"\s*(-?\d+)?}}/g;