feat(pastes): decide highlighting rules using extension

Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2021-08-17 02:29:24 +05:30
parent 425fb2b571
commit 182a5b1e55
No known key found for this signature in database
GPG Key ID: ED5C54FBBB920E51
3 changed files with 12 additions and 7 deletions

View File

@ -13,21 +13,24 @@ defmodule KetbinWeb.PageController do
end
def show(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do
[head | tail] = String.split(id, ".")
# fetch paste from db
paste = Pastes.get_paste!(id)
paste = Pastes.get_paste!(head)
# paste is a url, redirect
# regular paste, show content
if paste.is_url do
redirect(conn, external: paste.content)
else
render(conn, "show.html", paste: paste, show_edit: show_edit)
render(conn, "show.html", paste: paste, show_edit: show_edit, extension: List.first(tail) || "")
end
end
def showlink(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do
paste = Pastes.get_paste!(id)
render(conn, "show.html", paste: paste, show_edit: show_edit)
[head | tail] = String.split(id, ".")
paste = Pastes.get_paste!(head)
render(conn, "show.html", paste: paste, show_edit: show_edit, extension: List.first(tail) || "")
end
def raw(conn, %{"id" => id}) do

View File

@ -97,12 +97,14 @@ defmodule KetbinWeb.UserAuth do
end
def owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do
paste = Pastes.get_paste!(id)
[head|_tail] = String.split(id, ".")
paste = Pastes.get_paste!(head)
assign(conn, :show_edit, (user && user.id == paste.belongs_to) || false)
end
def ensure_owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do
paste = Pastes.get_paste!(id)
[head|_tail] = String.split(id, ".")
paste = Pastes.get_paste!(head)
allow_edit = (user && user.id == paste.belongs_to) || false
unless allow_edit do
conn

View File

@ -8,5 +8,5 @@
</a>
<% end %>
</div>
<code class="break-word px-6 py-4 h-full w-full nomarkdown overflow-y-auto"><%= raw Ketbin.Utils.Syntax.highlight_text(@paste.content, "rs") %></code>
<code class="break-word px-6 py-4 h-full w-full nomarkdown overflow-y-auto"><%= raw Ketbin.Utils.Syntax.highlight_text(@paste.content, @extension) %></code>
</div>