Skip to content

Commit

Permalink
feat:template support base64 (#4627)
Browse files Browse the repository at this point in the history
* feat:template support base64

Signed-off-by: jingyang <3161362058@qq.com>

* add sharp

Signed-off-by: jingyang <3161362058@qq.com>

---------

Signed-off-by: jingyang <3161362058@qq.com>
  • Loading branch information
zjy365 authored Mar 27, 2024
1 parent 2085ee6 commit b2beb0a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/providers/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"remark-unwrap-images": "^3.0.1",
"sass": "^1.68.0",
"sealos-desktop-sdk": "workspace:^",
"sharp": "^0.32.6",
"swiper": "^11.0.7",
"zustand": "^4.4.1"
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/template/src/pages/api/listTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<

jsonRes(res, { data: { templates: templates, menuKeys: topKeys.join(',') }, code: 200 });
} catch (error) {
jsonRes(res, { code: 500, data: 'api listTemplate error' });
jsonRes(res, { code: 500, data: 'api listTemplate error', error: error });
}
}
21 changes: 21 additions & 0 deletions frontend/providers/template/src/utils/json-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,33 @@ export const parseTemplateString = (
inputs: Record<string, string>;
}
) => {
// support function list
const functionHandlers = [
{
name: 'base64',
handler: (value: string) => {
const regex = /base64\((.*?)\)/;
const match = value.match(regex);
if (match) {
const token = match[1];
const value = token.split('.').reduce((obj: any, prop: string) => obj[prop], dataSource);
return Buffer.from(value).toString('base64');
}
return value;
}
}
];

try {
const replacedString = sourceString.replace(regex, (match: string, key: string) => {
if (dataSource[key] && key.indexOf('.') === -1) {
return dataSource[key];
}
if (key.indexOf('.') !== -1) {
const hasMatchingFunction = functionHandlers.find(({ name }) => key.includes(`${name}(`));
if (hasMatchingFunction) {
return hasMatchingFunction.handler(key);
}
const value = key.split('.').reduce((obj: any, prop: string) => obj[prop], dataSource);
return value !== undefined ? value : match;
}
Expand Down

0 comments on commit b2beb0a

Please sign in to comment.