fix: markdown handlebars rendering issue with this context (#5250)

This commit is contained in:
Katherine 2024-09-11 11:18:20 +08:00 committed by GitHub
parent 24b3b08876
commit fe0d003e99
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 9 deletions

View File

@ -9,11 +9,13 @@
import Handlebars from 'handlebars';
import _, { every, findIndex, some } from 'lodash';
import { evaluators } from '@nocobase/evaluators/client';
import { replaceVariableValue } from '../../../block-provider/hooks';
import { VariableOption, VariablesContextType } from '../../../variables/types';
import { isVariable } from '../../../variables/utils/isVariable';
import { transformVariableValue } from '../../../variables/utils/transformVariableValue';
import { getJsonLogic } from '../../common/utils/logic';
import { replaceVariables } from '../../../schema-settings/LinkageRules/bindLinkageRulesToFiled';
type VariablesCtx = {
/** 当前登录的用户 */
@ -162,15 +164,16 @@ const getVariablesData = (localVariables) => {
export async function getRenderContent(templateEngine, content, variables, localVariables, defaultParse) {
if (content && templateEngine === 'handlebars') {
try {
try {
await replaceVariableValue(content, variables, localVariables);
} catch (error) {
return null;
}
const renderedContent = Handlebars.compile(content);
const { evaluate } = evaluators.get('string');
const { exp, scope: expScope } = await replaceVariables(content, {
variables,
localVariables,
});
const result = evaluate(exp, { now: () => new Date().toString(), ...expScope });
const renderedContent = Handlebars.compile(result);
// 处理渲染后的内容
const data = getVariablesData(localVariables);
const html = renderedContent({ ...variables.ctxRef.current, ...data });
const html = renderedContent({ ...variables.ctxRef.current, ...data, ...expScope });
return await defaultParse(html);
} catch (error) {
console.log(error);

View File

@ -8,7 +8,7 @@
*/
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*(?!this\.)([a-zA-Z_$][a-zA-Z0-9_$.-]*)\s*\}\}/g;
export const isVariable = (str: unknown) => {
if (typeof str !== 'string') {

View File

@ -38,7 +38,7 @@ export function evaluate(this: Evaluator, options: EvaluatorOptions = {}, expres
const exp = expression.trim().replace(/{{\s*([\w$.-]+)\s*}}/g, (_, v) => {
appendArrayColumn(context, v);
let item = get(context, v) ?? null;
let item = get(context, v) ?? `{{${v}}}`;
if (typeof item === 'function') {
item = item();