Skip to content

Commit 3176a2c

Browse files
fix(theme): Fix code block magic comments with CRLF line breaks bug (#11046)
* fix: with CRLF line breaks, an extra empty line was rendered with // highlight-end at end of code blocks See: #11036 * Add unit tests --------- Co-authored-by: sebastien <[email protected]>
1 parent c0f3755 commit 3176a2c

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

packages/docusaurus-theme-common/src/utils/__tests__/__snapshots__/codeBlockUtils.test.ts.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ bbbbb",
7070
}
7171
`;
7272

73+
exports[`parseLines handles CRLF line breaks with highlight comments correctly 1`] = `
74+
{
75+
"code": "aaaaa
76+
bbbbb",
77+
"lineClassNames": {
78+
"1": [
79+
"theme-code-block-highlighted-line",
80+
],
81+
},
82+
}
83+
`;
84+
85+
exports[`parseLines handles CRLF line breaks with highlight metastring 1`] = `
86+
{
87+
"code": "aaaaa
88+
bbbbb",
89+
"lineClassNames": {
90+
"1": [
91+
"theme-code-block-highlighted-line",
92+
],
93+
},
94+
}
95+
`;
96+
7397
exports[`parseLines handles one line with multiple class names 1`] = `
7498
{
7599
"code": "

packages/docusaurus-theme-common/src/utils/__tests__/codeBlockUtils.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,29 @@ line
360360
),
361361
).toMatchSnapshot();
362362
});
363+
364+
it('handles CRLF line breaks with highlight comments correctly', () => {
365+
expect(
366+
parseLines(
367+
`aaaaa\r\n// highlight-start\r\nbbbbb\r\n// highlight-end\r\n`,
368+
{
369+
metastring: '',
370+
language: 'js',
371+
magicComments: defaultMagicComments,
372+
},
373+
),
374+
).toMatchSnapshot();
375+
});
376+
377+
it('handles CRLF line breaks with highlight metastring', () => {
378+
expect(
379+
parseLines(`aaaaa\r\nbbbbb\r\n`, {
380+
metastring: '{2}',
381+
language: 'js',
382+
magicComments: defaultMagicComments,
383+
}),
384+
).toMatchSnapshot();
385+
});
363386
});
364387

365388
describe('getLineNumbersStart', () => {

packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export function parseLines(
241241
*/
242242
code: string;
243243
} {
244-
let code = content.replace(/\n$/, '');
244+
let code = content.replace(/\r?\n$/, '');
245245
const {language, magicComments, metastring} = options;
246246
// Highlighted lines specified in props: don't parse the content
247247
if (metastring && metastringLinesRangeRegex.test(metastring)) {
@@ -266,7 +266,7 @@ export function parseLines(
266266
magicComments,
267267
);
268268
// Go through line by line
269-
const lines = code.split('\n');
269+
const lines = code.split(/\r?\n/);
270270
const blocks = Object.fromEntries(
271271
magicComments.map((d) => [d.className, {start: 0, range: ''}]),
272272
);

0 commit comments

Comments
 (0)