Skip to content

Commit

Permalink
Merge pull request #1551 from Waqar144/code-fix
Browse files Browse the repository at this point in the history
Preview highlighter improvements
  • Loading branch information
pbek committed Jan 6, 2020
2 parents 1eb683f + 8c76d51 commit 0fb2d5f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/entities/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2034,11 +2034,12 @@ QString Note::textToMarkdownHtml(QString str, const QString& notesPath,
int endline = str.indexOf(QChar('\n'), currentCbPos);
QString lang = str.mid(currentCbPos +3, endline - (currentCbPos + 3));
//in case someone decides to put ``` code ``` on the same line
//we skip it because it is inline code and not codeBlock
if (lang.contains(QLatin1String("```"))) {
//reset the endline to ``, the third backtick will be taken care of below
endline = currentCbPos + 2;
//empty the lang
lang = QLatin1String("");
int nextEnd = str.indexOf(QLatin1String("```"), currentCbPos + 3);
nextEnd += 3;
currentCbPos = str.indexOf(QLatin1String("```"), nextEnd);
continue;
}
//move start pos to after the endline

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/codetohtmlconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ int CodeToHtmlConverter::highlightComment(QString &output, int i, bool isSingleL
} else {
endPos = _input.indexOf(QLatin1String("*/"), i);
if (endPos == -1) {
//-1, otherwise we ruin the codeblock end and backticks will be visible in rendered html
endPos = _input.length() - 1;
//look for the last endline
endPos = _input.lastIndexOf(QLatin1Char('\n'));
} else {
endPos += 2;
}
Expand Down
17 changes: 15 additions & 2 deletions tests/unit_tests/testcases/app/test_notes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,24 @@ void TestNotes::testCodeToHtmlConversionSingleLineComment()
void TestNotes::testCodeToHtmlConversionMultiLineComment()
{
QString comment = QStringLiteral("/*hello my qownnotes blah blah single line*/\n");
QString commentTrueMultiLine = QStringLiteral("/*hello my \nqownnotes blah \nblah single line*/\n");
QString commentTrueMultiLineNoEnd = QStringLiteral("/*hello my \nqownnotes blah \nblah single line\n");

CodeToHtmlConverter c1(comment.midRef(0, comment.length()), QStringLiteral("cpp"));
CodeToHtmlConverter c2(commentTrueMultiLine.midRef(0, commentTrueMultiLine.length()), QStringLiteral("cpp"));
CodeToHtmlConverter c3(commentTrueMultiLineNoEnd.midRef(0, commentTrueMultiLineNoEnd.length()), QStringLiteral("cpp"));

QString outputMultiLineComment = c1.process();
QString expectedOutputMultiLineComment = QStringLiteral("<span class=\"code-comment\">&#47;*hello my qownnotes blah blah single line*&#47;</span>\n");
QString outputTrueMultiLine = c2.process();
QString outputTrueMultiLineNoEnd = c3.process();

QString expectedMultiLineComment = QStringLiteral("<span class=\"code-comment\">&#47;*hello my qownnotes blah blah single line*&#47;</span>\n");
QString expectedTrueMultiLine = QStringLiteral("<span class=\"code-comment\">&#47;*hello my \nqownnotes blah \nblah single line*&#47;</span>\n");
QString expectedTrueMultiLineNoEnd = QStringLiteral("<span class=\"code-comment\">&#47;*hello my \nqownnotes blah \nblah single line</span>\n");

QVERIFY(outputMultiLineComment == expectedOutputMultiLineComment);
QVERIFY(outputMultiLineComment == expectedMultiLineComment);
QVERIFY(outputTrueMultiLine == expectedTrueMultiLine);
QVERIFY(outputTrueMultiLineNoEnd == expectedTrueMultiLineNoEnd);
}

void TestNotes::testCodeToHtmlNumericLiterals()
Expand Down

0 comments on commit 0fb2d5f

Please sign in to comment.