Skip to content

Commit b76a1e8

Browse files
authored
fix(mdx): push last highlighted line when making highlight blocks (#13156)
* fix(mdx): making sure last highlighted line is pushed when making highlight blocks * fix(mdx): improved code highlights parser * fix(style): removing codeblock bottom padding if last child is a highlight block
1 parent d08d170 commit b76a1e8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/components/codeBlock/code-blocks.module.scss

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
min-width: 100%;
5858
}
5959

60+
:global(.code-highlight > .highlight-block:last-child) {
61+
margin-bottom: -10px;
62+
}
63+
6064
:global(.code-line) {
6165
display: block;
6266
float: left;
@@ -74,7 +78,6 @@
7478
}
7579
}
7680

77-
7881
/* Diff highlighting, classes provided by rehype-prism-plus */
7982
/* Set inserted line (+) color */
8083
/* Move the margin left and adjust width so we can keep the parent padding */

src/components/codeHighlights/codeHighlights.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function makeHighlightBlocks(
1818
let highlightedLineElements: ReactElement[] = [];
1919
let highlightElementGroupCounter = 0;
2020

21-
return items.reduce((arr: ChildrenItem[], child) => {
21+
return items.reduce((arr: ChildrenItem[], child, index) => {
2222
if (typeof child !== 'object') {
2323
arr.push(child);
2424
return arr;
@@ -42,7 +42,17 @@ export function makeHighlightBlocks(
4242

4343
if (isHighlightedLine) {
4444
highlightedLineElements.push(element);
45+
46+
// If it's the last line that's highlighted, push it
47+
if (index === items.length - 1) {
48+
arr.push(
49+
<HighlightBlock key={highlightElementGroupCounter} language={language}>
50+
{...highlightedLineElements}
51+
</HighlightBlock>
52+
);
53+
}
4554
} else {
55+
// Check for an opened highlight group before pushing the new line
4656
if (highlightedLineElements.length > 0) {
4757
arr.push(
4858
<HighlightBlock key={highlightElementGroupCounter} language={language}>
@@ -52,6 +62,7 @@ export function makeHighlightBlocks(
5262
highlightedLineElements = [];
5363
++highlightElementGroupCounter;
5464
}
65+
5566
arr.push(child);
5667
}
5768

@@ -98,7 +109,7 @@ export function HighlightBlock({
98109
}
99110

100111
return (
101-
<HighlightBlockContainer>
112+
<HighlightBlockContainer className="highlight-block">
102113
<CodeLinesContainer ref={codeRef}>{children}</CodeLinesContainer>
103114
<ClipBoardContainer onClick={copyCodeOnClick}>
104115
{showCopyButton && !copied && (

0 commit comments

Comments
 (0)