export async function downloadAttachmentsAndRewriteText()

in src/github/junie/attachment-downloader.ts [58:110]


export async function downloadAttachmentsAndRewriteText(text: string): Promise<string> {
    let updatedText = text;

    // Handle HTML image tags with user-attachments URLs
    const imgMatches = [...text.matchAll(ATTACHMENT_PATTERNS.imgTag)];
    for (const match of imgMatches) {
        const url = match[1];
        try {
            const localPath = await downloadFile(url);
            updatedText = updatedText.replace(match[0], `src="${localPath}"`);
        } catch (error) {
            console.error(`Failed to download image: ${url}`, error);
        }
    }

    // Handle markdown images: ![alt](url)
    const mdImgMatches = [...text.matchAll(ATTACHMENT_PATTERNS.markdownImg)];
    for (const match of mdImgMatches) {
        const url = match[1];
        try {
            const localPath = await downloadFile(url);
            updatedText = updatedText.replace(match[1], localPath);
        } catch (error) {
            console.error(`Failed to download markdown image: ${url}`, error);
        }
    }

    // Handle markdown file links: [text](url)
    const fileMatches = [...text.matchAll(ATTACHMENT_PATTERNS.file)];
    for (const match of fileMatches) {
        const url = match[1];
        try {
            const localPath = await downloadFile(url);
            updatedText = updatedText.replace(match[0], `(${localPath})`);
        } catch (error) {
            console.error(`Failed to download file: ${url}`, error);
        }
    }

    // Handle legacy user-images URLs
    const legacyMatches = [...text.matchAll(ATTACHMENT_PATTERNS.legacy)];
    for (const match of legacyMatches) {
        const url = match[0];
        try {
            const localPath = await downloadFile(url);
            updatedText = updatedText.replace(url, localPath);
        } catch (error) {
            console.error(`Failed to download legacy image: ${url}`, error);
        }
    }

    return updatedText;
}