add note export as raw text

This commit is contained in:
Inhji 2023-03-31 22:20:32 +02:00
parent c1843499e5
commit 58550fe4f3
6 changed files with 33 additions and 2 deletions

View file

@ -64,6 +64,17 @@ defmodule ChiyaWeb.NoteController do
|> redirect(to: ~p"/admin/notes")
end
def raw(conn, %{"id" => id}) do
raw_note =
id
|> Notes.get_note!()
|> Notes.preload_note()
|> ChiyaWeb.Exim.export_note()
conn
|> text(raw_note)
end
defp from_channel_ids(note_params) do
selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1)

12
lib/chiya_web/exim.ex Normal file
View file

@ -0,0 +1,12 @@
defmodule ChiyaWeb.Exim do
alias Chiya.Notes.Note
defp frontmatter(%Note{name: title, channels: channels}) do
channels_raw = Enum.map_join(channels, "], [", fn c -> "\"#{c.name}\"" end)
"title: \"#{title}\"\ncategories: [#{channels_raw}]"
end
def export_note(%Note{content: content} = note) do
"---\n#{frontmatter(note)}\n---\n#{content}"
end
end

View file

@ -17,7 +17,10 @@ defmodule ChiyaWeb.NoteShowLive do
<.button>Edit note</.button>
</.link>
<.link href={~p"/n/#{@note.slug}"}>
<.button>Preview note</.button>
<.button>Preview</.button>
</.link>
<.link href={~p"/admin/notes/#{@note}/raw"}>
<.button>Raw</.button>
</.link>
</:actions>
</.header>

View file

@ -62,6 +62,7 @@ defmodule ChiyaWeb.Router do
resources "/identities", IdentityController
live "/notes/:id", NoteShowLive, :show
get "/notes/:id/raw", NoteController, :raw
end
## Authentication routes

View file

@ -53,7 +53,8 @@ defmodule Chiya.MixProject do
{:oban, "~> 2.14"},
{:waffle, "~> 1.1"},
{:waffle_ecto, "~> 0.0.12"},
{:earmark, "~> 1.4"}
{:earmark, "~> 1.4"},
{:yaml_front_matter, "~> 1.0.0"}
]
end

View file

@ -56,4 +56,7 @@
"waffle_ecto": {:hex, :waffle_ecto, "0.0.12", "e5c17c49b071b903df71861c642093281123142dc4e9908c930db3e06795b040", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:waffle, "~> 1.0", [hex: :waffle, repo: "hexpm", optional: false]}], "hexpm", "585fe6371057066d2e8e3383ddd7a2437ff0668caf3f4cbf5a041e0de9837168"},
"websock": {:hex, :websock, "0.4.3", "184ac396bdcd3dfceb5b74c17d221af659dd559a95b1b92041ecb51c9b728093", [:mix], [], "hexpm", "5e4dd85f305f43fd3d3e25d70bec4a45228dfed60f0f3b072d8eddff335539cf"},
"websock_adapter": {:hex, :websock_adapter, "0.4.5", "30038a3715067f51a9580562c05a3a8d501126030336ffc6edb53bf57d6d2d26", [:mix], [{:bandit, "~> 0.6", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.4", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "1d9812dc7e703c205049426fd4fe0852a247a825f91b099e53dc96f68bafe4c8"},
"yamerl": {:hex, :yamerl, "0.10.0", "4ff81fee2f1f6a46f1700c0d880b24d193ddb74bd14ef42cb0bcf46e81ef2f8e", [:rebar3], [], "hexpm", "346adb2963f1051dc837a2364e4acf6eb7d80097c0f53cbdc3046ec8ec4b4e6e"},
"yaml_elixir": {:hex, :yaml_elixir, "2.9.0", "9a256da867b37b8d2c1ffd5d9de373a4fda77a32a45b452f1708508ba7bbcb53", [:mix], [{:yamerl, "~> 0.10", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "0cb0e7d4c56f5e99a6253ed1a670ed0e39c13fc45a6da054033928607ac08dfc"},
"yaml_front_matter": {:hex, :yaml_front_matter, "1.0.0", "9f3310906a9647d04fc4715b6bac5ed177defc9625c53b93430d36d8fd0961db", [:mix], [{:yaml_elixir, "~> 2.0", [hex: :yaml_elixir, repo: "hexpm", optional: false]}], "hexpm", "73cf825c18b7a14bfd8fb5856fa4887196035be69d2d45d1c370917aae0572ec"},
}