Skip to content

Commit

Permalink
Merge pull request #206 from Waqar144/work/fix-setext-heading
Browse files Browse the repository at this point in the history
Fix setext headings
  • Loading branch information
pbek authored Jul 31, 2024
2 parents b631fab + 1828a31 commit 9af9c41
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions markdownhighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ void MarkdownHighlighter::initCodeLangs() {
{QLatin1String("yml"), MarkdownHighlighter::CodeYAML},
{QLatin1String("yaml"), MarkdownHighlighter::CodeYAML},
{QLatin1String("forth"), MarkdownHighlighter::CodeForth},
{QLatin1String("systemverilog"), MarkdownHighlighter::CodeSystemVerilog}};
{QLatin1String("systemverilog"),
MarkdownHighlighter::CodeSystemVerilog}};
}

/**
Expand Down Expand Up @@ -437,7 +438,7 @@ void MarkdownHighlighter::highlightMarkdown(const QString &text) {
* @param text
* @return 1, if 1 space, 2 if 2 spaces, 3 if 3 spaces. Otherwise 0
*/
int getIndentation(const QString &text) {
static int getIndentation(const QString &text) {
int spaces = 0;
// no more than 3 spaces
while (spaces < 4 && spaces < text.length() &&
Expand All @@ -446,6 +447,66 @@ int getIndentation(const QString &text) {
return spaces;
}

static bool isParagraph(const QString &text) {
// blank line
if (text.isEmpty()) return false;
int indent = getIndentation(text);
// code block
if (indent >= 4) return false;

const auto textView = MH_SUBSTR(indent, -1);
if (textView.isEmpty()) return false;

// unordered listtextView
if (textView.startsWith(QStringLiteral("- ")) ||
textView.startsWith(QStringLiteral("+ ")) ||
textView.startsWith(QStringLiteral("*")))
return false;
// block quote
if (textView.startsWith(QStringLiteral("> "))) return false;
// atx heading
if (textView.startsWith(QStringLiteral("#"))) {
int firstSpace = textView.indexOf(' ');
if (firstSpace > 0 && firstSpace <= 7) {
return false;
}
}
// hr
auto isThematicBreak = [textView]() {
return std::all_of(textView.begin(), textView.end(),
[](QChar c) {
auto ch = c.unicode();
return ch == '-' || ch == ' ' || ch == '\t';
}) ||
std::all_of(textView.begin(), textView.end(),
[](QChar c) {
auto ch = c.unicode();
return ch == '+' || ch == ' ' || ch == '\t';
}) ||
std::all_of(textView.begin(), textView.end(), [](QChar c) {
auto ch = c.unicode();
return ch == '*' || ch == ' ' || ch == '\t';
});
};
if (isThematicBreak()) return false;
// ordered list
if (textView.at(0).isDigit()) {
int i = 1;
for (; i < textView.size(); ++i) {
if (textView[i].isDigit())
continue;
else
break;
}
if (textView[i] == QLatin1Char('.') ||
textView[i] == QLatin1Char(')')) {
return false;
}
}

return true;
}

/**
* Highlight headlines
*
Expand Down Expand Up @@ -501,16 +562,19 @@ void MarkdownHighlighter::highlightHeadline(const QString &text) {
// take care of ==== and ---- headlines
const QString prev = currentBlock().previous().text();
auto prevSpaces = getIndentation(prev);
const bool isPrevParagraph = isParagraph(prev);

if (text.at(spacesOffset) == QLatin1Char('=') && prevSpaces < 4) {
if (text.at(spacesOffset) == QLatin1Char('=') && prevSpaces < 4 &&
isPrevParagraph) {
const bool pattern1 =
!prev.isEmpty() &&
hasOnlyHeadChars(text, QLatin1Char('='), spacesOffset);
if (pattern1) {
highlightSubHeadline(text, H1);
return;
}
} else if (text.at(spacesOffset) == QLatin1Char('-') && prevSpaces < 4) {
} else if (text.at(spacesOffset) == QLatin1Char('-') && prevSpaces < 4 &&
isPrevParagraph) {
const bool pattern2 =
!prev.isEmpty() &&
hasOnlyHeadChars(text, QLatin1Char('-'), spacesOffset);
Expand All @@ -523,18 +587,20 @@ void MarkdownHighlighter::highlightHeadline(const QString &text) {
const QString nextBlockText = currentBlock().next().text();
if (nextBlockText.isEmpty()) return;
const int nextSpaces = getIndentation(nextBlockText);
const bool isCurrentParagraph = isParagraph(text);

if (nextSpaces >= nextBlockText.length()) return;

if (nextBlockText.at(nextSpaces) == QLatin1Char('=') && nextSpaces < 4) {
if (nextBlockText.at(nextSpaces) == QLatin1Char('=') && nextSpaces < 4 &&
isCurrentParagraph) {
const bool nextHasEqualChars =
hasOnlyHeadChars(nextBlockText, QLatin1Char('='), nextSpaces);
if (nextHasEqualChars) {
setFormat(0, text.length(), _formats[HighlighterState::H1]);
setCurrentBlockState(HighlighterState::H1);
}
} else if (nextBlockText.at(nextSpaces) == QLatin1Char('-') &&
nextSpaces < 4) {
nextSpaces < 4 && isCurrentParagraph) {
const bool nextHasMinusChars =
hasOnlyHeadChars(nextBlockText, QLatin1Char('-'), nextSpaces);
if (nextHasMinusChars) {
Expand Down

0 comments on commit 9af9c41

Please sign in to comment.