Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure markdown images have a line break when following a list #2346

Merged
merged 13 commits into from
Jun 2, 2024
22 changes: 22 additions & 0 deletions launcher/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,25 @@ QPair<QString, QString> StringUtils::splitFirst(const QString& s, const QRegular
right = s.mid(end);
return qMakePair(left, right);
}

static const QRegularExpression ulMatcher("<\\s*/\\s*ul\\s*>");

QString StringUtils::htmlListPatch(QString htmlStr)
{
int pos = htmlStr.indexOf(ulMatcher);
int imgPos;
while (pos != -1) {
pos = htmlStr.indexOf(">", pos) + 1; // Get the size of the </ul> tag. Add one for zeroeth index
imgPos = htmlStr.indexOf("<img ", pos);
if (imgPos == -1)
break; // no image after the tag

auto textBetween = htmlStr.mid(pos, imgPos - pos).trimmed(); // trim all white spaces

if (textBetween.isEmpty())
htmlStr.insert(pos, "<br>");

pos = htmlStr.indexOf(ulMatcher, pos);
}
return htmlStr;
}
2 changes: 2 additions & 0 deletions launcher/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ QPair<QString, QString> splitFirst(const QString& s, const QString& sep, Qt::Cas
QPair<QString, QString> splitFirst(const QString& s, QChar sep, Qt::CaseSensitivity cs = Qt::CaseSensitive);
QPair<QString, QString> splitFirst(const QString& s, const QRegularExpression& re);

QString htmlListPatch(QString htmlStr);

} // namespace StringUtils
5 changes: 3 additions & 2 deletions launcher/ui/dialogs/AboutDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "Application.h"
#include "BuildConfig.h"
#include "Markdown.h"
#include "StringUtils.h"
#include "ui_AboutDialog.h"

#include <net/NetJob.h>
Expand Down Expand Up @@ -139,10 +140,10 @@ AboutDialog::AboutDialog(QWidget* parent) : QDialog(parent), ui(new Ui::AboutDia
setWindowTitle(tr("About %1").arg(launcherName));

QString chtml = getCreditsHtml();
ui->creditsText->setHtml(chtml);
ui->creditsText->setHtml(StringUtils::htmlListPatch(chtml));

QString lhtml = getLicenseHtml();
ui->licenseText->setHtml(lhtml);
ui->licenseText->setHtml(StringUtils::htmlListPatch(lhtml));

ui->urlLabel->setOpenExternalLinks(true);

Expand Down
5 changes: 3 additions & 2 deletions launcher/ui/dialogs/ExportToModListDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QTextEdit>
#include "FileSystem.h"
#include "Markdown.h"
#include "StringUtils.h"
#include "modplatform/helpers/ExportToModList.h"
#include "ui_ExportToModListDialog.h"

Expand Down Expand Up @@ -136,10 +137,10 @@ void ExportToModListDialog::triggerImp()
case ExportToModList::CUSTOM:
return;
case ExportToModList::HTML:
ui->resultText->setHtml(txt);
ui->resultText->setHtml(StringUtils::htmlListPatch(txt));
break;
case ExportToModList::MARKDOWN:
ui->resultText->setHtml(markdownToHTML(txt));
ui->resultText->setHtml(StringUtils::htmlListPatch(markdownToHTML(txt)));
break;
case ExportToModList::PLAINTXT:
break;
Expand Down
3 changes: 2 additions & 1 deletion launcher/ui/dialogs/ModUpdateDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "CustomMessageBox.h"
#include "ProgressDialog.h"
#include "ScrollMessageBox.h"
#include "StringUtils.h"
#include "minecraft/mod/tasks/GetModDependenciesTask.h"
#include "modplatform/ModIndex.h"
#include "modplatform/flame/FlameAPI.h"
Expand Down Expand Up @@ -473,7 +474,7 @@ void ModUpdateDialog::appendMod(CheckUpdateTask::UpdatableMod const& info, QStri
break;
}

changelog_area->setHtml(text);
changelog_area->setHtml(StringUtils::htmlListPatch(text));
changelog_area->setOpenExternalLinks(true);
changelog_area->setLineWrapMode(QTextBrowser::LineWrapMode::WidgetWidth);
changelog_area->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
Expand Down
3 changes: 2 additions & 1 deletion launcher/ui/dialogs/UpdateAvailableDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Application.h"
#include "BuildConfig.h"
#include "Markdown.h"
#include "StringUtils.h"
#include "ui_UpdateAvailableDialog.h"

UpdateAvailableDialog::UpdateAvailableDialog(const QString& currentVersion,
Expand All @@ -43,7 +44,7 @@ UpdateAvailableDialog::UpdateAvailableDialog(const QString& currentVersion,
ui->icon->setPixmap(APPLICATION->getThemedIcon("checkupdate").pixmap(64));

auto releaseNotesHtml = markdownToHTML(releaseNotes);
ui->releaseNotes->setHtml(releaseNotesHtml);
ui->releaseNotes->setHtml(StringUtils::htmlListPatch(releaseNotesHtml));
ui->releaseNotes->setOpenExternalLinks(true);

connect(ui->skipButton, &QPushButton::clicked, this, [this]() {
Expand Down
8 changes: 5 additions & 3 deletions launcher/ui/pages/instance/ManagedPackPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "InstanceTask.h"
#include "Json.h"
#include "Markdown.h"
#include "StringUtils.h"

#include "modplatform/modrinth/ModrinthPackManifest.h"

Expand Down Expand Up @@ -332,7 +333,7 @@ void ModrinthManagedPackPage::suggestVersion()
}
auto version = m_pack.versions.at(index);

ui->changelogTextBrowser->setHtml(markdownToHTML(version.changelog.toUtf8()));
ui->changelogTextBrowser->setHtml(StringUtils::htmlListPatch(markdownToHTML(version.changelog.toUtf8())));

ManagedPackPage::suggestVersion();
}
Expand Down Expand Up @@ -420,7 +421,7 @@ void FlameManagedPackPage::parseManagedPack()
"Don't worry though, it will ask you to update this instance instead, so you'll not lose this instance!"
"</h4>");

ui->changelogTextBrowser->setHtml(message);
ui->changelogTextBrowser->setHtml(StringUtils::htmlListPatch(message));
return;
}

Expand Down Expand Up @@ -502,7 +503,8 @@ void FlameManagedPackPage::suggestVersion()
}
auto version = m_pack.versions.at(index);

