import { CONVERT_URL } from './config.js' function readFileAsBase64(file) { return new Promise((resolve, reject) => { const reader = new FileReader() reader.onload = () => { const dataUrl = typeof reader.result === 'string' ? reader.result : '' const splitIndex = dataUrl.indexOf(',') if (splitIndex === -1) { reject(new Error('Invalid file data')) return } resolve(dataUrl.slice(splitIndex + 1)) } reader.onerror = () => reject(reader.error || new Error('Failed to read file')) reader.readAsDataURL(file) }) } export async function convertFileToMarkdown(file) { const base64 = await readFileAsBase64(file) const res = await fetch(CONVERT_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ file: base64, filename: file.name || 'document', }), }) if (!res.ok) { const errorText = await res.text() throw new Error(`HTTP ${res.status}: ${errorText}`) } const data = await res.json() if (!data || typeof data.markdown !== 'string') { throw new Error('No markdown returned') } return data.markdown }