mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 11:36:42 +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 Handlebars from 'handlebars';
|
||||||
import _, { every, findIndex, some } from 'lodash';
|
import _, { every, findIndex, some } from 'lodash';
|
||||||
|
import { evaluators } from '@nocobase/evaluators/client';
|
||||||
import { replaceVariableValue } from '../../../block-provider/hooks';
|
import { replaceVariableValue } from '../../../block-provider/hooks';
|
||||||
import { VariableOption, VariablesContextType } from '../../../variables/types';
|
import { VariableOption, VariablesContextType } from '../../../variables/types';
|
||||||
import { isVariable } from '../../../variables/utils/isVariable';
|
import { isVariable } from '../../../variables/utils/isVariable';
|
||||||
import { transformVariableValue } from '../../../variables/utils/transformVariableValue';
|
import { transformVariableValue } from '../../../variables/utils/transformVariableValue';
|
||||||
import { getJsonLogic } from '../../common/utils/logic';
|
import { getJsonLogic } from '../../common/utils/logic';
|
||||||
|
import { replaceVariables } from '../../../schema-settings/LinkageRules/bindLinkageRulesToFiled';
|
||||||
|
|
||||||
type VariablesCtx = {
|
type VariablesCtx = {
|
||||||
/** 当前登录的用户 */
|
/** 当前登录的用户 */
|
||||||
@ -162,15 +164,16 @@ const getVariablesData = (localVariables) => {
|
|||||||
export async function getRenderContent(templateEngine, content, variables, localVariables, defaultParse) {
|
export async function getRenderContent(templateEngine, content, variables, localVariables, defaultParse) {
|
||||||
if (content && templateEngine === 'handlebars') {
|
if (content && templateEngine === 'handlebars') {
|
||||||
try {
|
try {
|
||||||
try {
|
const { evaluate } = evaluators.get('string');
|
||||||
await replaceVariableValue(content, variables, localVariables);
|
const { exp, scope: expScope } = await replaceVariables(content, {
|
||||||
} catch (error) {
|
variables,
|
||||||
return null;
|
localVariables,
|
||||||
}
|
});
|
||||||
const renderedContent = Handlebars.compile(content);
|
const result = evaluate(exp, { now: () => new Date().toString(), ...expScope });
|
||||||
|
const renderedContent = Handlebars.compile(result);
|
||||||
// 处理渲染后的内容
|
// 处理渲染后的内容
|
||||||
const data = getVariablesData(localVariables);
|
const data = getVariablesData(localVariables);
|
||||||
const html = renderedContent({ ...variables.ctxRef.current, ...data });
|
const html = renderedContent({ ...variables.ctxRef.current, ...data, ...expScope });
|
||||||
return await defaultParse(html);
|
return await defaultParse(html);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(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 = /^\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) => {
|
export const isVariable = (str: unknown) => {
|
||||||
if (typeof str !== 'string') {
|
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) => {
|
const exp = expression.trim().replace(/{{\s*([\w$.-]+)\s*}}/g, (_, v) => {
|
||||||
appendArrayColumn(context, v);
|
appendArrayColumn(context, v);
|
||||||
|
|
||||||
let item = get(context, v) ?? null;
|
let item = get(context, v) ?? `{{${v}}}`;
|
||||||
|
|
||||||
if (typeof item === 'function') {
|
if (typeof item === 'function') {
|
||||||
item = item();
|
item = item();
|
||||||
|
Loading…
Reference in New Issue
Block a user