Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-98927

QTextEdit can't parse the markdown it generates

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 5.15.2
    • None
    • Qt 5.15.2
    • Windows

      The fundamental issue is QTextEdit doesn't seem to recognize inline Markdown tags (like * bold or _ italic) if the start tag is followed by a space and/or the end tag is preceded by a space, e.g.:

      • Fails to format *{{ text}}{}, {{ text *}}{}, *text *{}.
      • Correctly formats *text*.

      At the same time, if a section of text that starts or ends with a space is formatted by e.g. setCurrentCharFormat, then toMarkdown will generate Markdown of the above failing form.

      As a consequence, if the format of a block of text with spaces on either end is set programmatically, the following will break the formatting even though it should be expected to not change anything in this situation:

      textEdit->setMarkdown(textEdit->toMarkdown())
      

      Here is a complete demonstration:

      #include <QApplication>
      #include <QMainWindow>
      #include <QTextEdit>
      #include <QDebug>
      
      int main (int argc, char *argv[]) {
      
          QApplication a(argc, argv);
          QMainWindow w;
      
          QTextEdit *edit;
          w.setCentralWidget(edit = new QTextEdit("it's bold it's bold it's very bold"));
      
          // sets edit's text to bold from some start position to some end position.
          auto setBold = [&](int from, int to) {
              QTextCharFormat f;
              f.setFontWeight(QFont::Bold);
              QTextCursor c = edit->textCursor();
              c.setPosition(from);
              c.setPosition(to, QTextCursor::KeepAnchor);
              c.setCharFormat(f);
              edit->setTextCursor(c);
          };
      
          // set some sections of text to bold
          qDebug() << edit->toMarkdown();
          setBold(4, 9);
          setBold(15, 19);
          setBold(25, 30);
          qDebug() << edit->toMarkdown();
      
          // set it to the markdown it generates
          edit->setMarkdown(edit->toMarkdown());
          qDebug() << edit->toMarkdown();
      
          w.show();
          return a.exec();
      
      }
      

      That program will produce the output seen in the "actual.png" attachment. The expected output (which is also the result if the setMarkdown line is commented out) can be seen in the "expected.png" attachment.

      The debug output produced by that program, however, is as expected:

      {{"it's bold it's bold it's very bold\n\n"
      "it's** bold** it's *bold* it's **very **bold\n\n"
      "it's** bold** it's *bold* it's **very **bold\n\n"}}

      I am not sure what the restrictions regarding the placement of whitespace is in those tags. If the whitespace is not allowed, then the issue is in toMarkdown. If the whitespace is allowed, then the issue is in setMarkdown.

        1. actual.png
          11 kB
          Jason Cipriani
        2. expected.png
          9 kB
          Jason Cipriani
        For Gerrit Dashboard: QTBUG-98927
        # Subject Branch Project Status CR V

            srutledg Shawn Rutledge
            jasonc Jason Cipriani
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:

                There is 1 open Gerrit change