From 7d16cec5db6af5d4880f9a3a310642e046aae40e Mon Sep 17 00:00:00 2001
From: shibao
Date: Sat, 18 Mar 2023 23:33:19 -0400
Subject: [PATCH] embed links
---
lib/memex_web/components/core_components.ex | 102 +++++++++---------
.../core_components/context_content.html.heex | 8 ++
.../core_components/note_content.html.heex | 8 ++
.../core_components/step_content.html.heex | 8 ++
4 files changed, 73 insertions(+), 53 deletions(-)
create mode 100644 lib/memex_web/components/core_components/context_content.html.heex
create mode 100644 lib/memex_web/components/core_components/note_content.html.heex
create mode 100644 lib/memex_web/components/core_components/step_content.html.heex
diff --git a/lib/memex_web/components/core_components.ex b/lib/memex_web/components/core_components.ex
index 9ab0b1e..dd725e0 100644
--- a/lib/memex_web/components/core_components.ex
+++ b/lib/memex_web/components/core_components.ex
@@ -121,67 +121,63 @@ defmodule MemexWeb.CoreComponents do
attr :note, Note, required: true
- def note_content(assigns) do
- ~H"""
- <%= add_links_to_content(@note.content, "note-link") %>
- """
- end
+ def note_content(assigns)
attr :context, Context, required: true
- def context_content(assigns) do
- ~H"""
- <%= add_links_to_content(@context.content, "context-note") %>
- """
- end
+ def context_content(assigns)
attr :step, Step, required: true
- def step_content(assigns) do
- ~H"""
- <%= add_links_to_content(@step.content, "step-context") %>
- """
- end
+ def step_content(assigns)
defp add_links_to_content(content, data_qa_prefix) 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: "#{data_qa_prefix}-#{slug}"]
- )
- |> HTML.Safe.to_iodata()
- |> IO.iodata_to_binary()
+ # replace links
- "
#{link}"
- end
- )
- |> HTML.raw()
+ # link regex from
+ # https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url
+ # and modified with additional schemes from
+ # https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
+
+ content =
+ Regex.replace(
+ ~r<((file|git|https?|ipfs|ipns|irc|jabber|magnet|mailto|mumble|tel|udp|xmpp):\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*))>,
+ content,
+ fn _whole_match, link ->
+ link =
+ HTML.Link.link(
+ link,
+ to: link,
+ class: "link inline",
+ target: "_blank",
+ rel: "noopener noreferrer"
+ )
+ |> HTML.Safe.to_iodata()
+ |> IO.iodata_to_binary()
+
+ "
#{link}"
+ end
+ )
+
+ content =
+ 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: "#{data_qa_prefix}-#{slug}"]
+ )
+ |> HTML.Safe.to_iodata()
+ |> IO.iodata_to_binary()
+
+ "
#{link}"
+ end
+ )
+
+ content |> HTML.raw()
end
end
diff --git a/lib/memex_web/components/core_components/context_content.html.heex b/lib/memex_web/components/core_components/context_content.html.heex
new file mode 100644
index 0000000..4692f2e
--- /dev/null
+++ b/lib/memex_web/components/core_components/context_content.html.heex
@@ -0,0 +1,8 @@
+
<%= add_links_to_content(@context.content, "context-note") %>
diff --git a/lib/memex_web/components/core_components/note_content.html.heex b/lib/memex_web/components/core_components/note_content.html.heex
new file mode 100644
index 0000000..7189bcd
--- /dev/null
+++ b/lib/memex_web/components/core_components/note_content.html.heex
@@ -0,0 +1,8 @@
+<%= add_links_to_content(@note.content, "note-link") %>
diff --git a/lib/memex_web/components/core_components/step_content.html.heex b/lib/memex_web/components/core_components/step_content.html.heex
new file mode 100644
index 0000000..887e752
--- /dev/null
+++ b/lib/memex_web/components/core_components/step_content.html.heex
@@ -0,0 +1,8 @@
+<%= add_links_to_content(@step.content, "step-context") %>