From 9e9b58f9a6828b9fcb23f0735bafb155a9e6e5e0 Mon Sep 17 00:00:00 2001 From: Inhji Date: Mon, 13 Mar 2023 03:26:39 +0100 Subject: [PATCH] add markdown rendering --- lib/chiya_web.ex | 5 +++-- lib/chiya_web/controllers/page_html/home.html.heex | 2 +- lib/chiya_web/controllers/page_html/note.html.heex | 7 ++++++- lib/chiya_web/markdown.ex | 14 ++++++++++++++ mix.exs | 3 ++- mix.lock | 2 ++ 6 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 lib/chiya_web/markdown.ex diff --git a/lib/chiya_web.ex b/lib/chiya_web.ex index 8c07f1d..35f3ab7 100644 --- a/lib/chiya_web.ex +++ b/lib/chiya_web.ex @@ -92,8 +92,9 @@ defmodule ChiyaWeb do alias Phoenix.LiveView.JS # Custom functions - import ChiyaWeb.Format, only: [from_now: 1, from_now_naive: 1] - + import ChiyaWeb.Format, only: [from_now: 1, pretty_date: 1] + alias ChiyaWeb.Markdown + # Routes generation with the ~p sigil unquote(verified_routes()) end diff --git a/lib/chiya_web/controllers/page_html/home.html.heex b/lib/chiya_web/controllers/page_html/home.html.heex index 2544d3f..ecdd612 100644 --- a/lib/chiya_web/controllers/page_html/home.html.heex +++ b/lib/chiya_web/controllers/page_html/home.html.heex @@ -29,7 +29,7 @@ <.header> <%= note.name %> - <:subtitle><%= note.published_at %> + <:subtitle><%= pretty_date(note.published_at) %> <% end %> diff --git a/lib/chiya_web/controllers/page_html/note.html.heex b/lib/chiya_web/controllers/page_html/note.html.heex index 4235434..96ea187 100644 --- a/lib/chiya_web/controllers/page_html/note.html.heex +++ b/lib/chiya_web/controllers/page_html/note.html.heex @@ -1,3 +1,8 @@ <.header> - <%= @note.name %> + <%= @note.name %> + <:subtitle><%= pretty_date(@note.published_at) %> + +
+ <%= Markdown.render(@note.content) |> raw %> +
diff --git a/lib/chiya_web/markdown.ex b/lib/chiya_web/markdown.ex new file mode 100644 index 0000000..6fd73b0 --- /dev/null +++ b/lib/chiya_web/markdown.ex @@ -0,0 +1,14 @@ +defmodule ChiyaWeb.Markdown do + + @options [ + footnotes: true, + breaks: true, + escape: true + ] + + def render(markdown) do + markdown + |> Earmark.as_html!(@options) + end + +end diff --git a/mix.exs b/mix.exs index cf20132..2cff7e9 100644 --- a/mix.exs +++ b/mix.exs @@ -53,7 +53,8 @@ defmodule Chiya.MixProject do {:plug_cowboy, "~> 2.5"}, {:oban, "~> 2.14"}, {:waffle, "~> 1.1"}, - {:waffle_ecto, "~> 0.0.12"} + {:waffle_ecto, "~> 0.0.12"}, + {:earmark, "~> 1.4"} ] end diff --git a/mix.lock b/mix.lock index d15925e..574176a 100644 --- a/mix.lock +++ b/mix.lock @@ -9,6 +9,8 @@ "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, "db_connection": {:hex, :db_connection, "2.4.3", "3b9aac9f27347ec65b271847e6baeb4443d8474289bd18c1d6f4de655b70c94d", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c127c15b0fa6cfb32eed07465e05da6c815b032508d4ed7c116122871df73c12"}, "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, + "earmark": {:hex, :earmark, "1.4.37", "56ce845c543393aa3f9b294c818c3d783452a4a67e4ab18c4303a954a8b59363", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "d86d5e12868db86d5321b00e62a4bbcb4150346e4acc9a90a041fb188a5cb106"}, + "earmark_parser": {:hex, :earmark_parser, "1.4.31", "a93921cdc6b9b869f519213d5bc79d9e218ba768d7270d46fdcf1c01bacff9e2", [:mix], [], "hexpm", "317d367ee0335ef037a87e46c91a2269fef6306413f731e8ec11fc45a7efd059"}, "ecto": {:hex, :ecto, "3.9.4", "3ee68e25dbe0c36f980f1ba5dd41ee0d3eb0873bccae8aeaf1a2647242bffa35", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "de5f988c142a3aa4ec18b85a4ec34a2390b65b24f02385c1144252ff6ff8ee75"}, "ecto_sql": {:hex, :ecto_sql, "3.9.2", "34227501abe92dba10d9c3495ab6770e75e79b836d114c41108a4bf2ce200ad5", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1eb5eeb4358fdbcd42eac11c1fbd87e3affd7904e639d77903c1358b2abd3f70"}, "elixir_make": {:hex, :elixir_make, "0.7.5", "784cc00f5fa24239067cc04d449437dcc5f59353c44eb08f188b2b146568738a", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "c3d63e8d5c92fa3880d89ecd41de59473fa2e83eeb68148155e25e8b95aa2887"},