ui->changelogTextBrowser->setHtml(m_api.getModFileChangelog(m_inst->getManagedPackID().toInt(), version.fileId));
ui->changelogTextBrowser->setHtml(
StringUtils::htmlListPatch(m_api.getModFileChangelog(m_inst->getManagedPackID().toInt(), version.fileId)));

ManagedPackPage::suggestVersion();
}
Expand Down
5 changes: 3 additions & 2 deletions launcher/ui/pages/modplatform/ResourcePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include "Markdown.h"

#include "StringUtils.h"
#include "ui/dialogs/ResourceDownloadDialog.h"
#include "ui/pages/modplatform/ResourceModel.h"
#include "ui/widgets/ProjectItem.h"
Expand Down Expand Up @@ -234,8 +235,8 @@ void ResourcePage::updateUi()

text += "<hr>";

m_ui->packDescription->setHtml(
text + (current_pack->extraData.body.isEmpty() ? current_pack->description : markdownToHTML(current_pack->extraData.body)));
m_ui->packDescription->setHtml(StringUtils::htmlListPatch(
text + (current_pack->extraData.body.isEmpty() ? current_pack->description : markdownToHTML(current_pack->extraData.body))));
m_ui->packDescription->flush();
}

Expand Down
3 changes: 2 additions & 1 deletion launcher/ui/pages/modplatform/atlauncher/AtlPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "ui_AtlPage.h"

#include "BuildConfig.h"
#include "StringUtils.h"

