-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
steps.tsx
164 lines (147 loc) · 4.06 KB
/
steps.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { EditorStep } from "../mini-editor"
import { isEditorNode, mapAnyCodeNode } from "./code"
import { reduceStep } from "./code-files-reducer"
import { CodeHikeConfig } from "./config"
import { JsxNode, SuperNode } from "./nodes"
import { getPresetConfig } from "./transform.preview"
// extract step info
export async function extractStepsInfo(
parent: SuperNode,
config: CodeHikeConfig,
merge:
| "merge steps with header"
| "merge step with previous"
) {
const steps = [] as {
editorStep?: EditorStep
previewStep?: JsxNode
children: SuperNode[]
}[]
const presetConfig = await getPresetConfig(
(parent as any).attributes
)
let stepIndex = 0
const children = parent.children || []
for (let i = 0; i < children.length; i++) {
const child = children[i]
if (child.type === "thematicBreak") {
stepIndex++
continue
}
steps[stepIndex] = steps[stepIndex] || { children: [] }
const step = steps[stepIndex]
if (!step.editorStep && isEditorNode(child, config)) {
const editorStep = await mapAnyCodeNode(
{ node: child, parent, index: i },
config
)
const filter = getFilterFromEditorNode(child)
if (stepIndex === 0) {
// for the header props, keep it as it is
step.editorStep = editorStep
} else {
// for the rest, merge the editor step with the header step or the prev step
const baseStep =
merge === "merge steps with header"
? steps[0].editorStep
: previousEditorStep(steps, stepIndex)
step.editorStep = reduceStep(
baseStep,
editorStep,
filter
)
}
step.children.push({
type: "mdxJsxFlowElement",
name: "CH.CodeSlot",
})
} else if (
child.type === "mdxJsxFlowElement" &&
child.name === "CH.Preview" &&
// only add the preview if we have a preview in step 0
(stepIndex === 0 ||
steps[0].previewStep != null ||
// or there is a global sandpack preset
presetConfig)
) {
step.previewStep = child
step.children.push({
type: "mdxJsxFlowElement",
name: "CH.PreviewSlot",
})
} else {
step.children.push(child)
}
}
parent.children = steps.map(step => {
return {
type: "mdxJsxFlowElement",
children: step.children,
data: { editorStep: step.editorStep },
}
})
const hasPreviewSteps =
steps[0].previewStep !== undefined || presetConfig
// if there is a CH.Preview in the first step or a preset config
// build the previewStep list
if (hasPreviewSteps) {
const previewSteps = steps.map(step => step.previewStep)
// fill empties with base step
previewSteps.forEach((previewStep, i) => {
if (!previewStep) {
if (presetConfig) {
// we fill the hole with a placeholder
previewSteps[i] = { type: "mdxJsxFlowElement" }
} else {
previewSteps[i] =
merge === "merge steps with header"
? previewSteps[0]
: previewSteps[i - 1]
}
}
})
parent.children = parent.children.concat(previewSteps)
}
// fill editor steps holes
const editorSteps = steps.map(step => step.editorStep)
editorSteps.forEach((editorStep, i) => {
if (!editorStep) {
editorSteps[i] =
merge === "merge steps with header"
? editorSteps[0]
: editorSteps[i - 1]
}
})
return {
editorSteps,
hasPreviewSteps,
presetConfig,
}
}
function previousEditorStep(
steps: {
editorStep?: EditorStep
}[],
index: number
) {
if (index === 0) {
throw new Error("The first step should have some code")
}
return (
steps[index - 1].editorStep ||
previousEditorStep(steps, index - 1)
)
}
/**
* Extracts the `show` prop from <CH.Code show={["foo.js", "bar.html"]} />
*/
function getFilterFromEditorNode(node: any) {
const value = node.attributes?.find(
a => a.name === "show"
)?.value?.value
if (value) {
return JSON.parse(value)
} else {
return undefined
}
}