@@ -36,17 +36,17 @@ import Yoga.FillInTheGaps.Logic (Segment, findResult, parseSegments, toCode)
3636import Yoga.Helpers ((?||))
3737import Yoga as Y
3838
39−type PreProps
40− = { children ∷
39+type PreProps =
40+ { children ∷
41+ Nullable
42+ { props ∷
4143 Nullable
42− { props ∷
43− Nullable
44− { mdxType ∷ Nullable String
45− , children ∷ Nullable String
46− , className ∷ Nullable String
47− }
44+ { mdxType ∷ Nullable String
45+ , children ∷ Nullable String
46+ , className ∷ Nullable String
4847 }
49− }
48+ }
49+ }
5050
5151isQuiz ∷ MDX -> Boolean
5252isQuiz (MDX a) =
@@ -55,9 +55,9 @@ isQuiz (MDX a) =
5555 && ((unsafeCoerce a).props.children.props.mdxType == "code")
5656 && ((unsafeCoerce a).props.children.props.children # parseSegments # isJust)
5757
58−type MdxProviderProps
59− = { children ∷ ReactChildren MDX
60− }
58+type MdxProviderProps =
59+ { children ∷ ReactChildren MDX
60+ }
6161
6262mkLiveMdxProviderComponent ∷ FetchImpl -> Effect (ReactComponent MdxProviderProps)
6363mkLiveMdxProviderComponent fetchImpl = mkMdxProviderComponent (apiCompiler fetchImpl)
@@ -75,38 +75,33 @@ mkMdxProviderComponent compiler = do
7575 visibleUntil /\ updateVisible <- useState 1
7676 let
7777 onFailure title kids = log "Oopsie daisy"
78−
7978 onSuccess = updateVisible (_ + one)
80−
8179 mapVisible ∷ Int -> MDX -> _
8280 mapVisible i kid =
8381 { accum: i + if isQuiz kid then one else zero
8482 , value: if (i < visibleUntil) then Just kid else Nothing
8583 }
86−
8784 visibleKids ∷ Array MDX
8885 visibleKids =
8986 reactChildrenToArray children
9087 # mapAccumL mapVisible zero
9188 # _.value
9289 # Array.catMaybes
93−
9490 contentMDX ∷ ReactChildren MDX
9591 contentMDX = reactChildrenFromArray visibleKids
96−
9792 paragraph parChildren =
9893 E.element M.p
9994 ( { layout: M.layout true
10095 , css:
101− E.css
102− { fontFamily: E.str "Cormorant Garamond"
103− , fontSize: E.str "min(calc(var(--s-1) + 2.4vw), var(--s1))"
104− , lineHeight: E.var "--line-height-small"
105− , "-ms-hyphens": E.str "auto"
106− , "-webkit-hyphens": E.str "auto"
107− , hyphens: E.str "auto"
108− , overflow: E.str "hidden"
109− }
96+ E.css
97+ { fontFamily: E.str "Cormorant Garamond"
98+ , fontSize: E.str "min(calc(var(--s-1) + 2.4vw), var(--s1))"
99+ , lineHeight: E.var "--line-height-small"
100+ , "-msHyphens": E.str "auto"
101+ , "-webkitHyphens": E.str "auto"
102+ , hyphens: E.str "auto"
103+ , overflow: E.str "hidden"
104+ }
110105 , className: "blog-p"
111106 , children: parChildren
112107 }
@@ -114,65 +109,56 @@ mkMdxProviderComponent compiler = do
114109 let
115110 mdxComponents =
116111 { "Sc":
117− \(props :: { children :: String }) ->
118− smallCaps props.children
112+ \(props ∷ { children ∷ String }) ->
113+ smallCaps props.children
119114 , h1:
120− \(props ∷ { | M.MotionProps Props_h1 }) ->
121− React.element M.h1 (props { layout = M.layout true })
115+ \(props ∷ { | M.MotionProps Props_h1 }) ->
116+ React.element M.h1 (props { layout = M.layout true })
122117 , h2:
123− \(props ∷ { | M.MotionProps Props_h2 }) ->
124− React.element M.h2 (props { layout = M.layout true })
118+ \(props ∷ { | M.MotionProps Props_h2 }) ->
119+ React.element M.h2 (props { layout = M.layout true })
125120 , h3:
126− \(props ∷ { | M.MotionProps Props_h3 }) ->
127− React.element M.h3 (props { layout = M.layout true })
121+ \(props ∷ { | M.MotionProps Props_h3 }) ->
122+ React.element M.h3 (props { layout = M.layout true })
128123 , h4:
129− \(props ∷ { | M.MotionProps Props_h4 }) ->
130− React.element M.h4 (props { layout = M.layout true })
124+ \(props ∷ { | M.MotionProps Props_h4 }) ->
125+ React.element M.h4 (props { layout = M.layout true })
131126 , hr: (const $ R.hr {})
132127 , p:
133− \(props ∷ { children :: Array JSX }) ->
134− paragraph props.children
128+ \(props ∷ { children ∷ Array JSX }) ->
129+ paragraph props.children
135130 , inlineCode:
136− \props -> do
137− R.span { children: props.children }
131+ \props -> do
132+ R.span { children: props.children }
138133 , pre:
139− mkFn2 \(props ∷ PreProps) other -> do
140− let
141− childrenQ = Nullable.toMaybe props.children
142−
143− propsQ = (_.props >>> Nullable.toMaybe) =<< childrenQ
144−
145− mdxTypeQ = (_.mdxType >>> Nullable.toMaybe) =<< propsQ
146−
147− childrenQ2 = (_.children >>> Nullable.toMaybe) =<< propsQ
148−
149− classNameQ = (_.className >>> Nullable.toMaybe) =<< propsQ
150−
151− isCode = fromMaybe false (mdxTypeQ <#> eq "code")
152−
153− codeQ = childrenQ2
154−
155− height =
156− (fromMaybe 200 >>> show >>> (_ <> "px")) do
157− code <- codeQ
158− pure
159− ( String.split (String.Pattern "\n") code
160− # Array.length
161− # \x -> (x * 12) + 100
162− )
163−
164− language = fromMaybe "" (classNameQ >>= String.stripPrefix (String.Pattern "language-"))
165−
166− segmentsQ = parseSegments (codeQ ?|| "")
167− case isCode, language, segmentsQ of
168− true, "purescript", Just initialSegments -> element quiz { initialSegments, onFailure, onSuccess }
169− true, _, _ ->
170− element editor
171− { initialCode: fromMaybe "" codeQ
172− , height
173− , language
174− }
175− false, _, _ -> element (unsafePerformEffect $ unsafeCreateDOMComponent "pre") props
134+ mkFn2 \(props ∷ PreProps) other -> do
135+ let
136+ childrenQ = Nullable.toMaybe props.children
137+ propsQ = (_.props >>> Nullable.toMaybe) =<< childrenQ
138+ mdxTypeQ = (_.mdxType >>> Nullable.toMaybe) =<< propsQ
139+ childrenQ2 = (_.children >>> Nullable.toMaybe) =<< propsQ
140+ classNameQ = (_.className >>> Nullable.toMaybe) =<< propsQ
141+ isCode = fromMaybe false (mdxTypeQ <#> eq "code")
142+ codeQ = childrenQ2
143+ height =
144+ (fromMaybe 200 >>> show >>> (_ <> "px")) do
145+ code <- codeQ
146+ pure
147+ ( String.split (String.Pattern "\n") code
148+ # Array.length
149+ # \x -> (x * 12) + 100
150+ )
151+ language = fromMaybe "" (classNameQ >>= String.stripPrefix (String.Pattern "language-"))
152+ segmentsQ = parseSegments (codeQ ?|| "")
153+ case isCode, language, segmentsQ of
154+ true, "purescript", Just initialSegments -> element quiz { initialSegments, onFailure, onSuccess }
155+ true, _, _ ->
156+ element editor
157+ { initialCode: fromMaybe "" codeQ
158+ , height
159+ , language
160+ }
161+ false, _, _ -> element (unsafePerformEffect $ unsafeCreateDOMComponent "pre") props
176162 }
177163 pure
178164 $ element mdxProvider
@@ -180,11 +166,11 @@ mkMdxProviderComponent compiler = do
180166 , components: mdxComponents
181167 }
182168
183−type QuizProps
184− = { initialSegments ∷ Array (Array Segment)
185− , onFailure ∷ String -> Array JSX -> Effect Unit
186− , onSuccess ∷ Effect Unit
187− }
169+type QuizProps =
170+ { initialSegments ∷ Array (Array Segment)
171+ , onFailure ∷ String -> Array JSX -> Effect Unit
172+ , onSuccess ∷ Effect Unit
173+ }
188174
189175mkQuiz ∷ ∀ r. { | Compiler r } -> Effect (ReactComponent QuizProps)
190176mkQuiz compiler = do
@@ -196,24 +182,22 @@ mkQuiz compiler = do
196182 $ element fillInTheGaps
197183 { segments
198184 , run:
199− launchAff_ do
200− let
201− code = toCode segments
202− result <- compiler.compileAndRun { code }
203− liftEffect case result of
204− Right r
205− | String.stripSuffix (String.Pattern "\n") r.stdout == (findResult $ join segments) -> do
206− updateSolvedWith (const $ String.stripSuffix (String.Pattern "\n") r.stdout)
207− onSuccess
208− Right r
209− | r.stdout /= "\n" -> onFailure "Oh shit!" [ R.text r.stdout ]
210− Right r -> onFailure "Oh shit!" [ R.text r.stderr ]
211− Left (cr ∷ CompileResult) -> onFailure "Oh shit!" [ R.text (intercalate ", " (cr.result <#> _.message)) ]
185+ launchAff_ do
186+ let code = toCode segments
187+ result <- compiler.compileAndRun { code }
188+ liftEffect case result of
189+ Right r
190+ | String.stripSuffix (String.Pattern "\n") r.stdout == (findResult $ join segments) -> do
191+ updateSolvedWith (const $ String.stripSuffix (String.Pattern "\n") r.stdout)
192+ onSuccess
193+ Right r
194+ | r.stdout /= "\n" -> onFailure "Oh shit!" [ R.text r.stdout ]
195+ Right r -> onFailure "Oh shit!" [ R.text r.stderr ]
196+ Left (cr ∷ CompileResult) -> onFailure "Oh shit!" [ R.text (intercalate ", " (cr.result <#> _.message)) ]
212197 , updateSegments:
213− \update -> do
214− let
215− updated = update segments
216− unless (segments == updated) do updateSegments (const updated)
198+ \update -> do
199+ let updated = update segments
200+ unless (segments == updated) do updateSegments (const updated)
217201 , solvedWith
218202 }
219203