Skip to content

Commit f37b4c3

Browse files
committed
Fix empty check
1 parent aa9cb76 commit f37b4c3

File tree

3 files changed

+142
-24
lines changed

3 files changed

+142
-24
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Spotlight
2+
3+
<CH.Spotlight>
4+
5+
```text
6+
123456789
7+
123456789
8+
123456789
9+
123456789
10+
123456789
11+
123456789
12+
123456789
13+
```
14+
15+
---
16+
17+
```text focus=1:5
18+
19+
```
20+
21+
focus=1:5
22+
23+
---
24+
25+
```text focus=1,2,3
26+
27+
```
28+
29+
focus=1,2,3
30+
31+
---
32+
33+
```text focus=2[1:6]
34+
35+
```
36+
37+
focus=2[1:6]
38+
39+
</CH.Spotlight>
40+
41+
# Scrolly
42+
43+
<CH.Scrollycoding>
44+
45+
```text
46+
123456789
47+
123456789
48+
123456789
49+
123456789
50+
123456789
51+
123456789
52+
123456789
53+
```
54+
55+
focus=1:5
56+
57+
---
58+
59+
```text focus=1,2,3
60+
61+
```
62+
63+
focus=1,2,3
64+
65+
---
66+
67+
```text focus=2[1:6]
68+
69+
```
70+
71+
focus=2[1:6]
72+
73+
</CH.Scrollycoding>
74+
75+
# Slides
76+
77+
<CH.Slideshow>
78+
79+
```text
80+
123456789
81+
123456789
82+
123456789
83+
123456789
84+
123456789
85+
123456789
86+
123456789
87+
```
88+
89+
focus=1:5
90+
91+
---
92+
93+
```text focus=1,2,3
94+
95+
```
96+
97+
focus=1,2,3
98+
99+
---
100+
101+
```text focus=2[1:6]
102+
103+
```
104+
105+
focus=2[1:6]
106+
107+
</CH.Slideshow>

packages/mdx/src/remark/code-files-reducer.tsx

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@ import { EditorStep, CodeFile } from "../mini-editor"
22

33
// extend steps with info from previous steps
44

5-
export function reduceSteps(
5+
/**
6+
* Extends `extraStep` with info from `baseStep`.
7+
*
8+
* @param baseStep it could be the header step or the previous step
9+
* @param step the step to be extended
10+
* @param filter if it is defined, show only the files in the array.
11+
*
12+
*/
13+
export function reduceStep(
614
baseStep: EditorStep,
7-
extraStep: EditorStep,
15+
step: EditorStep,
816
filter?: string[]
917
): EditorStep {
10-
let files = reduceFiles(baseStep.files, extraStep.files)
18+
let files = reduceFiles(baseStep.files, step.files)
1119

1220
const newNorthPanel = reducePanel(
1321
baseStep.northPanel,
14-
extraStep.northPanel,
15-
extraStep.southPanel
22+
step.northPanel,
23+
step.southPanel
1624
)!
1725

1826
const newSouthPanel = reducePanel(
1927
baseStep.southPanel,
20-
extraStep.southPanel,
21-
extraStep.northPanel
28+
step.southPanel,
29+
step.northPanel
2230
)
2331

2432
if (filter) {
@@ -35,7 +43,7 @@ export function reduceSteps(
3543

3644
return {
3745
...baseStep,
38-
...extraStep,
46+
...step,
3947
files: files,
4048
northPanel: newNorthPanel,
4149
southPanel: newSouthPanel,
@@ -68,33 +76,36 @@ function reducePanel(
6876
}
6977

7078
function reduceFiles(
71-
baseFiles: CodeFile[],
72-
extraFiles: CodeFile[]
79+
oldFiles: CodeFile[],
80+
newFiles: CodeFile[]
7381
): CodeFile[] {
7482
const filesMap = {} as { [name: string]: CodeFile }
75-
baseFiles.forEach(f => (filesMap[f.name] = f))
76-
extraFiles.forEach(ef => {
77-
const bf = filesMap[ef.name]
78-
if (!bf) {
79-
filesMap[ef.name] = ef
83+
oldFiles.forEach(f => (filesMap[f.name] = f))
84+
newFiles.forEach(newFile => {
85+
const prevFile = filesMap[newFile.name]
86+
if (!prevFile) {
87+
filesMap[newFile.name] = newFile
8088
return
8189
}
8290

83-
// merge old and new files
84-
const { code, ...rest } = ef
91+
// if the file is in both arrays, merge the content
92+
// but if the new file is empty, keep the old content
93+
const { code, ...rest } = newFile
8594
if (isEmpty(code)) {
86-
filesMap[ef.name] = { ...bf, ...rest }
95+
filesMap[newFile.name] = { ...prevFile, ...rest }
8796
} else {
88-
filesMap[ef.name] = ef
97+
filesMap[newFile.name] = newFile
8998
}
9099
})
91100

101+
// return a new array following the original order:
102+
// first the old files, then the new ones
92103
const result = [] as CodeFile[]
93-
baseFiles.forEach(f => {
104+
oldFiles.forEach(f => {
94105
result.push(filesMap[f.name])
95106
delete filesMap[f.name]
96107
})
97-
extraFiles.forEach(
108+
newFiles.forEach(
98109
f => filesMap[f.name] && result.push(filesMap[f.name])
99110
)
100111

@@ -103,7 +114,7 @@ function reduceFiles(
103114

104115
function isEmpty(code: CodeFile["code"]) {
105116
const anyContent = code.lines.some(l =>
106-
l.tokens.some(t => t.content.trim() == "")
117+
l.tokens.some(t => t.content.trim() !== "")
107118
)
108119
return !anyContent
109120
}

packages/mdx/src/remark/steps.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EditorStep } from "../mini-editor"
22
import { isEditorNode, mapAnyCodeNode } from "./code"
3-
import { reduceSteps } from "./code-files-reducer"
3+
import { reduceStep } from "./code-files-reducer"
44
import { CodeHikeConfig } from "./config"
55
import { SuperNode } from "./nodes"
66

@@ -47,7 +47,7 @@ export async function extractStepsInfo(
4747
? steps[0].editorStep
4848
: steps[stepIndex - 1].editorStep
4949

50-
step.editorStep = reduceSteps(
50+
step.editorStep = reduceStep(
5151
baseStep,
5252
editorStep,
5353
filter

0 commit comments

Comments
 (0)