Repository: nomic-ai/gpt4all
File: gpt4all-chat/src/xlsxtomd.cpp (C++)

Code before:
using namespace Qt::Literals::StringLiterals;


static QString formatCellText(const QXlsx::Cell *cell)
{
    if (!cell) return QString();

    QVariant value = cell->value();
    QXlsx::Format format = cell->format();
    QString cellText;

    // Determine the cell type based on format
    if (format.isDateTimeFormat()) {
        // Handle DateTime
        QDateTime dateTime = value.toDateTime();
        cellText = dateTime.isValid() ? dateTime.toString(QStringView(u"yyyy-MM-dd")) : value.toString();
    } else {
        cellText = value.toString();
    }

    if (cellText.isEmpty())
        return QString();

    // Escape special characters
    static QRegularExpression special(uR"([\\`*_{}[\]()#+-.!])"_s);
    cellText.replace(special, uR"(\\1)"_s);
    cellText.replace(u'&', "&amp;"_L1);
    cellText.replace(u'<', "&lt;"_L1);
    cellText.replace(u'>', "&gt;"_L1);

    // Apply Markdown formatting based on font styles
    if (format.fontUnderline())
        cellText = u"_%1_"_s.arg(cellText);
    if (format.fontBold())
        cellText = u"**%1**"_s.arg(cellText);
    if (format.fontItalic())
        cellText = u"*%1*"_s.arg(cellText);
    if (format.fontStrikeOut())
        cellText = u"~~%1~~"_s.arg(cellText);

    return cellText;
}

static QString getCellValue(QXlsx::Worksheet *sheet, int row, int col)
{
    if (!sheet)
        return QString();

    // Attempt to retrieve the cell directly
    std::shared_ptr<QXlsx::Cell> cell = sheet->cellAt(row, col);


Review comment (suggestion):
The escaping is worse. I am getting this kind of md on the walt disney xlsx example for produced markdown:

```
| | | | | | | |
|---|---|---|---|---|---|---|
|~~***_Walt Disney Co\\1_***~~| | | | | | |
|~~***_Consolidated Income Statement_***~~| | | | | | |
| | | | | | | |
|US$ in millions| | | | | | |
|~~***_12 months ended:_***~~|~~***_45199_***~~|~~***_44835_***~~|~~***_44471_***~~|~~***_44107_***~~|~~***_43736_***~~|~~***_43372_***~~|
|Services|79562\\10|74200\\10|61768\\10|59265\\10|60542\\10|50869\\10|
|Products|9336\\10|8522\\10|5650\\10|6123\\10|9028\\10|8565\\10|
|~~***_Revenues_***~~|~~***_88898\\10_***~~|~~***_82722\\10_***~~|~~***_67418\\10_***~~|~~***_65388\\10_***~~|~~***_69570\\10_***~~|~~***_59434\\10_***~~|
|Cost of services\\1 exclusive of depreciation and amortization|\\153139\\10|\\148962\\10|\\141129\\10|\\139406\\10|\\136450\\10|\\127528\\10|
```

Which looks like this:

![image](https://github.com/user-attachments/assets/859d1e4a-110b-4f51-bfec-12d485bf4622)




Code after:
using namespace Qt::Literals::StringLiterals;


static QString formatCellText(const QXlsx::Cell *cell)
{
    if (!cell) return QString();

    QVariant value = cell->value();
    QXlsx::Format format = cell->format();
    QString cellText;

    // Determine the cell type based on format
    if (cell->isDateTime()) {
        // Handle DateTime
        QDateTime dateTime = cell->dateTime().toDateTime();
        cellText = dateTime.isValid() ? dateTime.toString(QStringView(u"yyyy-MM-dd")) : value.toString();
    } else {
        cellText = value.toString();
    }

    if (cellText.isEmpty())
        return QString();

    // Escape special characters
    static QRegularExpression special(
        QStringLiteral(
            R"(()([\\`*_[\]<>()!|])|)"    // special characters
            R"(^(\s*)(#+(?:\s|$))|)"      // headings
            R"(^(\s*[0-9])(\.(?:\s|$))|)" // ordered lists ("1. a")
            R"(^(\s*)([+-](?:\s|$)))"     // unordered lists ("- a")
        ),
        QRegularExpression::MultilineOption
    );
    cellText.replace(special, uR"(\1\\2)"_s);
    cellText.replace(u'&', "&amp;"_L1);
    cellText.replace(u'<', "&lt;"_L1);
    cellText.replace(u'>', "&gt;"_L1);

    // Apply Markdown formatting based on font styles
    if (format.fontUnderline())
        cellText = u"_%1_"_s.arg(cellText);
    if (format.fontBold())
        cellText = u"**%1**"_s.arg(cellText);
    if (format.fontItalic())
        cellText = u"*%1*"_s.arg(cellText);
    if (format.fontStrikeOut())
        cellText = u"~~%1~~"_s.arg(cellText);

    return cellText;
}