feat: refactor variable extraction logic to use extractTemplateVariable function

This commit is contained in:
Sheldon Guo 2025-03-05 00:05:45 +08:00
parent 469d6819ab
commit fd8784eee5
3 changed files with 13 additions and 7 deletions

View File

@ -6,7 +6,7 @@
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { extractTemplateVariable } from '@nocobase/json-template-parser';
import { REGEX_OF_VARIABLE } from './isVariable';
/**
@ -20,6 +20,6 @@ export const getPath = (variableString: string) => {
return variableString;
}
const matches = variableString.match(REGEX_OF_VARIABLE);
return matches[0].replace(REGEX_OF_VARIABLE, '$1');
const variable = extractTemplateVariable(variableString);
return variable;
};

View File

@ -6,6 +6,7 @@
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { extractTemplateVariable } from '@nocobase/json-template-parser';
export const REGEX_OF_VARIABLE = /^\s*\{\{\s*([a-zA-Z0-9_$-.]+?)\s*\}\}\s*$/g;
export const REGEX_OF_VARIABLE_IN_EXPRESSION = /\{\{\s*([a-zA-Z0-9_$-.]+?)\s*\}\}/g;
@ -14,12 +15,11 @@ export const isVariable = (str: unknown) => {
if (typeof str !== 'string') {
return false;
}
const matches = str.match(REGEX_OF_VARIABLE);
const variable = extractTemplateVariable(str);
if (!matches) {
if (!variable) {
return false;
}
return true;
};

View File

@ -18,9 +18,15 @@ type Filter = {
args: string[];
};
export function extractTemplateVariable(template: string) {
const escapedTemplate = escape(template ?? '');
const fullVariable = engine.fullVariablesSync(escapedTemplate)[0] ?? '';
return revertEscape(fullVariable);
}
export function extractTemplateElements(template: string) {
const escapedTemplate = escape(template ?? '');
const fullVariable = engine.fullVariablesSync(escapeSpecialChars(escapedTemplate))[0] ?? '';
const fullVariable = engine.fullVariablesSync(escapedTemplate)[0] ?? '';
const variableSegments = engine.variableSegmentsSync(escapedTemplate)[0] ?? [];
const parsedTemplate = engine.parse(escapedTemplate)[0] ?? {};