Skip to content

Commit

Permalink
#201 add: improve speed of link highlighting in lists starting with 4…
Browse files Browse the repository at this point in the history
…+ spaces
  • Loading branch information
pbek committed Dec 10, 2023
1 parent 725564f commit 405b423
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions markdownhighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,18 +2003,47 @@ int MarkdownHighlighter::highlightLinkOrImage(const QString &text,
// If the first 4 characters are spaces (for 4-spaces fence code),
// but not list markers, return
if (text.left(4).trimmed().isEmpty()) {
// Check if text starts with a "- ", "+ ", "* ", "\d+. ", "\d+) "
QStringList patterns = {"- ", "+ ", "* ", "\\d+. ", "\\d+) "};

// Construct the regular expression pattern
QString patternString = "^(" + patterns.join("|") + ")";
QRegularExpression pattern(patternString);

// Check if the text starts with any of the specified patterns
QRegularExpressionMatch match = pattern.match(text.trimmed());

if (match.hasMatch()) {
return startIndex;
// Check for unordered list markers
auto leftChars = text.trimmed().left(2);

if (leftChars != QLatin1String("- ") &&
leftChars != QLatin1String("+ ") &&
leftChars != QLatin1String("* ")) {
// Check for a few ordered list markers
leftChars = text.trimmed().left(3);

if (leftChars != QLatin1String("1) ") &&
leftChars != QLatin1String("2) ") &&
leftChars != QLatin1String("3) ") &&
leftChars != QLatin1String("4) ") &&
leftChars != QLatin1String("5) ") &&
leftChars != QLatin1String("6) ") &&
leftChars != QLatin1String("7) ") &&
leftChars != QLatin1String("8) ") &&
leftChars != QLatin1String("9) ") &&
leftChars != QLatin1String("1. ") &&
leftChars != QLatin1String("2. ") &&
leftChars != QLatin1String("3. ") &&
leftChars != QLatin1String("4. ") &&
leftChars != QLatin1String("5. ") &&
leftChars != QLatin1String("6. ") &&
leftChars != QLatin1String("7. ") &&
leftChars != QLatin1String("8. ") &&
leftChars != QLatin1String("9. ")) {
// Check if text starts with a "\d+. ", "\d+) "
const static QStringList patterns = {"\\d+\\. ", "\\d+\\) "};

// Construct the regular expression pattern
const static QString patternString = "^(" + patterns.join("|") + ")";
const static QRegularExpression pattern(patternString);

// Check if the text starts with any of the specified patterns
QRegularExpressionMatch match = pattern.match(text.trimmed());

if (!match.hasMatch()) {
return startIndex;
}
}
}
}

Expand Down

0 comments on commit 405b423

Please sign in to comment.