1module MDXProvider where
4import Data.Array (intercalate)
5import Data.Array as Array
6import Data.Either (Either(..))
7import Data.Function.Uncurried (mkFn2)
8import Data.Maybe (Maybe(..), fromMaybe, isJust)
9import Data.Nullable (Nullable)
10import Data.Nullable as Nullable
11import Data.String as String
12import Data.Traversable (mapAccumL)
13import Data.Tuple.Nested ((/\))
15import Effect.Aff (launchAff_)
16import Effect.Class (liftEffect)
17import Effect.Class.Console (log)
18import Effect.Unsafe (unsafePerformEffect)
19import Framer.Motion as M
21import Milkis.Impl (FetchImpl)
22import React.Basic (JSX, ReactComponent)
23import React.Basic.DOM (Props_h1, Props_h2, Props_h3, Props_h4, Props_p, Props_a, unsafeCreateDOMComponent)
24import React.Basic.DOM as R
25import React.Basic.Emotion as E
26import React.Basic.Hooks (ReactChildren, element, memo, reactChildrenFromArray, reactChildrenToArray, reactComponent, reactComponentWithChildren, useState)
27import React.Basic.Hooks as React
28import Shared.Models.Body (CompileResult)
29import Typography.SmallCaps (mkSmallCaps)
30import Unsafe.Coerce (unsafeCoerce)
31import Yoga.CompileEditor.Component (mkCompileEditor)
32import Yoga.Compiler.Api (apiCompiler)
33import Yoga.Compiler.Types (Compiler)
34import Yoga.FillInTheGaps.Component as FillInTheGaps
35import Yoga.FillInTheGaps.Logic (Segment, findResult, parseSegments, toCode)
36import Yoga.Helpers ((?||))
44 { mdxType ∷ Nullable String
45 , children ∷ Nullable String
46 , className ∷ Nullable String
51isQuiz ∷ MDX -> Boolean
53 a.props.mdxType == "pre"
54 && ((unsafeCoerce a).props.children.props.className == "language-purescript")
55 && ((unsafeCoerce a).props.children.props.mdxType == "code")
56 && ((unsafeCoerce a).props.children.props.children # parseSegments # isJust)
58type MdxProviderProps =
59 { children ∷ ReactChildren MDX
62mkLiveMdxProviderComponent ∷ FetchImpl -> Effect (ReactComponent MdxProviderProps)
63mkLiveMdxProviderComponent fetchImpl = mkMdxProviderComponent (apiCompiler fetchImpl)
65mkMdxProviderComponent ∷
68 Effect (ReactComponent MdxProviderProps)
69mkMdxProviderComponent compiler = do
70 editor <- memo $ mkCompileEditor compiler
71 quiz <- memo $ mkQuiz compiler
72 smallCaps <- mkSmallCaps
73 reactComponentWithChildren "MDXProviderComponent" do
74 \{ children } -> React.do
75 visibleUntil /\ updateVisible <- useState 1
77 onFailure title kids = log "Oopsie daisy"
78 onSuccess = updateVisible (_ + one)
79 mapVisible ∷ Int -> MDX -> _
81 { accum: i + if isQuiz kid then one else zero
82 , value: if (i < visibleUntil) then Just kid else Nothing
84 visibleKids ∷ Array MDX
86 reactChildrenToArray children
87 # mapAccumL mapVisible zero
90 contentMDX ∷ ReactChildren MDX
91 contentMDX = reactChildrenFromArray visibleKids
92 paragraph parChildren =
94 ( { layout: M.layout true
97 { fontFamily: E.str "'Cormorant Garamond', Georgia, serif"
98 , fontSize: E.str "min(calc(var(--s-1) + 2.4vw), var(--s1))"
99 , fontWeight: E.str "400"
100 , lineHeight: E.var "--line-height-small"
101 , "-msHyphens": E.str "auto"
102 , "-webkitHyphens": E.str "auto"
103 , hyphens: E.str "auto"
104 , overflow: E.str "hidden"
106 , className: "blog-p"
107 , children: parChildren
113 \(props ∷ { children ∷ String }) ->
114 smallCaps props.children
116 \(props ∷ { | M.MotionProps Props_h1 }) ->
117 React.element M.h1 (props { layout = M.layout true })
119 \(props ∷ { | M.MotionProps Props_h2 }) ->
120 React.element M.h2 (props { layout = M.layout true })
122 \(props ∷ { | M.MotionProps Props_h3 }) ->
123 React.element M.h3 (props { layout = M.layout true })
125 \(props ∷ { | M.MotionProps Props_h4 }) ->
126 React.element M.h4 (props { layout = M.layout true })
127 , hr: (const $ R.hr {})
129 \(props ∷ { children ∷ Array JSX }) ->
130 paragraph props.children
133 R.span { children: props.children }
135 mkFn2 \(props ∷ PreProps) other -> do
137 childrenQ = Nullable.toMaybe props.children
138 propsQ = (_.props >>> Nullable.toMaybe) =<< childrenQ
139 mdxTypeQ = (_.mdxType >>> Nullable.toMaybe) =<< propsQ
140 childrenQ2 = (_.children >>> Nullable.toMaybe) =<< propsQ
141 classNameQ = (_.className >>> Nullable.toMaybe) =<< propsQ
142 isCode = fromMaybe false (mdxTypeQ <#> eq "code")
145 (fromMaybe 200 >>> show >>> (_ <> "px")) do
148 ( String.split (String.Pattern "\n") code
150 # \x -> (x * 12) + 100
152 language = fromMaybe "" (classNameQ >>= String.stripPrefix (String.Pattern "language-"))
153 segmentsQ = parseSegments (codeQ ?|| "")
154 case isCode, language, segmentsQ of
155 true, "purescript", Just initialSegments -> element quiz { initialSegments, onFailure, onSuccess }
158 { initialCode: fromMaybe "" codeQ
162 false, _, _ -> element (unsafePerformEffect $ unsafeCreateDOMComponent "pre") props
165 $ element mdxProvider
166 { children: contentMDX
167 , components: mdxComponents
171 { initialSegments ∷ Array (Array Segment)
172 , onFailure ∷ String -> Array JSX -> Effect Unit
173 , onSuccess ∷ Effect Unit
176mkQuiz ∷ ∀ r. { | Compiler r } -> Effect (ReactComponent QuizProps)
178 fillInTheGaps <- FillInTheGaps.makeComponent
179 reactComponent "Quiz" \({ initialSegments, onFailure, onSuccess } ∷ QuizProps) -> React.do
180 segments /\ updateSegments <- useState initialSegments
181 solvedWith /\ updateSolvedWith <- useState Nothing
183 $ element fillInTheGaps
187 let code = toCode segments
188 result <- compiler.compileAndRun { code }
189 liftEffect case result of
191 | String.stripSuffix (String.Pattern "\n") r.stdout == (findResult $ join segments) -> do
192 updateSolvedWith (const $ String.stripSuffix (String.Pattern "\n") r.stdout)
195 | r.stdout /= "\n" -> onFailure "Oh shit!" [ R.text r.stdout ]
196 Right r -> onFailure "Oh shit!" [ R.text r.stderr ]
197 Left (cr ∷ CompileResult) -> onFailure "Oh shit!" [ R.text (intercalate ", " (cr.result <#> _.message)) ]
200 let updated = update segments
201 unless (segments == updated) do updateSegments (const updated)
205foreign import mdxProvider ∷
208 { components ∷ { | r }
209 , children ∷ ReactChildren MDX
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list