Skip to content

Commit 2112ee3

Browse files
committed
Split annotation focus string
1 parent 934a105 commit 2112ee3

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

packages/playground/content/meta-annotations.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Besides `focus`, there are a few more annotations that can be used to highlight
66

77
### `box` annotation
88

9-
```js box=2[15:43]
9+
```js box=2[15:43],4[10:12]
1010
function lorem(ipsum, dolor = 1) {
1111
const sit = ipsum == null ? 0 : ipsum.sit
1212
dolor = sit - amet(dolor)
@@ -18,7 +18,7 @@ function lorem(ipsum, dolor = 1) {
1818

1919
### `bg` annotation
2020

21-
```js bg=4:6
21+
```js bg=2,4:6
2222
function lorem(ipsum, dolor = 1) {
2323
const sit = ipsum == null ? 0 : ipsum.sit
2424
dolor = sit - amet(dolor)

packages/smooth-code/src/annotations.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
map,
1616
hasColumns,
1717
parsePartToObject,
18+
splitParts,
1819
ColumnExtremes,
1920
parseExtremes,
2021
} from "@code-hike/utils"
@@ -32,13 +33,27 @@ export function parseAnnotations(
3233
>
3334
multilineAnnotations: FullTween<MultiLineAnnotation[]>
3435
} {
35-
const inlineCodeAnnotations = mapWithDefault(
36+
// split annotations with multiple parts in the focus string
37+
// "1:2,3[4:5]" becomes two annotations "1:2" and "3[4:5]"
38+
const expandedAnnotations = mapWithDefault(
3639
annotations,
3740
[],
41+
annotations =>
42+
annotations!.flatMap(annotation =>
43+
splitParts(annotation.focus).map(part => ({
44+
...annotation,
45+
focus: part,
46+
}))
47+
)
48+
)
49+
50+
const inlineCodeAnnotations = mapWithDefault(
51+
expandedAnnotations,
52+
[],
3853
annotations => annotations!.filter(isInline)
3954
)
4055
const multilineCodeAnnotations = mapWithDefault(
41-
annotations,
56+
expandedAnnotations,
4257
[],
4358
annotations => annotations!.filter(a => !isInline(a))
4459
)

packages/storybook/src/code-spring.story.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ const x = (y) => y++
3636
`.trim()
3737

3838
const prevAnnotations = [
39-
{ focus: "1[10:11]" },
40-
{ focus: "2[1:5]" },
39+
{ focus: "1[10:11],2[1:5]" },
4140
{ focus: "3[8]" },
4241
]
4342

packages/utils/src/focus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function mapFocusToLineNumbers(
3434
}
3535
}
3636

37-
function splitParts(focus: string) {
37+
export function splitParts(focus: string) {
3838
return focus.split(/,(?![^\[]*\])/g)
3939
}
4040

0 commit comments

Comments
 (0)