Line highlighting now takes ranges

This commit is contained in:
Matt Aitken
2023-05-11 10:50:57 +01:00
parent 4fa3ea2491
commit 83b79610af
2 changed files with 10 additions and 3 deletions
@@ -30,7 +30,7 @@ type CodeBlockProps = {
showLineNumbers?: boolean;
/** Highlight line at given line number with color from theme.colors */
highlightLines?: number[];
highlightedRanges?: [number, number][];
/** Add/override classes on the overall element */
className?: string;
@@ -154,7 +154,7 @@ export const CodeBlock = forwardRef<HTMLDivElement, CodeBlockProps>(
{
showCopyButton = true,
showLineNumbers = true,
highlightLines,
highlightedRanges,
code,
className,
language = "typescript",
@@ -189,6 +189,10 @@ export const CodeBlock = forwardRef<HTMLDivElement, CodeBlockProps>(
}rem + 1.5rem )`;
}
const highlightLines = highlightedRanges?.flatMap(([start, end]) =>
Array.from({ length: end - start + 1 }, (_, i) => start + i)
);
return (
<div
className={cn(
@@ -32,7 +32,10 @@ export const Block: Story = {
console.info(log);
},
});`,
highlightLines: [2],
highlightedRanges: [
[6, 8],
[12, 14],
],
},
render: (args) => <CodeBlock {...args} />,