feat(MilkdownEditor): add file upload support for documents and text files
Added new upload functionality to the editor supporting doc/docx/ppt/pptx/pdf/zip/txt/json files. Includes: - New upload button with file input - File type detection utilities (isTextFile, isConvertibleFile) - Initial markdown sync with trailing whitespace normalization - Warning messages for unsupported file types
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { CONVERT_URL } from './config.js'
|
||||
|
||||
const API_KEY = 'your-secret-key-here'
|
||||
|
||||
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',
|
||||
'X-API-Key': API_KEY,
|
||||
},
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user