diff options
| author | Fuwn <[email protected]> | 2024-06-24 09:55:18 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-06-24 09:55:18 +0000 |
| commit | cdd0f2f69aebad62e1dab910ae8400dc6716b42f (patch) | |
| tree | dcec4fb7c28621552bd47bca2f402f1ad63a71ec | |
| parent | docs(readme): update description (diff) | |
| download | momoka-cdd0f2f69aebad62e1dab910ae8400dc6716b42f.tar.xz momoka-cdd0f2f69aebad62e1dab910ae8400dc6716b42f.zip | |
feat(gopher): directory prefix absolute urls
| -rw-r--r-- | src/gemini.gleam | 17 | ||||
| -rw-r--r-- | src/gopher.gleam | 11 |
2 files changed, 20 insertions, 8 deletions
diff --git a/src/gemini.gleam b/src/gemini.gleam index ad04079..52d5e08 100644 --- a/src/gemini.gleam +++ b/src/gemini.gleam @@ -4,6 +4,7 @@ import gleam/bit_array import gleam/bytes_builder import gleam/http/request import gleam/httpc +import gleam/string import gopher pub fn get_gemtext_from_capsule(message) { @@ -24,16 +25,20 @@ pub fn get_gemtext_from_capsule(message) { } let assert Ok(request) = case bit_array.to_string(message) { Ok(path) -> { - case path { - "/\r\n" | "\r\n" | "\n" -> request.to(gemini_proxy <> root_capsule) - "/proxy/" <> route -> - request.to(gemini_proxy <> gopher.trim_gopher_line_ending(route)) - "/" <> path -> + case string.split(path, "/") { + ["/\r\n"] | ["\r\n"] | ["\n"] -> + request.to(gemini_proxy <> root_capsule) + ["", "proxy", ..route] -> + request.to( + gemini_proxy + <> gopher.trim_gopher_line_ending(string.join(route, "/")), + ) + ["", ..path] -> request.to( gemini_proxy <> root_capsule <> "/" - <> gopher.trim_gopher_line_ending(path), + <> gopher.trim_gopher_line_ending(string.join(path, "/")), ) _ -> request.to(root_capsule <> gopher.trim_gopher_line_ending(path)) } diff --git a/src/gopher.gleam b/src/gopher.gleam index 162ac22..1c8b277 100644 --- a/src/gopher.gleam +++ b/src/gopher.gleam @@ -4,7 +4,9 @@ import gleam/option.{None, Some} import gleam/string pub fn trim_gopher_line_ending(line) { - string.replace(line, "\r", "") |> string.replace("\n", "") + line + |> string.replace("\n", "") + |> string.replace("\r", "") } fn terminate_text_line(line) { @@ -59,7 +61,12 @@ fn gopher_link_line(to, description) { "h" <> description <> "\tURL:" - <> to + <> { + case string.starts_with(to, "/") { + True -> "/1" <> to + False -> to + } + } <> "\t" <> extract_domain_from_url(to) <> "\t70" |