Skip to content

Commit

Permalink
Allow for multiple highlighted areas in code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorblades committed Jun 3, 2022
1 parent 8992b20 commit ca1491f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-actors-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/chakra-helpers": patch
---

Allow for multiple highlight areas in code blocks
19 changes: 13 additions & 6 deletions packages/chakra-helpers/src/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,22 @@ export const CodeBlock = ({
// create an array of lines highlighted by "highlight-start" and
// "highlight-end" comments
const highlightRange = [];
let isHighlighting = false;
let highlightOffset = 0;
for (let i = 0; i < tokens.length; i++) {
const line = tokens[i];
if (isHighlightEnd(line)) {
highlightRange.pop();
break;
}

if (highlightRange.length || isHighlightStart(line)) {
highlightRange.push(i + 1);
// turn highlighting off
isHighlighting = false;
// account for the soon-to-be-missing start and end comments
highlightOffset += 2;
} else if (isHighlightStart(line)) {
// start highlighting
isHighlighting = true;
} else if (isHighlighting) {
// while highlighting, push the current index minus the offset into
// the array of highlighted lines
highlightRange.push(i - highlightOffset);
}
}

Expand Down

0 comments on commit ca1491f

Please sign in to comment.