elm-pages
elm-pages copied to clipboard
Get directory given a PagePath
It's useful to know what directory a given page is located in.
One use-case would be to compare two pages to see if they are from the same directory or not. This would allow building an index or a navigation at a directory level and not across the entire content folder. The existing Directory#includes function needs a known directory as an argument, ~not a PagePath~ which cannot currently be obtained from a PagePath. If we can obtain the directory from a PagePath, we could use includes
to perform the check.
It would be cool to explore an API that lets you do
PagePath.directory page1 == PagePath.directory page2
Might be as simple as adding the directory to the internal type and constructor, and exposing a function like this:
module PagePath -- ...
type PagePath key
= Internal (List String) (Directory key)
| External String
directory : PagePath key -> Maybe (Directory key)
directory path =
case path of
Internal rawPath directory ->
Just directory
External url ->
Nothing
https://github.com/dillonkearns/elm-pages/blob/master/src/Pages/PagePath.elm#L90-L92
FYI...just noticed a cyclical dependency between PagePath and Directory modules with the above approach, just in case it makes you rethink the API 😬
That's true! Pretty easy to solve by splitting the actual types out into separate modules fortunately 👍 With Elm packages, you can choose which modules to expose in the package, so you can have some internal packages that have the type definitions, and expose constructors to them as functions in the exposed modules.
Another approach would be to add the function to Directory
, e.g. as Directory.of pagePath
.
I'm needing this too, because I want to show a list of sub-pages on each directory's index page. To get the list of sub-pages, I need to be able to filter Pages.allPages
by Directory.includes currentDirectory
, but I can't derive currentDirectory
from the current page's PagePath
.
For anyone else facing this, @dillonkearns on Slack suggested manually writing a mapping function for now:
directoryFor : PagePath Pages.PathKey -> Maybe (Directory Pages.PathKey Directory.WithIndex)
directoryFor page =
[ Pages.pages.directory
, Pages.pages.blog.directory
, Pages.pages.docs.directory
, Pages.pages.showcase.directory
]
|> List.filter
(\directory ->
page |> Directory.includes directory
)
|> List.head