display note backlinks in contexts

This commit is contained in:
shibao 2022-11-26 16:36:30 -05:00
parent ca5b29c914
commit 3dceb17085
9 changed files with 81 additions and 52 deletions

View File

@ -0,0 +1,44 @@
defmodule MemexWeb.Components.ContextContent do
@moduledoc """
Display the content for a context
"""
use MemexWeb, :component
alias Memex.Contexts.Context
alias Phoenix.HTML
attr :context, Context, required: true
def context_content(assigns) do
~H"""
<div
id={"show-context-content-#{@context.id}"}
class="input input-primary h-128 min-h-128 inline-block"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><p class="inline"><%= add_links_to_content(@context.content) %></p></div>
"""
end
defp add_links_to_content(content) do
Regex.replace(
~r/\[\[([\p{L}\p{N}\-]+)\]\]/,
content,
fn _whole_match, slug ->
link =
HTML.Link.link(
"[[#{slug}]]",
to: Routes.note_show_path(Endpoint, :show, slug),
class: "link inline",
data: [qa: "context-note-#{slug}"]
)
|> HTML.Safe.to_iodata()
|> IO.iodata_to_binary()
"</p>#{link}<p class=\"inline\">"
end
)
|> HTML.raw()
end
end

View File

@ -1,29 +0,0 @@
defmodule MemexWeb.Components.NoteCard do
@moduledoc """
Display card for an note
"""
use MemexWeb, :component
def note_card(assigns) do
~H"""
<div class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center space-y-4
border border-gray-400 rounded-lg shadow-lg hover:shadow-md
transition-all duration-300 ease-in-out">
<h1 class="title text-xl">
<%= @note.name %>
</h1>
<h2 class="title text-md">
<%= gettext("visibility: %{visibility}", visibility: @note.visibility) %>
</h2>
<%= if @inner_block do %>
<div class="flex space-x-4 justify-center items-center">
<%= render_slot(@inner_block) %>
</div>
<% end %>
</div>
"""
end
end

View File

@ -20,7 +20,7 @@
class: "input input-primary h-64 min-h-64", class: "input input-primary h-64 min-h-64",
phx_hook: "MaintainAttrs", phx_hook: "MaintainAttrs",
phx_update: "ignore", phx_update: "ignore",
placeholder: gettext("content") placeholder: gettext("use [[note-slug]] to link to a note")
) %> ) %>
<%= error_tag(f, :content) %> <%= error_tag(f, :content) %>

View File

@ -1,6 +1,6 @@
defmodule MemexWeb.ContextLive.Show do defmodule MemexWeb.ContextLive.Show do
use MemexWeb, :live_view use MemexWeb, :live_view
import MemexWeb.Components.ContextContent
alias Memex.{Accounts.User, Contexts, Contexts.Context} alias Memex.{Accounts.User, Contexts, Contexts.Context}
@impl true @impl true

View File

@ -5,14 +5,7 @@
<p><%= if @context.tags, do: @context.tags |> Enum.join(", ") %></p> <p><%= if @context.tags, do: @context.tags |> Enum.join(", ") %></p>
<textarea <.context_content context={@context} />
id="show-context-content"
class="input input-primary h-128 min-h-128"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><%= @context.content %></textarea>
<p class="self-end"> <p class="self-end">
<%= gettext("Visibility: %{visibility}", visibility: @context.visibility) %> <%= gettext("Visibility: %{visibility}", visibility: @context.visibility) %>

View File

@ -68,7 +68,7 @@ msgid "create invite"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:49 #: lib/memex_web/live/context_live/index.html.heex:49
#: lib/memex_web/live/context_live/show.html.heex:41 #: lib/memex_web/live/context_live/show.html.heex:34
#: lib/memex_web/live/note_live/index.html.heex:49 #: lib/memex_web/live/note_live/index.html.heex:49
#: lib/memex_web/live/note_live/show.html.heex:38 #: lib/memex_web/live/note_live/show.html.heex:38
#: lib/memex_web/live/pipeline_live/index.html.heex:49 #: lib/memex_web/live/pipeline_live/index.html.heex:49
@ -83,7 +83,7 @@ msgid "delete user"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:38 #: lib/memex_web/live/context_live/index.html.heex:38
#: lib/memex_web/live/context_live/show.html.heex:30 #: lib/memex_web/live/context_live/show.html.heex:23
#: lib/memex_web/live/note_live/index.html.heex:38 #: lib/memex_web/live/note_live/index.html.heex:38
#: lib/memex_web/live/note_live/show.html.heex:27 #: lib/memex_web/live/note_live/show.html.heex:27
#: lib/memex_web/live/pipeline_live/index.html.heex:38 #: lib/memex_web/live/pipeline_live/index.html.heex:38
@ -141,7 +141,7 @@ msgstr ""
msgid "save" msgid "save"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/show.html.heex:23 #: lib/memex_web/live/context_live/show.html.heex:16
#: lib/memex_web/live/note_live/show.html.heex:23 #: lib/memex_web/live/note_live/show.html.heex:23
#: lib/memex_web/live/pipeline_live/show.html.heex:23 #: lib/memex_web/live/pipeline_live/show.html.heex:23
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format

