@@ -2,10 +2,9 @@ import { EditorStep } from "../mini-editor"
22import { isEditorNode , mapAnyCodeNode } from "./code"
33import { reduceStep } from "./code-files-reducer"
44import { CodeHikeConfig } from "./config"
5- import { SuperNode } from "./nodes"
5+ import { JsxNode , SuperNode } from "./nodes"
66
77// extract step info
8-
98export async function extractStepsInfo (
109 parent : SuperNode ,
1110 config : CodeHikeConfig ,
@@ -15,6 +14,7 @@ export async function extractStepsInfo(
1514) {
1615 const steps = [ ] as {
1716 editorStep ?: EditorStep
17+ previewStep ?: JsxNode
1818 children : SuperNode [ ]
1919 } [ ]
2020
@@ -29,6 +29,7 @@ export async function extractStepsInfo(
2929
3030 steps [ stepIndex ] = steps [ stepIndex ] || { children : [ ] }
3131 const step = steps [ stepIndex ]
32+
3233 if ( ! step . editorStep && isEditorNode ( child , config ) ) {
3334 const editorStep = await mapAnyCodeNode (
3435 { node : child , parent, index : i } ,
@@ -53,6 +54,13 @@ export async function extractStepsInfo(
5354 filter
5455 )
5556 }
57+ } else if (
58+ child . type === "mdxJsxFlowElement" &&
59+ child . name === "CH.Preview" &&
60+ // only add the preview if we have a preview in step 0
61+ ( stepIndex === 0 || steps [ 0 ] . previewStep != null )
62+ ) {
63+ step . previewStep = child
5664 } else {
5765 step . children . push ( child )
5866 }
@@ -66,7 +74,27 @@ export async function extractStepsInfo(
6674 }
6775 } )
6876
69- return steps . map ( step => step . editorStep )
77+ const hasPreviewSteps = steps [ 0 ] . previewStep !== undefined
78+ // if there is a CH.Preview in the first step
79+ // build the previewStep list
80+ if ( hasPreviewSteps ) {
81+ const previewSteps = steps . map ( step => step . previewStep )
82+ // fill empties with previous step
83+ previewSteps . forEach ( ( previewStep , i ) => {
84+ if ( ! previewStep ) {
85+ previewSteps [ i ] =
86+ merge === "merge steps with header"
87+ ? previewSteps [ 0 ]
88+ : previewSteps [ i - 1 ]
89+ }
90+ } )
91+ parent . children = parent . children . concat ( previewSteps )
92+ }
93+
94+ return {
95+ editorSteps : steps . map ( step => step . editorStep ) ,
96+ hasPreviewSteps,
97+ }
7098}
7199
72100/**
0 commit comments