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

View File

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

View File

@ -18,9 +18,15 @@ type Filter = {
args: string[]; 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) { export function extractTemplateElements(template: string) {
const escapedTemplate = escape(template ?? ''); const escapedTemplate = escape(template ?? '');
const fullVariable = engine.fullVariablesSync(escapeSpecialChars(escapedTemplate))[0] ?? ''; const fullVariable = engine.fullVariablesSync(escapedTemplate)[0] ?? '';
const variableSegments = engine.variableSegmentsSync(escapedTemplate)[0] ?? []; const variableSegments = engine.variableSegmentsSync(escapedTemplate)[0] ?? [];
const parsedTemplate = engine.parse(escapedTemplate)[0] ?? {}; const parsedTemplate = engine.parse(escapedTemplate)[0] ?? {};