@charmland/gem 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,12 @@ module Gem.Render exposing
2
2
  ( Code
3
3
  , CodeView
4
4
  , blocks
5
+ , blocksWithHref
5
6
  , codeHead
6
7
  , codeLines
7
8
  , defaultCodeView
8
9
  , inlines
10
+ , inlinesWithHref
9
11
  , linesToText
10
12
  )
11
13
 
@@ -45,28 +47,46 @@ type alias CodeView msg =
45
47
  Code -> Html msg
46
48
 
47
49
 
48
- {-| Render a list of blocks.
50
+ {-| Render a list of blocks with links and images left as authored.
49
51
  -}
50
52
  blocks : Slots msg -> CodeView msg -> List Block -> List (Html msg)
51
53
  blocks slots codeView list =
52
- List.map (block slots codeView) list
54
+ blocksWithHref identity slots codeView list
53
55
 
54
56
 
55
- {-| Render a list of inlines.
57
+ {-| Render a list of blocks, rewriting internal link/image URLs and the
58
+ `href`/`src`/`poster` component props with `rewrite` before they reach a
59
+ `Slots` renderer. The default shell passes `Gem.Page.href` so a non-root base
60
+ prefixes every generated URL; a custom renderer that wants base support calls
61
+ this with the same function.
62
+ -}
63
+ blocksWithHref : (String -> String) -> Slots msg -> CodeView msg -> List Block -> List (Html msg)
64
+ blocksWithHref rewrite slots codeView list =
65
+ List.map (block rewrite slots codeView) list
66
+
67
+
68
+ {-| Render a list of inlines with links and images left as authored.
56
69
  -}
57
70
  inlines : Slots msg -> List Inline -> List (Html msg)
58
71
  inlines slots list =
59
- List.map (inline slots) list
72
+ inlinesWithHref identity slots list
73
+
74
+
75
+ {-| Render a list of inlines, rewriting internal URLs with `rewrite`.
76
+ -}
77
+ inlinesWithHref : (String -> String) -> Slots msg -> List Inline -> List (Html msg)
78
+ inlinesWithHref rewrite slots list =
79
+ List.map (inline rewrite slots) list
60
80
 
61
81
 
62
- block : Slots msg -> CodeView msg -> Block -> Html msg
63
- block slots codeView node =
82
+ block : (String -> String) -> Slots msg -> CodeView msg -> Block -> Html msg
83
+ block rewrite slots codeView node =
64
84
  case node of
65
85
  Heading depth id children ->
66
- heading depth id (inlines slots children)
86
+ heading depth id (inlinesWithHref rewrite slots children)
67
87
 
68
88
  Paragraph children ->
69
- Html.p [ A.class "md-p" ] (inlines slots children)
89
+ Html.p [ A.class "md-p" ] (inlinesWithHref rewrite slots children)
70
90
 
71
91
  CodeBlock code ->
72
92
  codeView
@@ -77,13 +97,13 @@ block slots codeView node =
77
97
  }
78
98
 
79
99
  ListBlock list ->
80
- listBlock slots codeView list
100
+ listBlock rewrite slots codeView list
81
101
 
82
102
  Quote children ->
83
- Html.blockquote [ A.class "md-quote" ] (blocks slots codeView children)
103
+ Html.blockquote [ A.class "md-quote" ] (blocksWithHref rewrite slots codeView children)
84
104
 
85
105
  Table table ->
86
- tableView slots table
106
+ tableView rewrite slots table
87
107
 
88
108
  Rule ->
89
109
  Html.hr [] []
@@ -97,13 +117,13 @@ block slots codeView node =
97
117
  BlockComponent "ui-cards" props children ->
98
118
  case cardGroups children of
99
119
  Just groups ->
100
- cardsGroupView slots codeView groups
120
+ cardsGroupView rewrite slots codeView groups
101
121
 
102
122
  Nothing ->
103
- slots.component "ui-cards" props (blocks slots codeView children)
123
+ slots.component "ui-cards" (rewriteProps rewrite props) (blocksWithHref rewrite slots codeView children)
104
124
 
105
125
  BlockComponent tag props children ->
106
- slots.component tag props (blocks slots codeView children)
126
+ slots.component tag (rewriteProps rewrite props) (blocksWithHref rewrite slots codeView children)
107
127
 
108
128
 
109
129
  {-| The code panels inside a `<code-group>`: every direct child that is a
@@ -195,11 +215,12 @@ cardGroups children =
195
215
  visible. The switching is the theme boot's tab behavior.
196
216
  -}
197
217
  cardsGroupView :
198
- Slots msg
218
+ (String -> String)
219
+ -> Slots msg
199
220
  -> CodeView msg
200
221
  -> List { label : String, cards : List Block }
201
222
  -> Html msg
202
- cardsGroupView slots codeView groups =
223
+ cardsGroupView rewrite slots codeView groups =
203
224
  Html.div
204
225
  [ A.class "cards-root", A.attribute "data-tabs" "" ]
205
226
  ([ Html.nav
@@ -229,14 +250,14 @@ cardsGroupView slots codeView groups =
229
250
  []
230
251
  )
231
252
  )
232
- [ Html.div [ A.class "cards" ] (blocks slots codeView group.cards) ]
253
+ [ Html.div [ A.class "cards" ] (blocksWithHref rewrite slots codeView group.cards) ]
233
254
  )
