Skip to content

Commit ff322ed

Browse files
authored
feat: Add filename parameter to markdownTransform tool (#316)
1 parent 5e133f0 commit ff322ed

File tree

6 files changed

+69
-16
lines changed

6 files changed

+69
-16
lines changed

modules/tool/packages/markdownTransform/bun.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"lockfileVersion": 1,
3+
"configVersion": 0,
34
"workspaces": {
45
"": {
56
"name": "fastgpt-tools-markdownTransform",

modules/tool/packages/markdownTransform/config.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,51 @@ export default defineTool({
1313
en: 'Convert Markdown to specified format file, return the file link please download it in time.'
1414
},
1515
versionList: [
16+
{
17+
value: '0.2.0',
18+
description: '支持自定义文件名',
19+
inputs: [
20+
{
21+
key: 'markdown',
22+
label: 'Markdown 内容',
23+
description: '要转换的 Markdown 内容',
24+
toolDescription: '要转换的 Markdown 内容',
25+
required: true,
26+
renderTypeList: [FlowNodeInputTypeEnum.input, FlowNodeInputTypeEnum.reference],
27+
valueType: WorkflowIOValueTypeEnum.string
28+
},
29+
{
30+
key: 'format',
31+
label: '转换格式',
32+
description: '需要转换的格式,支持 xlsx 和 docx 和 pptx',
33+
toolDescription: '需要转换的格式,支持 xlsx 和 docx 和 pptx',
34+
required: true,
35+
renderTypeList: [FlowNodeInputTypeEnum.select, FlowNodeInputTypeEnum.reference],
36+
valueType: WorkflowIOValueTypeEnum.string,
37+
list: [
38+
{ label: 'xlsx', value: 'xlsx' },
39+
{ label: 'docx', value: 'docx' },
40+
{ label: 'pptx', value: 'pptx' }
41+
]
42+
},
43+
{
44+
key: 'filename',
45+
label: '文件名',
46+
description: '自定义文件名(不包含扩展名)',
47+
toolDescription: '自定义文件名(不包含扩展名)',
48+
required: false,
49+
renderTypeList: [FlowNodeInputTypeEnum.input, FlowNodeInputTypeEnum.reference],
50+
valueType: WorkflowIOValueTypeEnum.string
51+
}
52+
],
53+
outputs: [
54+
{
55+
valueType: WorkflowIOValueTypeEnum.string,
56+
key: 'url',
57+
label: '文件链接'
58+
}
59+
]
60+
},
1661
{
1762
value: '0.1.0',
1863
description: 'Default version',

modules/tool/packages/markdownTransform/src/docx.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
} from './shared';
2424

2525
export const InputType = z.object({
26-
markdown: z.string().describe('Markdown content to convert')
26+
markdown: z.string().describe('Markdown content to convert'),
27+
filename: z.string().optional().describe('Custom filename without extension')
2728
});
2829

2930
function createTextRun(
@@ -361,7 +362,8 @@ async function parseMarkdownToParagraphs(markdown: string): Promise<(Paragraph |
361362
}
362363

363364
export async function docxTool({
364-
markdown
365+
markdown,
366+
filename
365367
}: z.infer<typeof InputType>): Promise<z.infer<typeof OutputType>> {
366368
const elements = await parseMarkdownToParagraphs(markdown);
367369

@@ -375,12 +377,12 @@ export async function docxTool({
375377
});
376378

377379
const docBuffer = await Packer.toBuffer(doc);
378-
const filename = `markdown-to-docx.docx`;
380+
const finalFilename = filename ? `${filename}.docx` : `markdown-to-docx.docx`;
379381
const buf = Buffer.from(new Uint8Array(docBuffer));
380382

381383
const result = await uploadFile({
382384
buffer: buf,
383-
defaultFilename: filename
385+
defaultFilename: finalFilename
384386
});
385387

386388
if (!result.accessUrl) {

modules/tool/packages/markdownTransform/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ import { OutputType } from './type';
66

77
export const InputType = z.object({
88
format: z.enum(['xlsx', 'docx', 'pptx']),
9-
markdown: z.string()
9+
markdown: z.string(),
10+
filename: z.string().optional()
1011
});
1112

1213
export async function tool({
1314
format,
14-
markdown
15+
markdown,
16+
filename
1517
}: z.infer<typeof InputType>): Promise<z.infer<typeof OutputType>> {
1618
if (format === 'xlsx') {
17-
return xlsxTool({ markdown });
19+
return xlsxTool({ markdown, filename });
1820
}
1921
if (format === 'docx') {
20-
return docxTool({ markdown });
22+
return docxTool({ markdown, filename });
2123
}
2224
if (format === 'pptx') {
23-
return pptxTool({ markdown });
25+
return pptxTool({ markdown, filename });
2426
}
2527
return Promise.reject('Invalid format');
2628
}

modules/tool/packages/markdownTransform/src/pptx.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
} from './shared';
1212

1313
export const InputType = z.object({
14-
markdown: z.string().describe('Markdown content to convert')
14+
markdown: z.string().describe('Markdown content to convert'),
15+
filename: z.string().optional().describe('Custom filename without extension')
1516
});
1617

1718
// slide styles config
@@ -444,12 +445,12 @@ async function parseMarkdownToPptx(markdown: string): Promise<Buffer> {
444445
export async function pptxTool(
445446
input: z.infer<typeof InputType>
446447
): Promise<z.infer<typeof OutputType>> {
447-
const { markdown } = input;
448+
const { markdown, filename } = input;
448449
const pptxBuffer = await parseMarkdownToPptx(markdown);
449-
const filename = `markdown-to-pptx.pptx`;
450+
const finalFilename = filename ? `${filename}.pptx` : `markdown-to-pptx.pptx`;
450451
const result = await uploadFile({
451452
buffer: pptxBuffer,
452-
defaultFilename: filename
453+
defaultFilename: finalFilename
453454
});
454455
if (!result.accessUrl) {
455456
return Promise.reject('Upload failed: No access URL in result');

modules/tool/packages/markdownTransform/src/xlsx.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function getExcelImageExtension(url: string): 'png' | 'jpeg' | 'gif' {
2020
}
2121

2222
export const InputType = z.object({
23-
markdown: z.string().describe('Markdown content to convert')
23+
markdown: z.string().describe('Markdown content to convert'),
24+
filename: z.string().optional().describe('Custom filename without extension')
2425
});
2526

2627
async function processLineWithImages(
@@ -331,12 +332,13 @@ async function createExcelFromMarkdown(markdown: string): Promise<Buffer> {
331332
export async function xlsxTool(
332333
input: z.infer<typeof InputType>
333334
): Promise<z.infer<typeof OutputType>> {
334-
const { markdown } = input;
335+
const { markdown, filename } = input;
335336
try {
336337
const xlsxBuffer = await createExcelFromMarkdown(markdown);
338+
const finalFilename = filename ? `${filename}.xlsx` : `markdown-to-excel.xlsx`;
337339
const result = await uploadFile({
338340
buffer: xlsxBuffer,
339-
defaultFilename: `markdown-to-excel.xlsx`
341+
defaultFilename: finalFilename
340342
});
341343

342344
return { url: result.accessUrl };

0 commit comments

Comments
 (0)