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 <volker.hilsheimer@qt.io>
This commit is contained in:
Shawn Rutledge 2021-10-13 23:00:31 +02:00
parent 74e634d82c
commit 5e55297ee0
2 changed files with 11 additions and 3 deletions

View File

@ -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;
}

View File

@ -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.