summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2022-06-20 22:19:34 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2022-06-24 22:42:43 +0200
commit846b314aaf484a3cb62d466660c08bbde00542cb (patch)
treecb2f3af13e11b5367cf006542b15d2ae3a83ebef
parent56f0ebfe860e440dcbba8997f44836debc901119 (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. Pick-to: 6.4 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>
-rw-r--r--src/gui/text/qtextmarkdownwriter.cpp18
-rw-r--r--tests/auto/gui/text/qtextmarkdownwriter/data/blockquotes.md3
-rw-r--r--tests/auto/gui/text/qtextmarkdownwriter/data/links.md3
3 files changed, 16 insertions, 8 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;
diff --git a/tests/auto/gui/text/qtextmarkdownwriter/data/blockquotes.md b/tests/auto/gui/text/qtextmarkdownwriter/data/blockquotes.md
index b29b53651b..a021f15aba 100644
--- a/tests/auto/gui/text/qtextmarkdownwriter/data/blockquotes.md
+++ b/tests/auto/gui/text/qtextmarkdownwriter/data/blockquotes.md
@@ -13,8 +13,7 @@ MacFarlane writes:
> > as readable as possible. The idea is that a Markdown-formatted document should
> > be publishable as-is, as plain text, without looking like it's been marked up
> > with tags or formatting instructions. (
-> > [http://daringfireball.net/projects/markdown/](http://daringfireball.net/projects/markdown/)
-> > )
+> > <http://daringfireball.net/projects/markdown/> )
> The point can be illustrated by comparing a sample of AsciiDoc with an
> equivalent sample of Markdown. Here is a sample of AsciiDoc from the AsciiDoc
diff --git a/tests/auto/gui/text/qtextmarkdownwriter/data/links.md b/tests/auto/gui/text/qtextmarkdownwriter/data/links.md
index 33cdb2b3ab..c9aae80c67 100644
--- a/tests/auto/gui/text/qtextmarkdownwriter/data/links.md
+++ b/tests/auto/gui/text/qtextmarkdownwriter/data/links.md
@@ -23,3 +23,6 @@ title")
* [link](/url "title
title title")
* nonlink
+
+Qt has the <https://qt.io> site
+