View File

@ -80,7 +80,7 @@ msgstr ""
msgid "Uses left" msgid "Uses left"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/show.html.heex:18 #: lib/memex_web/live/context_live/show.html.heex:11
#: lib/memex_web/live/note_live/show.html.heex:18 #: lib/memex_web/live/note_live/show.html.heex:18
#: lib/memex_web/live/pipeline_live/show.html.heex:18 #: lib/memex_web/live/pipeline_live/show.html.heex:18
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
@ -109,7 +109,6 @@ msgstr ""
#: lib/memex_web/components/contexts_table_component.ex:48 #: lib/memex_web/components/contexts_table_component.ex:48
#: lib/memex_web/components/notes_table_component.ex:48 #: lib/memex_web/components/notes_table_component.ex:48
#: lib/memex_web/live/context_live/form_component.html.heex:23
#: lib/memex_web/live/note_live/form_component.html.heex:23 #: lib/memex_web/live/note_live/form_component.html.heex:23
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "content" msgid "content"
@ -385,11 +384,6 @@ msgstr ""
msgid "visibility" msgid "visibility"
msgstr "" msgstr ""
#: lib/memex_web/components/note_card.ex:18
#, elixir-autogen, elixir-format
msgid "visibility: %{visibility}"
msgstr ""
#: lib/memex_web/live/note_live/index.ex:29 #: lib/memex_web/live/note_live/index.ex:29
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "new note" msgid "new note"
@ -483,3 +477,8 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "home" msgid "home"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/form_component.html.heex:23
#, elixir-autogen, elixir-format
msgid "use [[note-slug]] to link to a note"
msgstr ""

View File

@ -142,7 +142,7 @@ msgid "are you sure you want to make %{invite_name} unlimited?"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:46 #: lib/memex_web/live/context_live/index.html.heex:46
#: lib/memex_web/live/context_live/show.html.heex:38 #: lib/memex_web/live/context_live/show.html.heex:31
#: lib/memex_web/live/note_live/index.html.heex:46 #: lib/memex_web/live/note_live/index.html.heex:46
#: lib/memex_web/live/note_live/show.html.heex:35 #: lib/memex_web/live/note_live/show.html.heex:35
#: lib/memex_web/live/pipeline_live/index.html.heex:46 #: lib/memex_web/live/pipeline_live/index.html.heex:46

View File

@ -1,8 +1,8 @@
defmodule MemexWeb.ContextLiveTest do defmodule MemexWeb.ContextLiveTest do
use MemexWeb.ConnCase use MemexWeb.ConnCase
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
import Memex.ContextsFixtures import Memex.{ContextsFixtures, NotesFixtures}
alias MemexWeb.Endpoint
@create_attrs %{ @create_attrs %{
"content" => "some content", "content" => "some content",
@ -132,4 +132,26 @@ defmodule MemexWeb.ContextLiveTest do
refute has_element?(index_live, "#context-#{context.id}") refute has_element?(index_live, "#context-#{context.id}")
end end
end end
describe "show with note" do
setup [:register_and_log_in_user]
setup %{user: user} do
%{slug: note_slug} = note = note_fixture(user)
[
note: note,
context:
context_fixture(%{content: "example with backlink to [[#{note_slug}]] note"}, user)
]
end
test "displays context", %{conn: conn, context: context, note: %{slug: note_slug}} do
{:ok, show_live, html} = live(conn, Routes.context_show_path(conn, :show, context.slug))
assert html =~ "context"
assert html =~ Routes.note_show_path(Endpoint, :show, note_slug)
assert has_element?(show_live, "[data-qa=\"context-note-#{note_slug}\"]")
end
end
end end