返回 slidev
line-highlighting.md
根目录 / docs / features / line-highlighting.md
1 ---
2 depends:
3 - guide/syntax#code-block
4 - guide/animations
5 tags: [codeblock, animation]
6 description: |
7 Highlight specific lines in code blocks based on clicks.
8 ---
9
10 # Line Highlighting
11
12 To highlight specific lines, simply add line numbers within brackets `{}`. Line numbers start counting from 1 by default.
13
14 ````md
15 ```ts {2,3}
16 function add(
17 a: Ref<number> | number,
18 b: Ref<number> | number
19 ) {
20 return computed(() => unref(a) + unref(b))
21 }
22 ```
23 ````
24
25 ## Dynamic Line Highlighting
26
27 To change what's highlighted with multiple clicks, you can use `|` to separate each stage:
28
29 ````md
30 ```ts {2-3|5|all}
31 function add(
32 a: Ref<number> | number,
33 b: Ref<number> | number
34 ) {
35 return computed(() => unref(a) + unref(b))
36 }
37 ```
38 ````
39
40 This will first highlight `a: Ref<number> | number` and `b: Ref<number> | number`, and then `return computed(() => unref(a) + unref(b))` after one click, and lastly, the whole block.
41
42 You can set the line number to `hide` to hide the code block or `none` to not highlight any line:
43
44 ````md
45 ```ts {hide|none}
46 function add(
47 a: Ref<number> | number,
48 b: Ref<number> | number
49 ) {
50 return computed(() => unref(a) + unref(b))
51 }
52 ```
53 ````
54
55 ::: tip
56 Learn more in the [click animations guide](/guide/animations#positioning).
57 :::
58
58 lines MARKDOWN