@@ -303,7 +303,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
||||
draft = current_draft(socket.assigns, post, metadata, active_language)
|
||||
normalized = normalize_list_entry(value)
|
||||
|
||||
if normalized in [nil, ""] do
|
||||
if normalized == "" do
|
||||
socket
|
||||
else
|
||||
ensure_list_value(post.project_id, kind, normalized)
|
||||
@@ -414,8 +414,8 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
||||
|
||||
def build(_assigns), do: nil
|
||||
|
||||
def normalize_mode(mode) when mode in [:visual, :markdown, :preview], do: mode
|
||||
def normalize_mode("visual"), do: :visual
|
||||
def normalize_mode(mode) when mode in [:markdown, :preview], do: mode
|
||||
def normalize_mode("visual"), do: :markdown
|
||||
def normalize_mode("preview"), do: :preview
|
||||
def normalize_mode(_mode), do: :markdown
|
||||
|
||||
@@ -507,7 +507,6 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
||||
def post_editor_save_state_label(:discarded), do: translated("Reverted")
|
||||
def post_editor_save_state_label(_state), do: translated("Idle")
|
||||
|
||||
def post_editor_mode_label(:visual), do: translated("Visual")
|
||||
def post_editor_mode_label(:markdown), do: translated("Markdown")
|
||||
def post_editor_mode_label(:preview), do: translated("Preview")
|
||||
|
||||
@@ -687,7 +686,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
||||
%{
|
||||
"title" => post.title || "",
|
||||
"excerpt" => post.excerpt || "",
|
||||
"content" => post.content || "",
|
||||
"content" => Posts.editor_body(post),
|
||||
"tags" => Enum.join(post.tags || [], ", "),
|
||||
"categories" => Enum.join(post.categories || [], ", "),
|
||||
"author" => post.author || metadata.default_author || "",
|
||||
@@ -699,7 +698,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
||||
%{
|
||||
"title" => translation && translation.title || "",
|
||||
"excerpt" => translation && translation.excerpt || "",
|
||||
"content" => translation && translation.content || "",
|
||||
"content" => if(translation, do: Posts.editor_body(translation), else: ""),
|
||||
"tags" => Enum.join(post.tags || [], ", "),
|
||||
"categories" => Enum.join(post.categories || [], ", "),
|
||||
"author" => post.author || metadata.default_author || "",
|
||||
|
||||
@@ -327,7 +327,7 @@
|
||||
|
||||
<div class="editor-toolbar-center">
|
||||
<div class="editor-mode-toggle">
|
||||
<%= for mode <- [:visual, :markdown, :preview] do %>
|
||||
<%= for mode <- [:markdown, :preview] do %>
|
||||
<button
|
||||
class={if(@post_editor.mode == mode, do: "active")}
|
||||
type="button"
|
||||
@@ -386,7 +386,10 @@
|
||||
<% end %>
|
||||
</div>
|
||||
<% else %>
|
||||
<textarea id={"post-editor-content-#{@post_editor.id}"} class="post-editor-textarea post-editor-content" data-testid="post-editor-content" phx-hook="PostEditorContent" data-post-editor-id={@post_editor.id} name="post_editor[content]" rows="18"><%= @post_editor.form["content"] %></textarea>
|
||||
<div id={"post-editor-markdown-surface-#{@post_editor.id}"} class="post-editor-markdown-surface" data-testid="post-editor-markdown-surface" phx-hook="PostEditorContent" data-post-editor-id={@post_editor.id}>
|
||||
<pre class="post-editor-markdown-highlight" aria-hidden="true"></pre>
|
||||
<textarea id={"post-editor-content-#{@post_editor.id}"} class="post-editor-textarea post-editor-content" data-testid="post-editor-content" data-post-editor-id={@post_editor.id} name="post_editor[content]" rows="18"><%= @post_editor.form["content"] %></textarea>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -215,6 +215,30 @@ defmodule BDS.Posts do
|
||||
end
|
||||
end
|
||||
|
||||
def editor_body(%Post{content: content}) when is_binary(content), do: content
|
||||
|
||||
def editor_body(%Post{project_id: project_id, file_path: file_path})
|
||||
when is_binary(file_path) and file_path != "" do
|
||||
project_id
|
||||
|> Projects.get_project!()
|
||||
|> Projects.project_data_dir()
|
||||
|> Path.join(file_path)
|
||||
|> read_markdown_body()
|
||||
end
|
||||
|
||||
def editor_body(%Translation{content: content}) when is_binary(content), do: content
|
||||
|
||||
def editor_body(%Translation{project_id: project_id, file_path: file_path})
|
||||
when is_binary(file_path) and file_path != "" do
|
||||
project_id
|
||||
|> Projects.get_project!()
|
||||
|> Projects.project_data_dir()
|
||||
|> Path.join(file_path)
|
||||
|> read_markdown_body()
|
||||
end
|
||||
|
||||
def editor_body(_record), do: ""
|
||||
|
||||
def delete_post(post_id) do
|
||||
case Repo.get(Post, post_id) do
|
||||
nil ->
|
||||
@@ -664,8 +688,10 @@ defmodule BDS.Posts do
|
||||
defp published_post_body(%Post{content: content}, _full_path) when is_binary(content),
|
||||
do: content
|
||||
|
||||
defp published_post_body(_post, full_path) do
|
||||
case File.read(full_path) do
|
||||
defp published_post_body(_post, full_path), do: read_markdown_body(full_path)
|
||||
|
||||
defp read_markdown_body(path) do
|
||||
case File.read(path) do
|
||||
{:ok, contents} ->
|
||||
case String.split(contents, "\n---\n", parts: 2) do
|
||||
[_frontmatter, body] -> String.trim_trailing(body, "\n")
|
||||
|
||||
Reference in New Issue
Block a user