Skip to content

Commit b245726

Browse files
authored
fix: largestLineNumber for unparsable texts (react-syntax-highlighter#463)
1 parent 183f7fe commit b245726

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/highlight.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,12 @@ export default function(defaultAstGenerator, defaultStyle) {
401401
}
402402

403403
// determine largest line number so that we can force minWidth on all linenumber elements
404-
const largestLineNumber = codeTree.value.length + startingLineNumber;
404+
let lineCount = codeTree.value.length;
405+
if (lineCount === 1 && codeTree.value[0].type === 'text') {
406+
// Since codeTree for an unparsable text (e.g. 'a\na\na') is [{ type: 'text', value: 'a\na\na' }]
407+
lineCount = codeTree.value[0].value.split('\n').length;
408+
}
409+
const largestLineNumber = lineCount + startingLineNumber;
405410

406411
const rows = processLines(
407412
codeTree,

0 commit comments

Comments
 (0)