Back to Lobehub

@lobechat/file-loaders

packages/file-loaders/README.md

2.1.566.4 KB
Original Source

@lobechat/file-loaders

@lobechat/file-loaders is a toolkit within the LobeHub project, specifically designed for loading various types of files from local file paths and converting their content into standardized Document object arrays.

Its primary purpose is to provide a unified interface for reading different file formats, extracting their core text content, and preparing them for subsequent processing (such as file preview, content extraction, or serving as knowledge base data sources in LobeHub).

✨ Features

  • Unified Interface: Provides loadFile(filePath: string) function as the core entry point.
  • Automatic Type Detection: Automatically selects appropriate loading methods based on file extensions.
  • Extensive Format Support:
    • Plain Text: .txt, .csv, .md, .json, .xml, .yaml, .html and various code and configuration file formats.
    • PDF: .pdf files.
    • Word: .docx files.
    • Excel: .xlsx, .xls files, with each worksheet as a Page.
    • PowerPoint: .pptx files, with each slide as a Page.
  • Standardized Output: Always returns Promise<Document>. A Document object represents a loaded file, containing an array of Page objects that represent the logical units of the file (pages, slides, worksheets, text blocks, etc.).
  • Hierarchical Structure: Uses a structure where Document contains Page[], better reflecting the original organization of the file.
  • Rich Metadata: Provides detailed metadata at both Document and Page levels, including file information, content statistics, and structural information.

Core Data Structures

The loadFile function returns a FileDocument object containing file-level information and all its logical pages/blocks (DocumentPage).

FileDocument Interface

FieldTypeDescription
contentstringFile content (aggregated content)
createdTimeDateFile creation timestamp.
fileTypestringFile type or extension.
filenamestringOriginal filename.
metadataobjectFile-level metadata.
metadata.authorstring?Document author (if available).
metadata.errorstring?Error information if the entire file loading failed.
metadata.titlestring?Document title (if available).
...anyOther file-level metadata.
modifiedTimeDateFile last modified timestamp.
pagesDocumentPage[]?Array containing all logical pages/blocks in the document (optional).
sourcestringFull path of the original file.
totalCharCountnumberTotal character count of the entire document (sum of all DocumentPage charCount).
totalLineCountnumberTotal line count of the entire document (sum of all DocumentPage lineCount).

DocumentPage Interface

FieldTypeDescription
charCountnumberCharacter count of this page/block content.
lineCountnumberLine count of this page/block content.
metadataobjectMetadata related to this page/block.
metadata.chunkIndexnumber?Current chunk index if split into chunks.
metadata.errorstring?Error occurred when processing this page/block.
metadata.lineNumberEndnumber?End line number in the original file.
metadata.lineNumberStartnumber?Start line number in the original file.
metadata.pageNumbernumber?Page number (applicable to PDF, DOCX).
metadata.sectionTitlestring?Related section title.
metadata.sheetNamestring?Worksheet name (applicable to XLSX).
metadata.slideNumbernumber?Slide number (applicable to PPTX).
metadata.totalChunksnumber?Total chunks if split into chunks.
...anyOther page/block-specific metadata.
pageContentstringCore text content of this page/block.

🤝 Contribution

File formats and parsing requirements are constantly evolving. We welcome community contributions to expand format support and improve parsing accuracy. You can participate in improvements through:

How to Contribute

  1. New File Format Support: Add support for additional file types
  2. Parser Improvements: Enhance existing parsers for better content extraction
  3. Metadata Enhancement: Improve metadata extraction capabilities
  4. Performance Optimization: Optimize file loading and processing performance

Contribution Process

  1. Fork the LobeHub repository
  2. Add new format support or improve existing parsers
  3. Submit a Pull Request describing:
  • New file formats supported or improvements made
  • Testing with various file samples
  • Performance impact analysis
  • Documentation updates

📌 Note

This is an internal module of LobeHub ("private": true), designed specifically for LobeHub and not published as a standalone package.

If you're interested in our project, feel free to check it out, star it, or contribute code on GitHub!