Skip to content

Commit

Permalink
refactor(editor): move template rendering function to separate module
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Oct 9, 2024
1 parent fd2d932 commit 6e10d3e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
15 changes: 15 additions & 0 deletions web/core/lib/editor/prompts/TemplateRender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function render(template: string, data: Record<string, any>): string {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return template.replace(/\$\{([\s\S]+?)\}/g, (match, p1) => {
const keys = p1.trim().split('.');
let value = data;
for (const key of keys) {
value = value[key];
if (value === undefined) {
return '';
}
}
return value;
});
}
26 changes: 6 additions & 20 deletions web/core/lib/editor/prompts/prompts-manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import i18next from "i18next";
import { DefinedVariable, FacetType, PromptAction } from "@/editor/defs/custom-action.type";
import ArticlePrompts from "@/editor/prompts/article-prompts";
import { TypeOptions } from "@/editor/defs/type-options.type";
import RequirementsPrompts from "@/editor/prompts/requirements-prompts";
import i18next from 'i18next';
import { DefinedVariable, FacetType, PromptAction } from '@/editor/defs/custom-action.type';
import ArticlePrompts from '@/editor/prompts/article-prompts';
import { TypeOptions } from '@/editor/defs/type-options.type';
import RequirementsPrompts from '@/editor/prompts/requirements-prompts';
import { render } from '@/editor/prompts/TemplateRender.ts';

export class PromptsManager {
private constructor() {
Expand Down Expand Up @@ -59,18 +60,3 @@ export class PromptsManager {
}
}

function render(template: string, data: object) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return template.replace(/\$\{([\s\S]+?)\}/g, (match, p1) => {
const keys = p1.trim().split('.');
let value = data;
for (const key of keys) {
value = value[key];
if (value === undefined) {
return '';
}
}
return value;
});
}

0 comments on commit 6e10d3e

Please sign in to comment.