Skip to content

Commit

Permalink
fix: check for \n in CodeMirror runmode (#23)
Browse files Browse the repository at this point in the history
* fix: check for \n to prevent hitting else

* test: check number of lines matches given code
  • Loading branch information
julshotal authored Oct 23, 2020
1 parent 3daa603 commit 66444f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions __tests__/codeMirror.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,31 @@ describe('highlight mode', () => {
expect(node.find('.cm-linerow.cm-overlay')).toHaveLength(6);
});
});

describe('runmode', () => {
let node;
const code = `CURL *hnd = curl_easy_init();\n\nurl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "GET");\n\ncurl_easy_setopt(hnd, CURLOPT_URL, "http://httpbin.orgpet/");`;

beforeEach(() => {
node = mount(
syntaxHighlighter(code, 'c', {
dark: true,
highlightMode: true,
tokenizeVariables: true,
ranges: [
[
{ ch: 0, line: 0 },
{ ch: 0, line: 1 },
],
],
})
);
});

it('should display the correct number of lines with multiple linebreaks', () => {
const checkLineBreaks = parseInt(node.find('.cm-linerow').last().find('.cm-lineNumber').text(), 10);
const totalLines = code.split('\n');

expect(checkLineBreaks).toStrictEqual(totalLines.length);
});
});
3 changes: 2 additions & 1 deletion src/codeMirror/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ const ReadmeCodeMirror = (code, lang, opts = { tokenizeVariables: false, highlig
}

CodeMirror.runMode(code, mode, (text, style) => {
if (style !== curStyle) {
const lineBreakRegex = /\r?\n/;
if (style !== curStyle || lineBreakRegex.test(text)) {
flush();
curStyle = style;
accum = text;
Expand Down

0 comments on commit 66444f4

Please sign in to comment.