diff --git a/lib/bds/posts.ex b/lib/bds/posts.ex index 7cbfd98..629c34f 100644 --- a/lib/bds/posts.ex +++ b/lib/bds/posts.ex @@ -78,9 +78,10 @@ defmodule BDS.Posts do relative_path = build_post_relative_path(post.slug, post.created_at) full_path = Path.join(Projects.project_data_dir(project), relative_path) updated_at = System.system_time(:second) + body = publishable_post_body(post, full_path, project) :ok = File.mkdir_p(Path.dirname(full_path)) - :ok = File.write(full_path, serialize_post_file(%{post | updated_at: updated_at}, published_at)) + :ok = File.write(full_path, serialize_post_file(%{post | updated_at: updated_at, content: body}, published_at)) post |> Post.changeset(%{ @@ -94,6 +95,19 @@ defmodule BDS.Posts do end end + def rebuild_posts_from_files(project_id) do + project = Projects.get_project!(project_id) + + posts = + project + |> Projects.project_data_dir() + |> Path.join("posts") + |> list_matching_files("*.md") + |> Enum.map(&upsert_post_from_file(project_id, project, &1)) + + {:ok, posts} + end + def delete_post(post_id) do case Repo.get(Post, post_id) do nil -> @@ -244,6 +258,19 @@ defmodule BDS.Posts do Path.join(["posts", year, month, "#{slug}.md"]) end + defp publishable_post_body(%Post{content: content}, _full_path, _project) when is_binary(content), do: content + + defp publishable_post_body(%Post{file_path: file_path} = post, full_path, project) do + source_path = + if file_path in [nil, ""] do + full_path + else + Path.join(Projects.project_data_dir(project), file_path) + end + + published_post_body(post, source_path) + end + defp serialize_post_file(post, published_at) do Frontmatter.serialize_document( [ @@ -281,6 +308,58 @@ defmodule BDS.Posts do end end + defp upsert_post_from_file(project_id, project, path) do + contents = File.read!(path) + {:ok, %{fields: fields}} = Frontmatter.parse_document(contents) + relative_path = Path.relative_to(path, Projects.project_data_dir(project)) + now = System.system_time(:second) + + attrs = %{ + id: Map.get(fields, "id") || Ecto.UUID.generate(), + project_id: project_id, + title: Map.get(fields, "title") || "", + slug: Map.fetch!(fields, "slug"), + excerpt: Map.get(fields, "excerpt"), + content: nil, + status: parse_post_status(Map.get(fields, "status", "published")), + author: Map.get(fields, "author"), + created_at: Map.get(fields, "created_at", now), + updated_at: Map.get(fields, "updated_at", now), + published_at: Map.get(fields, "published_at"), + file_path: relative_path, + checksum: nil, + tags: Map.get(fields, "tags", []), + categories: Map.get(fields, "categories", []), + template_slug: Map.get(fields, "template_slug"), + language: Map.get(fields, "language"), + do_not_translate: Map.get(fields, "do_not_translate", false), + published_title: nil, + published_content: nil, + published_tags: nil, + published_categories: nil, + published_excerpt: nil + } + + post = Repo.get_by(Post, project_id: project_id, slug: attrs.slug) || %Post{} + + post + |> Post.changeset(attrs) + |> Repo.insert_or_update!() + end + + defp parse_post_status(status) when is_atom(status), do: status + defp parse_post_status(status), do: String.to_existing_atom(status) + + defp list_matching_files(dir, pattern) do + if File.dir?(dir) do + Path.join([dir, "**", pattern]) + |> Path.wildcard() + |> Enum.sort() + else + [] + end + end + defp delete_post_file(%Post{project_id: _project_id, file_path: file_path}) when file_path in [nil, ""], do: :ok defp delete_post_file(%Post{} = post) do diff --git a/test/bds/posts_test.exs b/test/bds/posts_test.exs index 6a223d3..f35ca5e 100644 --- a/test/bds/posts_test.exs +++ b/test/bds/posts_test.exs @@ -190,6 +190,95 @@ defmodule BDS.PostsTest do assert archived_published.published_at == published_post.published_at end + test "publish_post republishes archived posts without losing the existing body or original published_at" do + temp_dir = Path.join(System.tmp_dir!(), "bds-post-republish-#{System.unique_integer([:positive])}") + File.mkdir_p!(temp_dir) + on_exit(fn -> File.rm_rf(temp_dir) end) + + assert {:ok, project} = BDS.Projects.create_project(%{name: "Republish", data_path: temp_dir}) + + assert {:ok, post} = + BDS.Posts.create_post(%{ + project_id: project.id, + title: "Republish Me", + content: "Body" + }) + + assert {:ok, published} = BDS.Posts.publish_post(post.id) + assert {:ok, archived} = BDS.Posts.archive_post(published.id) + assert archived.status == :archived + assert archived.content == nil + + assert {:ok, republished} = BDS.Posts.publish_post(archived.id) + assert republished.status == :published + assert republished.published_at == published.published_at + + contents = File.read!(Path.join(temp_dir, republished.file_path)) + assert contents =~ "\n---\nBody\n" + end + + test "rebuild_posts_from_files recreates published posts from disk" do + temp_dir = Path.join(System.tmp_dir!(), "bds-post-rebuild-#{System.unique_integer([:positive])}") + File.mkdir_p!(temp_dir) + on_exit(fn -> File.rm_rf(temp_dir) end) + + assert {:ok, project} = BDS.Projects.create_project(%{name: "Rebuild", data_path: temp_dir}) + + posts_dir = Path.join([BDS.Projects.project_data_dir(project), "posts", "2026", "04"]) + File.mkdir_p!(posts_dir) + + file_path = Path.join(posts_dir, "recovered-post.md") + + File.write!( + file_path, + [ + "---", + "id: post-from-file", + "title: Recovered Post", + "slug: recovered-post", + "excerpt: Summary", + "status: published", + "author: Writer", + "language: en", + "do_not_translate: true", + "template_slug: article", + "created_at: 1711843200", + "updated_at: 1711929600", + "published_at: 1712016000", + "tags:", + " - alpha", + "categories:", + " - notes", + "---", + "Restored body", + "" + ] + |> Enum.join("\n") + ) + + assert {:ok, posts} = BDS.Posts.rebuild_posts_from_files(project.id) + assert length(posts) == 1 + + [post] = posts + assert post.id == "post-from-file" + assert post.project_id == project.id + assert post.title == "Recovered Post" + assert post.slug == "recovered-post" + assert post.excerpt == "Summary" + assert post.status == :published + assert post.author == "Writer" + assert post.language == "en" + assert post.do_not_translate == true + assert post.template_slug == "article" + assert post.created_at == 1711843200 + assert post.updated_at == 1711929600 + assert post.published_at == 1712016000 + assert post.tags == ["alpha"] + assert post.categories == ["notes"] + assert post.file_path == "posts/2026/04/recovered-post.md" + assert post.content == nil + end + defp errors_on(changeset) do Ecto.Changeset.traverse_errors(changeset, fn {message, opts} -> Regex.replace(~r"%{(\w+)}", message, fn _, key ->