@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charmland/gem",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Gem's Elm sources, default application, browser runtime, UI kit, stylesheet and fonts.",
5
5
  "keywords": ["gem", "elm", "documentation", "theme", "ui"],
6
6
  "homepage": "https://github.com/charmbracelet/gem/tree/main/packages/gem",
@@ -77,7 +77,7 @@
77
77
  "prepack": "bun run build"
78
78
  },
79
79
  "dependencies": {
80
- "@charmland/gem-compiler": "0.1.0"
80
+ "@charmland/gem-compiler": "0.1.1"
81
81
  },
82
82
  "devDependencies": {
83
83
  "@types/node": "^26.6.5",
@@ -107,10 +107,10 @@ init slots flags url key =
107
107
  Ok site ->
108
108
  let
109
109
  page =
110
- Page.byRoute site (normalize url.path)
110
+ Page.byRoute site (Page.routeOf site url.path)
111
111
 
112
112
  doc =
113
- Page.docFor site (normalize url.path)
113
+ Page.docFor site (Page.routeOf site url.path)
114
114
 
115
115
  model =
116
116
  { site = site
@@ -202,10 +202,10 @@ update msg model =
202
202
  ChangedUrl url ->
203
203
  let
204
204
  page =
205
- Page.byRoute model.site (normalize url.path)
205
+ Page.byRoute model.site (Page.routeOf model.site url.path)
206
206
 
207
207
  doc =
208
- Page.docFor model.site (normalize url.path)
208
+ Page.docFor model.site (Page.routeOf model.site url.path)
209
209
 
210
210
  next =
211
211
  { model
@@ -262,7 +262,7 @@ update msg model =
262
262
  case inner of
263
263
  Ui.Search.Select route ->
264
264
  ( { model | search = search, query = "", searchOpen = False }
265
- , Nav.pushUrl model.key route
265
+ , Nav.pushUrl model.key (Page.href model.site route)
266
266
  )
267
267
 
268
268
  _ ->
@@ -446,7 +446,7 @@ update msg model =
446
446
  Just page ->
447
447
  ( { model | pageCopied = True }
448
448
  , Cmd.batch
449
- [ Ports.copyMarkdown (Page.markdownPath page)
449
+ [ Ports.copyMarkdown (Page.markdownHref model.site page)
450
450
  , Process.sleep 1200
451
451
  |> Task.perform (\_ -> PageCopyReset)
452
452
  ]
@@ -699,7 +699,7 @@ pageUrl : Model -> String
699
699
  pageUrl model =
700
700
  case model.page of
701
701
  Just page ->
702
- origin model ++ Page.markdownPath page
702
+ origin model ++ Page.markdownHref model.site page
703
703
 
704
704
  Nothing ->
705
705
  origin model
@@ -840,13 +840,17 @@ setMeta model =
840
840
  |> Maybe.map (\item -> String.join ", " item.tags)
841
841
  |> Maybe.withDefault ""
842
842
  , url =
843
- page
844
- |> Maybe.map .route
845
- |> Maybe.withDefault "/"
843
+ Page.href model.site
844
+ (page
845
+ |> Maybe.map .route
846
+ |> Maybe.withDefault "/"
847
+ )
846
848
  , image =
847
- page
848
- |> Maybe.map ogPath
849
- |> Maybe.withDefault "/og/index.png"
849
+ Page.href model.site
850
+ (page
851
+ |> Maybe.map ogPath
852
+ |> Maybe.withDefault "/og/index.png"
853
+ )
850
854
  }
851
855
 
852
856
 
@@ -915,7 +919,7 @@ docNavigation model before after =
915
919
  of
916
920
  Just doc ->
917
921
  if doc.id /= model.doc.id then
918
- Nav.pushUrl model.key doc.root
922
+ Nav.pushUrl model.key (Page.href model.site doc.root)
919
923
 
920
924
  else
921
925
  Cmd.none
@@ -1218,18 +1222,6 @@ closeBurger burger =
1218
1222
  burger
1219
1223
 
1220
1224
 
1221
- normalize : String -> String
1222
- normalize path =
1223
- if String.length path > 1 && String.endsWith "/" path then
1224
- String.dropRight 1 path
1225
-
1226
- else if path == "" then
1227
- "/"
1228
-
1229
- else
1230
- path
1231
-
1232
-
1233
1225
  scrollTo : Url -> Cmd Msg
1234
1226
  scrollTo url =
1235
1227
  case url.fragment of
@@ -1272,4 +1264,5 @@ emptySite =
1272
1264
  , toolbar = { copy = True, openIn = True }
1273
1265
  , footer = { enabled = False, text = "Gem", blurb = "", chips = [], columns = [], eof = False }
1274
1266
  }
1267
+ , base = "/"
1275
1268
  }
@@ -191,14 +191,14 @@ header config =
191
191
  ]
192
192
  ]
