feat: enhance image path detection in input processing (#189)
I wanted to be able to drag and drop images while in the chat. Here it is. I have read the CLA Document and I hereby sign the CLA
This commit is contained in:
@@ -234,13 +234,34 @@ export default function TerminalChatInput({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// detect image file paths for dynamic inclusion
|
||||||
const images: Array<string> = [];
|
const images: Array<string> = [];
|
||||||
const text = inputValue
|
let text = inputValue;
|
||||||
.replace(/!\[[^\]]*?\]\(([^)]+)\)/g, (_m, p1: string) => {
|
// markdown-style image syntax: 
|
||||||
|
text = text.replace(/!\[[^\]]*?\]\(([^)]+)\)/g, (_m, p1: string) => {
|
||||||
|
images.push(p1.startsWith("file://") ? fileURLToPath(p1) : p1);
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
// quoted file paths ending with common image extensions (e.g. '/path/to/img.png')
|
||||||
|
text = text.replace(
|
||||||
|
/['"]([^'"]+?\.(?:png|jpe?g|gif|bmp|webp|svg))['"]/gi,
|
||||||
|
(_m, p1: string) => {
|
||||||
images.push(p1.startsWith("file://") ? fileURLToPath(p1) : p1);
|
images.push(p1.startsWith("file://") ? fileURLToPath(p1) : p1);
|
||||||
return "";
|
return "";
|
||||||
})
|
},
|
||||||
.trim();
|
);
|
||||||
|
// bare file paths ending with common image extensions
|
||||||
|
text = text.replace(
|
||||||
|
// eslint-disable-next-line no-useless-escape
|
||||||
|
/\b(?:\.[\/\\]|[\/\\]|[A-Za-z]:[\/\\])?[\w-]+(?:[\/\\][\w-]+)*\.(?:png|jpe?g|gif|bmp|webp|svg)\b/gi,
|
||||||
|
(match: string) => {
|
||||||
|
images.push(
|
||||||
|
match.startsWith("file://") ? fileURLToPath(match) : match,
|
||||||
|
);
|
||||||
|
return "";
|
||||||
|
},
|
||||||
|
);
|
||||||
|
text = text.trim();
|
||||||
|
|
||||||
const inputItem = await createInputItem(text, images);
|
const inputItem = await createInputItem(text, images);
|
||||||
submitInput([inputItem]);
|
submitInput([inputItem]);
|
||||||
|
|||||||
Reference in New Issue
Block a user