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

View File

@ -97,12 +97,14 @@ defmodule KetbinWeb.UserAuth do
end end
def owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do 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) assign(conn, :show_edit, (user && user.id == paste.belongs_to) || false)
end end
def ensure_owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do 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 allow_edit = (user && user.id == paste.belongs_to) || false
unless allow_edit do unless allow_edit do
conn conn

View File

@ -8,5 +8,5 @@
</a> </a>
<% end %> <% end %>
</div> </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> </div>