mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 04:05:45 +00:00
fix: markdown handlebars rendering issue with this context (#5250)
This commit is contained in:
parent
24b3b08876
commit
fe0d003e99
@ -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);
|
||||
|
@ -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') {
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user