Skip to content

Commit 66444f4

Browse files
authored
fix: check for \n in CodeMirror runmode (#23)
* fix: check for \n to prevent hitting else * test: check number of lines matches given code
1 parent 3daa603 commit 66444f4

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

__tests__/codeMirror.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,31 @@ describe('highlight mode', () => {
170170
expect(node.find('.cm-linerow.cm-overlay')).toHaveLength(6);
171171
});
172172
});
173+
174+
describe('runmode', () => {
175+
let node;
176+
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/");`;
177+
178+
beforeEach(() => {
179+
node = mount(
180+
syntaxHighlighter(code, 'c', {
181+
dark: true,
182+
highlightMode: true,
183+
tokenizeVariables: true,
184+
ranges: [
185+
[
186+
{ ch: 0, line: 0 },
187+
{ ch: 0, line: 1 },
188+
],
189+
],
190+
})
191+
);
192+
});
193+
194+
it('should display the correct number of lines with multiple linebreaks', () => {
195+
const checkLineBreaks = parseInt(node.find('.cm-linerow').last().find('.cm-lineNumber').text(), 10);
196+
const totalLines = code.split('\n');
197+
198+
expect(checkLineBreaks).toStrictEqual(totalLines.length);
199+
});
200+
});

src/codeMirror/index.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ const ReadmeCodeMirror = (code, lang, opts = { tokenizeVariables: false, highlig
172172
}
173173

174174
CodeMirror.runMode(code, mode, (text, style) => {
175-
if (style !== curStyle) {
175+
const lineBreakRegex = /\r?\n/;
176+
if (style !== curStyle || lineBreakRegex.test(text)) {
176177
flush();
177178
curStyle = style;
178179
accum = text;

0 commit comments

Comments
 (0)