From 5e55297ee01b20c9cd4119a6e5757a0b4aafd620 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Wed, 13 Oct 2021 23:00:31 +0200 Subject: Markdown writer: indent fence consistent with code block - Under a list item, we've been indenting code blocks: ``` int main() ... ``` - But it's also ok *not* to indent (and github handles that better): ``` int main() ... ``` - There was a bug that when the code is not indented, the fence would be indented anyway: ``` int main() ... ``` and that was not OK, neither for md4c nor for github. Now with this change, either way is rewritable: you can read markdown into QTextDocument, make small edits and write it back out again, with the indentation being preserved (the code block is either part of the list item, thus indented, or else it's outside the list completely). Pick-to: 6.2 Task-number: QTBUG-92445 Change-Id: I5f51899e28ba9f09b88a71e640d9283416cce171 Reviewed-by: Volker Hilsheimer --- src/gui/text/qtextmarkdownwriter.cpp | 7 ++++--- tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/gui/text/qtextmarkdownwriter.cpp b/src/gui/text/qtextmarkdownwriter.cpp index 26e41149b2..e99242218c 100644 --- a/src/gui/text/qtextmarkdownwriter.cpp +++ b/src/gui/text/qtextmarkdownwriter.cpp @@ -370,8 +370,7 @@ int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ign const bool codeBlock = blockFmt.hasProperty(QTextFormat::BlockCodeFence) || blockFmt.stringProperty(QTextFormat::BlockCodeLanguage).length() > 0; if (m_fencedCodeBlock && !codeBlock) { - m_stream << m_linePrefix << QString(m_wrappedLineIndent, Space) - << m_codeBlockFence << Newline; + m_stream << m_linePrefix << m_codeBlockFence << Newline; m_fencedCodeBlock = false; m_codeBlockFence.clear(); } @@ -445,8 +444,10 @@ int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ign if (fenceChar.isEmpty()) fenceChar = QLatin1String("`"); m_codeBlockFence = QString(3, fenceChar.at(0)); + if (blockFmt.hasProperty(QTextFormat::BlockIndent)) + m_codeBlockFence = QString(m_wrappedLineIndent, Space) + m_codeBlockFence; // A block quote can contain an indented code block, but not vice-versa. - m_stream << m_linePrefix << QString(m_wrappedLineIndent, Space) << m_codeBlockFence + m_stream << m_linePrefix << m_codeBlockFence << blockFmt.stringProperty(QTextFormat::BlockCodeLanguage) << Newline; m_fencedCodeBlock = true; } diff --git a/tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md b/tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md index 54e3f25afa..b3539167ab 100644 --- a/tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md +++ b/tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md @@ -22,3 +22,10 @@ - still didn't fix it, expecting a breakthrough any day now - some sort of miracle - profit! +- Alternatively we can have a non-indented fenced code block under a list item: + +```qml +import QtQuick +Text { text: "hello world" } +``` +- but that means the code block is not part of the list item. -- cgit v1.2.3