193
193
  ([ if chrome.logo then
194
- Html.a [ A.class "docs-header-title", A.href config.doc.root ]
194
+ Html.a [ A.class "docs-header-title", A.href (Page.href config.site config.doc.root) ]
195
195
  [ logoView config.site.meta.logo config ]
196
196
 
197
197
  else
198
198
  Html.text ""
199
199
  ]
200
200
  ++ [ Html.div [ A.class "docs-header-end" ]
201
- (List.map headerAction chrome.actions
201
+ (List.map (headerAction config) chrome.actions
202
202
  ++ [ if chrome.search then
203
203
  searchToggle config
204
204
 
@@ -242,8 +242,8 @@ searchToggle config =
242
242
  [ Ui.Icon.view Ui.Icon.Search ]
243
243
 
244
244
 
245
- headerAction : { label : String, href : String } -> Html msg
246
- headerAction action =
245
+ headerAction : Config msg -> { label : String, href : String } -> Html msg
246
+ headerAction config action =
247
247
  Ui.Button.viewLink
248
248
  { label = action.label
249
249
  , variant = Ui.Button.Secondary
@@ -253,7 +253,7 @@ headerAction action =
253
253
  , icon = Nothing
254
254
  , attributes = [ A.class "docs-header-action" ]
255
255
  }
256
- action.href
256
+ (Page.href config.site action.href)
257
257
 
258
258
 
259
259
  {-| A site brand: its configured logo image when it has one, otherwise the
@@ -264,7 +264,7 @@ logoView : String -> Config msg -> Html msg
264
264
  logoView label config =
265
265
  case config.site.meta.logoImage of
266
266
  Just src ->
267
- Html.node "img" [ A.class "site-logo", A.src src, A.alt label ] []
267
+ Html.node "img" [ A.class "site-logo", A.src (Page.href config.site src), A.alt label ] []
268
268
 
269
269
  Nothing ->
270
270
  Html.text label
@@ -289,7 +289,7 @@ footerView config =
289
289
  , Html.ul []
290
290
  (List.map
291
291
  (\link ->
292
- Html.li [] [ Html.a [ A.href link.href ] [ Html.text link.label ] ]
292
+ Html.li [] [ Html.a [ A.href (Page.href config.site link.href) ] [ Html.text link.label ] ]
293
293
  )
294
294
  view_.links
295
295
  )
@@ -448,8 +448,8 @@ navItem : Config msg -> String -> String -> Ui.Sidebar.Item msg
448
448
  navItem config label route =
449
449
  { label = label
450
450
  , icon = Nothing
451
- , target = Ui.Sidebar.Href route
452
- , active = isActive config.url.path route
451
+ , target = Ui.Sidebar.Href (Page.href config.site route)
452
+ , active = isActive config route
453
453
  }
454
454
 
455
455
 
@@ -462,7 +462,7 @@ pageView config =
462
462
  [ Ui.Toolbar.view { title = "Not found", actions = [], attributes = [] }
463
463
  , Html.p [ A.class "md-p" ]
464
464
  [ Html.text "There is no page at this address. "
465
- , Html.a [ A.class "md-link", A.href config.doc.root ] [ Html.text "Back to the introduction" ]
465
+ , Html.a [ A.class "md-link", A.href (Page.href config.site config.doc.root) ] [ Html.text "Back to the introduction" ]
466
466
  , Html.text "."
467
467
  ]
468
468
  ]
@@ -482,7 +482,7 @@ pageView config =
482
482
  ++ lede page
483
483
  ++ [ pageActions config ]
484
484
  ++ [ Html.div [ A.class "md" ]
485
- (Render.blocks config.slots config.codeView page.blocks)
485
+ (Render.blocksWithHref (Page.href config.site) config.slots config.codeView page.blocks)
486
486
  ]
487
487
  ++ [ pageTail config page ]
488
488
  ++ [ pager config page ]
@@ -598,27 +598,37 @@ eofBand config page =
598
598
  ]
599
599
 
600
600
 
601
- {-| The page's markdown URL: absolute when the site declares an origin,
602
- relative (so it still works in dev) when it doesn't.
601
+ {-| The page's markdown URL: absolute when the site declares an origin
602
+ (origin only — any path, query or fragment in `site.url` is ignored, matching
603
+ the build), and relative so it still works in dev and under a base when it
604
+ doesn't.
603
605
  -}
604
606
  pageMarkdownUrl : Config msg -> Page -> String
605
607
  pageMarkdownUrl config page =
606
608
  let
609
+ markdown =
610
+ Page.markdownHref config.site page
611
+
607
612
  raw =
608
613
  String.trim config.site.meta.url
609
614
 
610
- origin =
611
- if String.endsWith "/" raw then
612
- String.dropRight 1 raw
615
+ candidate =
616
+ if String.contains "://" raw then
617
+ raw
613
618
 
614
619
  else
615
- raw
620
+ "https://" ++ raw
616
621
  in
617
- if String.isEmpty origin then
618
- Page.markdownPath page
622
+ if String.isEmpty raw then
623
+ markdown
619
624
 
620
625
  else
621
- origin ++ Page.markdownPath page
626
+ case Url.fromString candidate of
627
+ Just parsed ->
628
+ Url.toString { parsed | path = "", query = Nothing, fragment = Nothing } ++ markdown
629
+
630
+ Nothing ->
631
+ markdown
622
632
 
623
633
 
624
634
  tocView : Config msg -> Page -> Html msg
@@ -842,17 +852,17 @@ pager config page =
842
852
 
843
853
  else
844
854
  Ui.PageNav.view
845
- { previous = Maybe.map pageNavItem previous
846
- , next = Maybe.map pageNavItem next
855
+ { previous = Maybe.map (pageNavItem config) previous
856
+ , next = Maybe.map (pageNavItem config) next
847
857
  , attributes = []
848
858
  }
849
859
 
850
860
 
851
- pageNavItem : Page.Entry -> Ui.PageNav.Item
852
- pageNavItem entry =
861
+ pageNavItem : Config msg -> Page.Entry -> Ui.PageNav.Item
862
+ pageNavItem config entry =
853
863
  { title = entry.title
854
864
  , description = entry.summary
855
- , href = entry.route
865
+ , href = Page.href config.site entry.route
856
866
  }
857
867
 
858
868
 
@@ -883,26 +893,9 @@ itemAt index items =
883
893
  items |> List.drop index |> List.head
884
894
 
885
895
 
886
- isActive : String -> String -> Bool
887
- isActive path route =
888
- normalize path == normalize route
889
-
890
-
891
- normalize : String -> String
892
- normalize path =
893
- let
894
- trimmed =
895
- if String.length path > 1 && String.endsWith "/" path then
896
- String.dropRight 1 path
897
-
898
- else
899
- path
900
- in
901
- if trimmed == "" then
902
- "/"
903
-
904
- else
905
- trimmed
896
+ isActive : Config msg -> String -> Bool
897
+ isActive config route =
898
+ Page.routeOf config.site config.url.path == route
906
899
 
907
900
 
908
901
  drawer : Config msg -> Html msg
@@ -1,4 +1,4 @@
1
- module Gem.Page exposing (Chrome, ChromeLink, ChromeColumn, Color, Doc, Entry, FooterConfig, HeaderConfig, Heading, Meta, NavItem(..), Page, Section, Site, ToolbarConfig, byRoute, doc, docFor, markdownPath, nav, page, site)
1
+ module Gem.Page exposing (Chrome, ChromeLink, ChromeColumn, Color, Doc, Entry, FooterConfig, HeaderConfig, Heading, Meta, NavItem(..), Page, Section, Site, ToolbarConfig, byRoute, doc, docFor, href, markdownHref, markdownPath, nav, page, routeOf, site)
2
2
 
3
3
  {-| Site, page and navigation decoders. The compiler's JSON lands here and
4
4
  nowhere else, so this module is the schema in Elm form.
@@ -163,7 +163,8 @@ type alias Doc =
163
163
  }
164
164
 
165
165
 
166
- {-| Everything the shell needs, decoded once at boot.
166
+ {-| Everything the shell needs, decoded once at boot. `base` is the deployment
167
+ mount path (`"/"` or `"/sub/"`); route IDs and the filesystem stay base-free.
167
168
  -}
168
169
  type alias Site =
169
170
  { meta : Meta
@@ -174,6 +175,7 @@ type alias Site =
174
175
  , theme : String
175
176
  , themeSystem : Bool
176
177
  , chrome : Chrome
178
+ , base : String
177
179
  }
178
180
 
179
181
 
@@ -181,28 +183,177 @@ type alias Site =
181
183
  -}
182
184
  site : D.Decoder Site
183
185
  site =
184
- D.map8 Site
185
- (D.map8 Meta
186
- (D.at [ "site", "title" ] D.string)
187
- (D.at [ "site", "description" ] D.string)
188
- (D.at [ "site", "url" ] D.string)
189
- (D.at [ "site", "lang" ] D.string)
190
- (D.at [ "site", "logo" ] D.string)
191
- (D.oneOf [ D.at [ "site", "logoImage" ] (D.nullable D.string), D.succeed Nothing ])
192
- (D.at [ "site", "footer" ] (D.nullable D.string))
193
- (D.oneOf [ D.at [ "site", "editUrl" ] (D.nullable D.string), D.succeed Nothing ])
194
- )
195
- (D.field "pages" (D.list page) |> D.map pagesByRoute)
196
- (D.field "docs" (D.list doc))
197
- (D.field "search" (D.list entry))
198
- (D.field "colors" (D.list color))
199
- (D.oneOf
200
- [ D.at [ "theme", "mode" ] D.string
201
- , D.succeed "dark"
202
- ]
186
+ D.map2
187
+ (\build baseRaw -> build (normalizeBase baseRaw))
188
+ (D.map8 Site
189
+ (D.map8 Meta
190
+ (D.at [ "site", "title" ] D.string)
191
+ (D.at [ "site", "description" ] D.string)
192
+ (D.at [ "site", "url" ] D.string)
193
+ (D.at [ "site", "lang" ] D.string)
194
+ (D.at [ "site", "logo" ] D.string)
195
+ (D.oneOf [ D.at [ "site", "logoImage" ] (D.nullable D.string), D.succeed Nothing ])
196
+ (D.at [ "site", "footer" ] (D.nullable D.string))
197
+ (D.oneOf [ D.at [ "site", "editUrl" ] (D.nullable D.string), D.succeed Nothing ])
198
+ )
199
+ (D.field "pages" (D.list page) |> D.map pagesByRoute)
200
+ (D.field "docs" (D.list doc))
201
+ (D.field "search" (D.list entry))
202
+ (D.field "colors" (D.list color))
203
+ (D.oneOf
204
+ [ D.at [ "theme", "mode" ] D.string
205
+ , D.succeed "dark"
206
+ ]
207
+ )
208
+ (D.oneOf [ D.at [ "theme", "system" ] D.bool, D.succeed False ])
209
+ (D.at [ "chrome" ] chrome)
203
210
  )
204
- (D.oneOf [ D.at [ "theme", "system" ] D.bool, D.succeed False ])
205
- (D.at [ "chrome" ] chrome)
211
+ (D.oneOf [ D.field "base" D.string, D.succeed "/" ])
212
+
213
+
214
+ {-| A base path normalized to `"/"` or `"/sub/"`, matching the compiler's
215
+ `normalizeBase`: an absolute `http(s)` URL collapses to its pathname, a query or
216
+ fragment is dropped, and repeated, leading and trailing slashes collapse.
217
+ -}
218
+ normalizeBase : String -> String
219
+ normalizeBase raw =
220
+ let
221
+ trimmed =
222
+ String.trim raw
223
+ in
224
+ if String.isEmpty trimmed || trimmed == "/" then
225
+ "/"
226
+
227
+ else
228
+ let
229
+ pathOnly =
230
+ if isHttpUrl trimmed then
231
+ urlPath trimmed
232
+
233
+ else
234
+ trimmed
235
+
236
+ clean =
237
+ before "#" (before "?" pathOnly)
238
+
239
+ segments =
240
+ clean
241
+ |> String.split "/"
242
+ |> List.filter (\segment -> not (String.isEmpty segment))
243
+ in
244
+ if List.isEmpty segments then
245
+ "/"
246
+
247
+ else
248
+ "/" ++ String.join "/" segments ++ "/"
249
+
250
+
251
+ {-| Whether a string is an absolute `http(s)` URL. A protocol-relative
252
+ `//host/path` is deliberately not treated as one, matching the compiler.
253
+ -}
254
+ isHttpUrl : String -> Bool
255
+ isHttpUrl value =
256
+ let
257
+ lower =
258
+ String.toLower value
259
+ in
260
+ String.startsWith "http://" lower || String.startsWith "https://" lower
261
+
262
+
263
+ {-| The path portion of an absolute URL, from the first slash after the
264
+ authority; `"https://cdn.example/sub/x"` becomes `"/sub/x"`.
265
+ -}
266
+ urlPath : String -> String
267
+ urlPath url =
268
+ let
269
+ afterScheme =
270
+ String.split "://" url |> List.drop 1 |> String.join "://"
271
+
272
+ pathParts =
273
+ String.split "/" afterScheme |> List.drop 1
274
+ in
275
+ "/" ++ String.join "/" pathParts
276
+
277
+
278
+ {-| The part of a string before the first marker, so `before "?"` drops a query.
279
+ -}
280
+ before : String -> String -> String
281
+ before marker text =
282
+ text
283
+ |> String.split marker
284
+ |> List.head
285
+ |> Maybe.withDefault ""
286
+
287
+
288
+ {-| The route a browser path belongs to: strip the deployment base, then
289
+ normalize. `"/deploy/guides/install"` becomes `"/guides/install"`; the base
290
+ root becomes `"/"`; a path outside the base is returned unchanged.
291
+ -}
292
+ routeOf : Site -> String -> String
293
+ routeOf site_ path =
294
+ let
295
+ basePath =
296
+ site_.base
297
+
298
+ withoutBase =
299
+ if basePath == "/" then
300
+ path
301
+
302
+ else if String.startsWith basePath path then
303
+ "/" ++ String.dropLeft (String.length basePath) path
304
+
305
+ else if path ++ "/" == basePath then
306
+ "/"
307
+
308
+ else
309
+ path
310
+ in
311
+ normalizeRoute withoutBase
312
+
313
+
314
+ {-| Prefix an internal, root-absolute URL with the deployment base. Inputs are
315
+ base-free route IDs, markdown paths or OG paths, so `/docs/guides` under base
316
+ `/docs/` becomes `/docs/docs/guides`. External schemes, protocol-relative URLs
317
+ (`//host/...`), fragments and relative paths pass through unchanged. The `//`
318
+ exclusion is the only idempotence guard; callers must not pass a value that
319
+ already contains the base.
320
+ -}
321
+ href : Site -> String -> String
322
+ href site_ value =
323
+ let
324
+ basePath =
325
+ site_.base
326
+ in
327
+ if basePath == "/" || not (isRootAbsolute value) then
328
+ value
329
+
330
+ else
331
+ String.dropRight 1 basePath ++ value
332
+
333
+
334
+ {-| A page's markdown twin URL under the deployment base. The file itself is
335
+ base-free on disk (`markdownPath`); this is its deployed address.
336
+ -}
337
+ markdownHref : Site -> Page -> String
338
+ markdownHref site_ target =
339
+ href site_ (markdownPath target)
340
+
341
+
342
+ isRootAbsolute : String -> Bool
343
+ isRootAbsolute value =
344
+ String.startsWith "/" value && not (String.startsWith "//" value)
345
+
346
+
347
+ normalizeRoute : String -> String
348
+ normalizeRoute path =
349
+ if path == "" then
350
+ "/"
351
+
352
+ else if String.length path > 1 && String.endsWith "/" path then
353
+ String.dropRight 1 path
354
+
355
+ else
356
+ path
206
357
 
207
358
 
208
359
  chrome : D.Decoder Chrome