Skip to content

Commit 934bee3

Browse files
committed
fix: 修复SendToChat不支持多个文件的问题
1 parent 3930875 commit 934bee3

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/metaCommand.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1-
import * as fs from 'fs';
2-
import * as path from 'path';
3-
import {readFileAsUtf8} from './utiliti';
1+
import * as path from 'path';
2+
import { readFileAsUtf8 } from './utiliti';
43

5-
// Process @file:path placeholders in the message text
64
export async function processFilePlaceholder(messageText: string): Promise<string> {
7-
const filePlaceholderRegex = /^@file:([^\s\n]+)(?:\s|\n|$)/m;
8-
const match = messageText.match(filePlaceholderRegex);
5+
// 修改1:添加全局匹配标志/g,移除行首限制^
6+
const filePlaceholderRegex = /@file:([^\s\n]+)(?:\s|\n|$)/g;
7+
8+
// 修改2:存储所有匹配项
9+
const matches = [];
10+
let match;
11+
while ((match = filePlaceholderRegex.exec(messageText)) !== null) {
12+
matches.push(match);
13+
}
914

10-
if (!match) {
15+
if (matches.length === 0) {
1116
return messageText;
1217
}
1318

14-
const filePath = match[1];
15-
try {
16-
// Read file content
17-
const content = await readFileAsUtf8(filePath);
18-
const fileName = path.basename(filePath);
19+
let processedText = messageText;
20+
// 修改3:倒序处理(避免替换后索引变化)
21+
for (let i = matches.length - 1; i >= 0; i--) {
22+
const match = matches[i];
23+
const filePath = match[1];
24+
const fullMatch = match[0];
1925

20-
// Replace placeholder with formatted content
21-
return messageText.replace(
22-
filePlaceholderRegex,
23-
`<FILE_UPLOAD data-path="${filePath}"><content>\n${content}\n</content></FILE_UPLOAD>`
24-
);
25-
} catch (error) {
26-
return messageText.replace(
27-
filePlaceholderRegex,
28-
`<FILE_UPLOAD data-path="${filePath}">\n**Error reading file**: ${(error as Error).message}\n</FILE_UPLOAD>`
29-
);
26+
try {
27+
const content = await readFileAsUtf8(filePath);
28+
const replacement = `<FILE_UPLOAD data-path="${filePath}"><content>\n${content}\n</content></FILE_UPLOAD>`;
29+
processedText = processedText.substring(0, match.index) +
30+
replacement +
31+
processedText.substring(match.index + fullMatch.length);
32+
} catch (error) {
33+
const replacement = `<FILE_UPLOAD data-path="${filePath}">\n**Error reading file**: ${(error as Error).message}\n</FILE_UPLOAD>`;
34+
processedText = processedText.substring(0, match.index) +
35+
replacement +
36+
processedText.substring(match.index + fullMatch.length);
37+
}
3038
}
39+
40+
return processedText;
3141
}

0 commit comments

Comments
 (0)