eq directive

This commit is contained in:
Gregor Lohaus
2026-04-15 22:37:13 +02:00
parent 5edb1139b9
commit 0fd19254e9
7 changed files with 74 additions and 129 deletions

View File

@@ -6,12 +6,26 @@ export type TemplateVariable = {
type: string
}
const IF_RE = /<@if\(context\.(.+?)\)>/g
const IF_RE = /<@(?:if|elseif)\((.+?)\)>/g
const VAR_RE = /<@var\(context\.(.+?)(?::(\w+))?\)>/g
const EQ_RE = /^eq\(context\.(.+?),\s*"(.*)"\)$/
const PATH_RE = /^context\.(.+)$/
function extractCondition(expr: string, vars: TemplateVariable[]) {
const eqMatch = expr.match(EQ_RE)
if (eqMatch) {
vars.push({ path: eqMatch[1]!, type: "string" })
return
}
const pathMatch = expr.match(PATH_RE)
if (pathMatch) {
vars.push({ path: pathMatch[1]!, type: "boolean" })
}
}
function extractFromString(text: string, vars: TemplateVariable[]) {
for (const match of text.matchAll(IF_RE)) {
vars.push({ path: match[1]!, type: "boolean" })
extractCondition(match[1]!, vars)
}
for (const match of text.matchAll(VAR_RE)) {
vars.push({ path: match[1]!, type: match[2] ?? "string" })