Skip to content

Commit 7d0aaff

Browse files
authored
highlight is now mark, accepts a nested array for ranges (#127)
1 parent cd8ba25 commit 7d0aaff

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/docs/components/CodeBlock/CodeBlock.svelte

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@
66
interface Props {
77
code: string;
88
lang: BuiltinLanguage | SpecialLanguage;
9-
highlight?: number | Array<number>;
9+
mark?: Array<number | [number, number]>;
1010
}
1111
1212
// Props
13-
let { code, lang = 'text', highlight = [] }: Props = $props();
13+
let { code, lang = 'text', mark = [] }: Props = $props();
14+
15+
const highlightedLineNumbers = $derived(
16+
mark
17+
.map((mark) => {
18+
if (Array.isArray(mark)) {
19+
const [start, end] = mark;
20+
return Array.from({ length: end - start + 1 }, (_, i) => start + i);
21+
}
22+
return mark;
23+
})
24+
.flat(),
25+
);
1426
1527
// Process Language
1628
const renderedCode = $derived(
@@ -29,8 +41,8 @@
2941
* This transformer adds the `highlighted` class to lines that are to be highlighted.
3042
*/
3143
{
32-
line(node, line) {
33-
if (!(Array.isArray(highlight) ? highlight : [highlight]).includes(line)) {
44+
line(node, lineNumber) {
45+
if (!highlightedLineNumbers.includes(lineNumber)) {
3446
return;
3547
}
3648
this.addClassToHast(node, 'highlighted');

0 commit comments

Comments
 (0)