diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2022-06-20 22:19:34 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-06-24 23:18:46 +0000 |
commit | d4db1ef7db2a64f778c281de87c9c0db7966b7ce (patch) | |
tree | 9ded8325b38d3174966df7a42daa6f2269191580 /src | |
parent | 62a7360809ac642d2dd20e4fb55af9800ba9936b (diff) |
Emit autolinks in QTextMarkdownWriter
When a markdown document contains a "naked" URL, or an angle-bracketed
<URL>, md4c recognizes it, and we set the AnchorHref charfmt property.
There's no need to expand it into the [text](url) form if the text is
the same as the url, there is no tooltip, and the url is valid.
QTextMarkdownWriter now writes a CommonMark "autolink" in that case:
https://spec.commonmark.org/0.30/#autolinks
[ChangeLog][QtGui][Text] QTextMarkdownWriter now writes an autolink
whenever a hyperlink has no custom text and no tooltip, including
when the document was parsed from Markdown containing a naked URL.
Fixes: QTBUG-94713
Change-Id: I432db8499c62e1e0b1e913bfd8ef2147e3c2bb2a
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
(cherry picked from commit 846b314aaf484a3cb62d466660c08bbde00542cb)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/text/qtextmarkdownwriter.cpp | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/gui/text/qtextmarkdownwriter.cpp b/src/gui/text/qtextmarkdownwriter.cpp index 9f651a04fd..cda1f209ad 100644 --- a/src/gui/text/qtextmarkdownwriter.cpp +++ b/src/gui/text/qtextmarkdownwriter.cpp @@ -484,13 +484,19 @@ int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ign m_stream << s; col += s.length(); } else if (fmt.hasProperty(QTextFormat::AnchorHref)) { - QString s = u'[' + fragmentText + "]("_L1 + - fmt.property(QTextFormat::AnchorHref).toString(); - if (fmt.hasProperty(QTextFormat::TextToolTip)) { - s += Space; - s += createLinkTitle(fmt.property(QTextFormat::TextToolTip).toString()); + const auto href = fmt.property(QTextFormat::AnchorHref).toString(); + const bool hasToolTip = fmt.hasProperty(QTextFormat::TextToolTip); + QString s; + if (!hasToolTip && href == fragmentText && !QUrl(href, QUrl::StrictMode).scheme().isEmpty()) { + s = u'<' + href + u'>'; + } else { + s = u'[' + fragmentText + "]("_L1 + href; + if (hasToolTip) { + s += Space; + s += createLinkTitle(fmt.property(QTextFormat::TextToolTip).toString()); + } + s += u')'; } - s += u')'; if (wrap && col + s.length() > ColumnLimit) { m_stream << Newline << wrapIndentString; col = m_wrappedLineIndent; |