234
255
  groups
235
256
  )
236
257
 
237
258
 
238
- inline : Slots msg -> Inline -> Html msg
239
- inline slots node =
259
+ inline : (String -> String) -> Slots msg -> Inline -> Html msg
260
+ inline rewrite slots node =
240
261
  case node of
241
262
  Text text ->
242
263
  Html.text text
@@ -245,18 +266,18 @@ inline slots node =
245
266
  Html.code [ A.class "code-inline" ] [ Html.text text ]
246
267
 
247
268
  Strong children ->
248
- Html.strong [] (inlines slots children)
269
+ Html.strong [] (inlinesWithHref rewrite slots children)
249
270
 
250
271
  Em children ->
251
- Html.em [] (inlines slots children)
272
+ Html.em [] (inlinesWithHref rewrite slots children)
252
273
 
253
274
  Del children ->
254
- Html.del [] (inlines slots children)
275
+ Html.del [] (inlinesWithHref rewrite slots children)
255
276
 
256
277
  Link link children ->
257
278
  Html.a
258
279
  (A.class "md-link"
259
- :: A.href link.href
280
+ :: A.href (rewrite link.href)
260
281
  :: A.title (Maybe.withDefault "" link.title)
261
282
  :: (if link.external then
262
283
  [ A.target "_blank", A.rel "noreferrer" ]
@@ -265,12 +286,12 @@ inline slots node =
265
286
  []
266
287
  )
267
288
  )
268
- (inlines slots children)
289
+ (inlinesWithHref rewrite slots children)
269
290
 
270
291
  Image image ->
271
292
  Html.img
272
293
  [ A.class "md-image"
273
- , A.src image.src
294
+ , A.src (rewrite image.src)
274
295
  , A.alt image.alt
275
296
  , A.title (Maybe.withDefault "" image.title)
276
297
  ]
@@ -280,7 +301,7 @@ inline slots node =
280
301
  Html.br [] []
281
302
 
282
303
  InlineComponent tag props children ->
283
- slots.component tag props (inlines slots children)
304
+ slots.component tag (rewriteProps rewrite props) (inlinesWithHref rewrite slots children)
284
305
 
285
306
 
286
307
  heading : Int -> String -> List (Html msg) -> Html msg
@@ -435,11 +456,11 @@ token value =
435
456
  [ Html.text value.text ]
436
457
 
437
458
 
438
- listBlock : Slots msg -> CodeView msg -> { ordered : Bool, start : Int, items : List (List Block) } -> Html msg
439
- listBlock slots codeView list =
459
+ listBlock : (String -> String) -> Slots msg -> CodeView msg -> { ordered : Bool, start : Int, items : List (List Block) } -> Html msg
460
+ listBlock rewrite slots codeView list =
440
461
  let
441
462
  children =
442
- List.map (\item -> Html.li [] (blocks slots codeView item)) list.items
463
+ List.map (\item -> Html.li [] (blocksWithHref rewrite slots codeView item)) list.items
443
464
  in
444
465
  if list.ordered then
445
466
  Html.ol
@@ -457,16 +478,33 @@ listBlock slots codeView list =
457
478
  Html.ul [ A.class "md-ul" ] children
458
479
 
459
480
 
460
- tableView : Slots msg -> { head : List (List Inline), rows : List (List (List Inline)) } -> Html msg
461
- tableView slots table =
481
+ tableView : (String -> String) -> Slots msg -> { head : List (List Inline), rows : List (List (List Inline)) } -> Html msg
482
+ tableView rewrite slots table =
462
483
  Html.div [ A.class "table-wrap" ]
463
484
  [ Html.table []
464
485
  [ Html.thead []
465
- [ Html.tr [] (List.map (\cell -> Html.th [] (inlines slots cell)) table.head) ]
486
+ [ Html.tr [] (List.map (\cell -> Html.th [] (inlinesWithHref rewrite slots cell)) table.head) ]
466
487
  , Html.tbody []
467
488
  (List.map
468
- (\row -> Html.tr [] (List.map (\cell -> Html.td [] (inlines slots cell)) row))
489
+ (\row -> Html.tr [] (List.map (\cell -> Html.td [] (inlinesWithHref rewrite slots cell)) row))
469
490
  table.rows
470
491
  )
471
492
  ]
472
493
  ]
494
+
495
+
496
+ {-| Rewrite the URL props a component carries (`href`, `src`, `poster`) before
497
+ the `Slots` renderer sees them, so `ui-card` and custom elements receive the
498
+ base-prefixed value without changing the `Slots` record shape.
499
+ -}
500
+ rewriteProps : (String -> String) -> Dict String String -> Dict String String
501
+ rewriteProps rewrite props =
502
+ Dict.map
503
+ (\key value ->
504
+ if key == "href" || key == "src" || key == "poster" then
505
+ rewrite value
506
+
507
+ else
508
+ value
509
+ )
510
+ props