Skip to content

Commit 6e10d3e

Browse files
committed
refactor(editor): move template rendering function to separate module
The template rendering function has been moved to a new file, `TemplateRender.ts`, to improve code organization. The `PromptsManager` now imports this function from the module. This change enhances readability and maintainability by separating concerns.
1 parent fd2d932 commit 6e10d3e

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function render(template: string, data: Record<string, any>): string {
2+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3+
// @ts-expect-error
4+
return template.replace(/\$\{([\s\S]+?)\}/g, (match, p1) => {
5+
const keys = p1.trim().split('.');
6+
let value = data;
7+
for (const key of keys) {
8+
value = value[key];
9+
if (value === undefined) {
10+
return '';
11+
}
12+
}
13+
return value;
14+
});
15+
}

web/core/lib/editor/prompts/prompts-manager.ts

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import i18next from "i18next";
2-
import { DefinedVariable, FacetType, PromptAction } from "@/editor/defs/custom-action.type";
3-
import ArticlePrompts from "@/editor/prompts/article-prompts";
4-
import { TypeOptions } from "@/editor/defs/type-options.type";
5-
import RequirementsPrompts from "@/editor/prompts/requirements-prompts";
1+
import i18next from 'i18next';
2+
import { DefinedVariable, FacetType, PromptAction } from '@/editor/defs/custom-action.type';
3+
import ArticlePrompts from '@/editor/prompts/article-prompts';
4+
import { TypeOptions } from '@/editor/defs/type-options.type';
5+
import RequirementsPrompts from '@/editor/prompts/requirements-prompts';
6+
import { render } from '@/editor/prompts/TemplateRender.ts';
67

78
export class PromptsManager {
89
private constructor() {
@@ -59,18 +60,3 @@ export class PromptsManager {
5960
}
6061
}
6162

62-
function render(template: string, data: object) {
63-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
64-
// @ts-expect-error
65-
return template.replace(/\$\{([\s\S]+?)\}/g, (match, p1) => {
66-
const keys = p1.trim().split('.');
67-
let value = data;
68-
for (const key of keys) {
69-
value = value[key];
70-
if (value === undefined) {
71-
return '';
72-
}
73-
}
74-
return value;
75-
});
76-
}

0 commit comments

Comments
 (0)