Office Document Conversion
Relevant source files
The following files were used as context for generating this wiki page:
Office Document Conversion in the ThinkEx platform provides the infrastructure to ingest, validate, and transform proprietary Microsoft Office formats (Word, Excel, and PowerPoint) into standardized formats compatible with the workspace's visual canvas and AI reasoning tools. This system ensures that legacy document formats are converted to PDFs for OCR processing or exported to collaborative environments like Google Docs.
The conversion logic acts as a bridge between raw file uploads and the internal DocumentData or PdfData structures. By standardizing these files, the system can extract textual content via OCR or internal Markdown extraction, making the knowledge within Office documents accessible for AI-assisted analysis and persistent storage.
Validation and Metadata Detection
The platform identifies Office documents based on a combination of file extensions and MIME types. This detection is critical for routing files to the correct conversion service (e.g., iLovePDF) or internal processing workflows.
Supported Formats and Mappings
The system defines specific metadata for three primary Office categories:
- Word:
.doc,.docx(application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document) - Excel:
.xls,.xlsx(application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet) - PowerPoint:
.ppt,.pptx(application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation)
Sources: src/lib/uploads/office-document-validation.ts:7-33
Validation Logic
Validation is performed using functions like isOfficeDocument(file: File) which aggregate specific checks for each document type. The utility also provides a way to retrieve canonical MIME types for files that might have been uploaded with generic or incorrect content types.
The diagram above shows the logic flow for identifying and categorizing uploaded Office files for further processing.
Sources: src/lib/uploads/office-document-validation.ts:80-140
Asset Processing Workflow
When a workspace is automatically generated or files are uploaded, the system utilizes an asset processing pipeline to handle the transition from raw file to workspace item.
Upload and Item Creation
During the autogen process, uploaded assets are validated and used to build item definitions. If a file is recognized as a document, it is assigned a position on the spatial canvas and an UploadedAsset object is created.
Sources: src/app/api/workspaces/autogen/route.ts:391-410
Conversion Routing
For Office documents, the system retrieves a conversion URL that points to external services capable of transforming the proprietary format into a PDF. This allows the ThinkEx platform to utilize its existing PDF OCR and rendering infrastructure for Office content.
| Document Type | External Conversion Utility | Target Format |
|---|---|---|
| Word | https://www.ilovepdf.com/word_to_pdf | |
| Excel | https://www.ilovepdf.com/excel_to_pdf | |
| PowerPoint | https://www.ilovepdf.com/powerpoint_to_pdf |
Sources: src/lib/uploads/office-document-validation.ts:36-40
Export to Google Docs
ThinkEx supports a "Notes-to-Export" flow where internal Markdown documents can be converted into native Google Docs. This involves a multi-step transformation process to ensure formatting and math expressions are preserved.
Markdown Transformation
Before the content is sent to the Google Drive API, it undergoes preprocessing to:
- Protect Code Spans: Temporary placeholders replace code blocks to prevent accidental transformation.
- Stash Currency: Escaped dollar signs are protected to ensure they aren't confused with math delimiters.
- LaTeX to Unicode: Math expressions (between
$or$$) are converted into Unicode equivalents using theunicodeitlibrary.
Sources: src/lib/exportToGoogleDocs.ts:8-40
Export Sequence
The export process utilizes the Google Identity Services (GIS) to obtain an OAuth2 access token with the drive.file scope before performing a multipart upload to the Google Drive API.
This sequence diagram illustrates the secure communication flow required to convert a Markdown item into a Google Doc.
Sources: src/lib/exportToGoogleDocs.ts:167-227
Data Models and Formatting
Converted documents are stored within the workspace using the Item type, with specific data structures for documents and PDFs.
Document and PDF Data Structures
- DocumentData: Stores raw Markdown, processed HTML, and an array of sources (title and URL).
- PdfData: Stores filename, source URL, OCR status, and an array of
ocrPagescontaining extracted Markdown and table data.
System Prompt Formatting
When the AI assistant interacts with converted documents, the system formats the items into a specific text structure. For documents, this includes the trimmed Markdown body followed by a "Sources" block. For PDFs, it includes page-by-page OCR content with table placeholders replaced by actual table text.
Sources: src/lib/utils/format-workspace-context.ts:413-463, src/lib/utils/format-workspace-context.ts:517-531
Markdown Extraction from Rendered Content
To support conversion back from the UI to the data layer, the markdown-extractor utility converts HTML DOM nodes back into Markdown syntax. This is used when a user selects text in the UI to create a new document or reply.
| HTML Tag | Markdown Transformation |
|---|---|
h1 - h6 | # to ###### headers |
strong / b | **text** |
blockquote | > text |
ul / ol | - / 1. list items |
pre / code | ``` code blocks |
table | GFM Table syntax |
Sources: src/lib/utils/markdown-extractor.ts:15-85
Conclusion
The Office Document Conversion system is a vital component for ensuring content interoperability within ThinkEx. By combining robust validation with external conversion routing and native Google Drive integration, the platform allows users to bring disparate data formats into a unified, AI-readable workspace. This process transforms static files into dynamic, persistent knowledge cards that can be exported or synthesized by AI models.