COMPREHENSIVE_TABLE_SUPPORT
Overview and Purpose
SimpleChat now supports comprehensive table rendering from multiple input formats, ensuring that tables generated by AI agents or pasted by users are properly displayed as styled HTML tables regardless of their original format.
Version implemented: 0.230.001
Problem Statement
AI agents generate tables in various formats that weren't being properly rendered:
- Unicode box-drawing tables (┌─┬─┐ style)
- Markdown tables wrapped in code blocks
- Pipe-separated values (PSV) in code blocks
- Standard markdown tables (worked but needed pipeline integration)
Users were seeing raw text instead of properly formatted tables, reducing readability and user experience.
Technical Specifications
Architecture Overview
The solution implements a preprocessing pipeline that detects and converts various table formats to standard markdown tables before the markdown parser processes them.
Processing Pipeline:
- Content cleaning and citation parsing
- Unwrap markdown tables from code blocks
- Convert Unicode box-drawing tables to markdown
- Convert pipe-separated values to markdown tables
- Parse with Marked.js (GFM tables enabled)
- Sanitize with DOMPurify
- Apply Bootstrap styling
File Structure
application/single_app/static/js/chat/
├── chat-messages.js # Main implementation
functional_tests/
├── test_comprehensive_table_support.py # Validation script
├── test_unicode_table_conversion.html # Unicode test page
└── test_psv_table_conversion.html # PSV test page
API Integration Points
No new API endpoints required. The table processing happens entirely on the client side during message rendering.
Implementation Details
1. unwrapTablesFromCodeBlocks()
Purpose: Detects markdown tables wrapped in code blocks and unwraps them for proper parsing.
Pattern: /```[\w]*\n((?:[^\n]*\|[^\n]*\n)+)```/g
Logic:
- Finds code blocks containing pipe-separated content
- Validates that content has multiple lines with pipes
- Removes code block wrapper, preserving table content
2. convertUnicodeTableToMarkdown()
Purpose: Converts Unicode box-drawing tables to standard markdown format.
Pattern: /┌[─┬┐]*┐[\s\S]*?└[─┴┘]*┘/g
Logic:
- Identifies complete Unicode table structures
- Extracts data rows (ignoring border/separator lines)
- Splits cells by
│character - Builds markdown table with header separator
Example transformation:
┌─────────┬─────────┐
│ Name │ Status │
├─────────┼─────────┤
│ App1 │ Active │
└─────────┴─────────┘
Becomes:
| Name | Status |
| --- | --- |
| App1 | Active |
3. convertPSVCodeBlockToMarkdown()
Purpose: Converts pipe-separated values in code blocks to markdown tables.
Pattern: /```[\w]*\n((?:[^|\n]+\|[^|\n]*(?:\|[^|\n]*)*\n)+)```/g
Logic:
- Detects code blocks with pipe-separated content
- Handles both header-only and header+data scenarios
- Limits display to 50 data rows (plus header) for performance
- Adds truncation notice for large datasets
Row Limit: First 50 rows displayed, with "... and X more rows" notice for larger datasets.
Bootstrap Integration
All generated tables automatically receive Bootstrap classes:
table- Base table stylingtable-striped- Alternating row colorstable-hover- Row highlighting on hovertable-bordered- Cell borders
DOMPurify Configuration
Table elements explicitly allowed through sanitization:
ALLOWED_TAGS: ['table', 'thead', 'tbody', 'tr', 'th', 'td', ...]
Usage Instructions
For Users
No configuration required. The feature works automatically:
- Paste any table format in a message
- AI-generated tables are automatically converted
- Tables display with professional Bootstrap styling
For Developers
Adding new table formats:
- Create detection regex pattern
- Implement conversion function following existing patterns
- Add to processing pipeline in
chat-messages.js - Create test cases in functional tests
Pipeline integration:
const withUnwrappedTables = unwrapTablesFromCodeBlocks(withInlineCitations);
const withMarkdownTables = convertUnicodeTableToMarkdown(withUnwrappedTables);
const withPSVTables = convertPSVCodeBlockToMarkdown(withMarkdownTables);
const htmlContent = DOMPurify.sanitize(marked.parse(withPSVTables));
Testing and Validation
Functional Tests
- test_comprehensive_table_support.py - Integration validation
- test_unicode_table_conversion.html - Browser testing for Unicode tables
- test_psv_table_conversion.html - Browser testing for PSV format
Test Coverage
✅ Unicode box-drawing table conversion
✅ Standard markdown table preservation
✅ Pipe-separated values in code blocks
✅ Markdown tables wrapped in code blocks
✅ Mixed content with multiple table formats
✅ Bootstrap styling application
✅ DOMPurify sanitization compatibility
✅ Large dataset handling (50+ row tables)
Performance Considerations
- Regex optimization: Patterns designed for efficient matching
- Row limiting: PSV tables limited to 50 visible rows
- Memory usage: Processing happens during render, no storage impact
- Load time: Minimal impact on message rendering speed
Examples and Screenshots
Example 1: Unicode Table Input
┌─────────────┬─────────┬────────────┐
│ Application │ Version │ Status │
├─────────────┼─────────┼────────────┤
│ Simple Chat │ 0.229 │ Active │
│ ESAM Agent │ 1.2.3 │ Testing │
└─────────────┴─────────┴────────────┘
Renders as: Professional HTML table with Bootstrap styling
Example 2: PSV Code Block Input
Application Name|Version|Environment|Status Simple Chat|0.229.005|Production|Active ESAM Agent|1.2.3|Development|Testing
Renders as: Formatted table with headers and data rows
Example 3: Mixed Format Support
Messages can contain multiple table formats simultaneously, and all will be properly converted and displayed.
Known Limitations
- Complex Unicode tables with merged cells not supported
- Very wide tables may require horizontal scrolling on mobile
- Nested tables are not supported (by design)
- Custom table styling beyond Bootstrap classes not preserved
Integration Notes
CSS Dependencies
Requires Bootstrap 5.x for optimal table styling. Tables will render without Bootstrap but with basic browser default styling.
JavaScript Dependencies
- Marked.js 15.0.7+ (GFM tables support)
- DOMPurify 3.1.3+ (HTML sanitization)
Browser Compatibility
Works in all modern browsers supporting ES6+ features. Tested in:
- Chrome 90+
- Firefox 88+
- Safari 14+
- Edge 90+
Future Enhancements
Potential Improvements
- Excel table paste support - Direct paste from spreadsheets
- CSV import handling - File upload table conversion
- Table editing capabilities - Inline cell editing
- Export functionality - Download tables as CSV/Excel
- Advanced styling options - Theme customization
- Column sorting - Interactive table features
Extensibility
The pipeline architecture allows easy addition of new table formats by:
- Adding new conversion functions
- Inserting into the processing pipeline
- Creating corresponding test cases
Version History
- 0.229.005 - Added comprehensive table support (Unicode, PSV, wrapped markdown)
- 0.229.004 - Initial Unicode table conversion
- 0.229.003 - Table preprocessing foundation
Related Documentation
- Feature implementation:
chat-messages.jslines 150-300 - Test validation:
functional_tests/test_comprehensive_table_support.py - HTML examples:
functional_tests/test_*_table_conversion.html