diff --git a/assets/css/app.css b/assets/css/app.css index b02fd29..6593486 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -22,6 +22,10 @@ header { background-color: #1a1a1a } +code { + font-family: 'JetBrains Mono', monospace; +} + @import "./phoenix.css"; /* Alerts and form errors */ diff --git a/lib/ketbin/pastes/utils.ex b/lib/ketbin/pastes/utils.ex index da8115e..d408c71 100644 --- a/lib/ketbin/pastes/utils.ex +++ b/lib/ketbin/pastes/utils.ex @@ -16,4 +16,13 @@ defmodule Ketbin.Pastes.Utils do Enum.map(0..length, fn i -> if Integer.mod(i, 2) == random, do: rand_consonant(), else: rand_vowel() end) |> List.to_string end -end + + def is_url?(url) do + try do + uri = URI.parse(url) + uri.scheme != nil && uri.host =~ "." && Enum.member?(["https", "http"], uri.scheme) + rescue + FunctionClauseError -> false + end + end +end \ No newline at end of file diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index f29262b..e17edcd 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -3,6 +3,7 @@ defmodule KetbinWeb.PageController do alias Ketbin.Pastes alias Ketbin.Pastes.Paste + alias Ketbin.Pastes.Utils def index(conn, _params) do changeset = Pastes.change_paste(%Paste{}) @@ -15,12 +16,20 @@ defmodule KetbinWeb.PageController do end def create(conn, %{"paste" => paste_params}) do - # paste_params = Map.put(paste_params, "id", s) + id = Utils.generate_key() + + is_url = + Map.get(paste_params, "content") + |> Utils.is_url?() + + paste_params = + Map.put(paste_params, "id", id) + |> Map.put("is_url", is_url) + case Pastes.create_paste(paste_params) do {:ok, paste} -> conn - |> put_flash(:info, "Paste created successfully.") - |> redirect(to: Routes.paste_path(conn, :show, paste)) + |> redirect(to: Routes.page_path(conn, :show, paste)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "index.html", changeset: changeset) diff --git a/lib/ketbin_web/templates/page/form.html.eex b/lib/ketbin_web/templates/page/form.html.eex index 8c0782b..be761f5 100644 --- a/lib/ketbin_web/templates/page/form.html.eex +++ b/lib/ketbin_web/templates/page/form.html.eex @@ -8,7 +8,16 @@
+<%= @paste.content %>