From 270590ab8e7ad70d0c6fdb87d895fcc351286198 Mon Sep 17 00:00:00 2001 From: julshotal Date: Tue, 20 Oct 2020 19:43:40 -0400 Subject: [PATCH 1/2] fix: check for \n to prevent hitting else --- src/codeMirror/index.jsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/codeMirror/index.jsx b/src/codeMirror/index.jsx index b53d73d..cf67a65 100644 --- a/src/codeMirror/index.jsx +++ b/src/codeMirror/index.jsx @@ -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; From 1992213d37fc0a5ab23bb2a38b2c4cf5c68b0f27 Mon Sep 17 00:00:00 2001 From: julshotal Date: Fri, 23 Oct 2020 14:52:40 -0400 Subject: [PATCH 2/2] test: check number of lines matches given code --- __tests__/codeMirror.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/__tests__/codeMirror.test.js b/__tests__/codeMirror.test.js index 2760375..505bfbd 100644 --- a/__tests__/codeMirror.test.js +++ b/__tests__/codeMirror.test.js @@ -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); + }); +});