eq directive
This commit is contained in:
18
parser.ts
18
parser.ts
@@ -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" })
|
||||
|
||||
Reference in New Issue
Block a user