Skip to content

fix(mdx): push last highlighted line when making highlight blocks #13156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/codeBlock/code-blocks.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
min-width: 100%;
}

:global(.code-highlight > .highlight-block:last-child) {
margin-bottom: -10px;
}

:global(.code-line) {
display: block;
float: left;
Expand All @@ -74,7 +78,6 @@
}
}


/* Diff highlighting, classes provided by rehype-prism-plus */
/* Set inserted line (+) color */
/* Move the margin left and adjust width so we can keep the parent padding */
Expand Down
15 changes: 13 additions & 2 deletions src/components/codeHighlights/codeHighlights.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function makeHighlightBlocks(
let highlightedLineElements: ReactElement[] = [];
let highlightElementGroupCounter = 0;

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

if (isHighlightedLine) {
highlightedLineElements.push(element);

// If it's the last line that's highlighted, push it
if (index === items.length - 1) {
arr.push(
<HighlightBlock key={highlightElementGroupCounter} language={language}>
{...highlightedLineElements}
</HighlightBlock>
);
}
} else {
// Check for an opened highlight group before pushing the new line
if (highlightedLineElements.length > 0) {
arr.push(
<HighlightBlock key={highlightElementGroupCounter} language={language}>
Expand All @@ -52,6 +62,7 @@ export function makeHighlightBlocks(
highlightedLineElements = [];
++highlightElementGroupCounter;
}

arr.push(child);
}

Expand Down Expand Up @@ -98,7 +109,7 @@ export function HighlightBlock({
}

return (
<HighlightBlockContainer>
<HighlightBlockContainer className="highlight-block">
<CodeLinesContainer ref={codeRef}>{children}</CodeLinesContainer>
<ClipBoardContainer onClick={copyCodeOnClick}>
{showCopyButton && !copied && (
Expand Down
Loading