#include "AtlUserInteractionSupportImpl.h"
#include "modplatform/atlauncher/ATLPackInstallTask.h"
Expand Down Expand Up @@ -144,7 +145,7 @@ void AtlPage::onSelectionChanged(QModelIndex first, [[maybe_unused]] QModelIndex

selected = filterModel->data(first, Qt::UserRole).value<ATLauncher::IndexedPack>();

ui->packDescription->setHtml(selected.description.replace("\n", "<br>"));
ui->packDescription->setHtml(StringUtils::htmlListPatch(selected.description.replace("\n", "<br>")));

for (const auto& version : selected.versions) {
ui->versionSelectionBox->addItem(version.version);
Expand Down
3 changes: 2 additions & 1 deletion launcher/ui/pages/modplatform/flame/FlamePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "FlameModel.h"
#include "InstanceImportTask.h"
#include "Json.h"
#include "StringUtils.h"
#include "modplatform/flame/FlameAPI.h"
#include "ui/dialogs/NewInstanceDialog.h"
#include "ui/widgets/ProjectItem.h"
Expand Down Expand Up @@ -292,6 +293,6 @@ void FlamePage::updateUi()
text += "<hr>";
text += api.getModDescription(current.addonId).toUtf8();

ui->packDescription->setHtml(text + current.description);
ui->packDescription->setHtml(StringUtils::htmlListPatch(text + current.description));
ui->packDescription->flush();
}
6 changes: 4 additions & 2 deletions launcher/ui/pages/modplatform/legacy_ftb/Page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

#include "Page.h"
#include "StringUtils.h"
#include "ui/widgets/ProjectItem.h"
#include "ui_Page.h"

Expand Down Expand Up @@ -260,8 +261,9 @@ void Page::onPackSelectionChanged(Modpack* pack)
{
ui->versionSelectionBox->clear();
if (pack) {
currentModpackInfo->setHtml("Pack by <b>" + pack->author + "</b>" + "<br>Minecraft " + pack->mcVersion + "<br>" + "<br>" +
pack->description + "<ul><li>" + pack->mods.replace(";", "</li><li>") + "</li></ul>");
currentModpackInfo->setHtml(StringUtils::htmlListPatch("Pack by <b>" + pack->author + "</b>" + "<br>Minecraft " + pack->mcVersion +
"<br>" + "<br>" + pack->description + "<ul><li>" +
pack->mods.replace(";", "</li><li>") + "</li></ul>"));
bool currentAdded = false;

for (int i = 0; i < pack->oldVersions.size(); i++) {
Expand Down
3 changes: 2 additions & 1 deletion launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "InstanceImportTask.h"
#include "Json.h"
#include "Markdown.h"
#include "StringUtils.h"

#include "ui/widgets/ProjectItem.h"

Expand Down Expand Up @@ -304,7 +305,7 @@ void ModrinthPage::updateUI()

text += markdownToHTML(current.extra.body.toUtf8());

ui->packDescription->setHtml(text + current.description);
ui->packDescription->setHtml(StringUtils::htmlListPatch(text + current.description));
ui->packDescription->flush();
}

Expand Down
3 changes: 2 additions & 1 deletion launcher/ui/pages/modplatform/technic/TechnicPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include "BuildConfig.h"
#include "Json.h"
#include "StringUtils.h"
#include "TechnicModel.h"
#include "modplatform/technic/SingleZipPackInstallTask.h"
#include "modplatform/technic/SolderPackInstallTask.h"
Expand Down Expand Up @@ -233,7 +234,7 @@ void TechnicPage::metadataLoaded()

text += "<br><br>";

ui->packDescription->setHtml(text + current.description);
ui->packDescription->setHtml(StringUtils::htmlListPatch(text + current.description));

// Strip trailing forward-slashes from Solder URL's
if (current.isSolder) {
Expand Down
3 changes: 2 additions & 1 deletion launcher/updater/prismupdater/UpdaterDialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <QTextBrowser>
#include "Markdown.h"
#include "StringUtils.h"

SelectReleaseDialog::SelectReleaseDialog(const Version& current_version, const QList<GitHubRelease>& releases, QWidget* parent)
: QDialog(parent), m_releases(releases), m_currentVersion(current_version), ui(new Ui::SelectReleaseDialog)
Expand Down Expand Up @@ -96,7 +97,7 @@ void SelectReleaseDialog::selectionChanged(QTreeWidgetItem* current, QTreeWidget
QString body = markdownToHTML(release.body.toUtf8());
m_selectedRelease = release;

ui->changelogTextBrowser->setHtml(body);
ui->changelogTextBrowser->setHtml(StringUtils::htmlListPatch(body));
}

SelectReleaseAssetDialog::SelectReleaseAssetDialog(const QList<GitHubReleaseAsset>& assets, QWidget* parent)
Expand Down
Loading