Skip to content

Commit 58d1a95

Browse files
committed
init docs
1 parent b6bdce8 commit 58d1a95

19 files changed

+788
-1
lines changed

lib/algora/content.ex

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Algora.Content do
55

66
alias Algora.Markdown
77

8-
defstruct [:slug, :title, :date, :tags, :authors, :content]
8+
defstruct [:slug, :title, :date, :tags, :authors, :content, :path]
99

1010
defp base_path, do: Path.join([:code.priv_dir(:algora), "content"])
1111

@@ -38,6 +38,57 @@ defmodule Algora.Content do
3838
|> Enum.sort_by(& &1.date, :desc)
3939
end
4040

41+
def list_content_rec(directory) do
42+
list_content_rec_helper([base_path(), directory], directory)
43+
end
44+
45+
defp list_content_rec_helper(path, root_dir) do
46+
case File.ls(Path.join(path)) do
47+
{:ok, entries} ->
48+
entries
49+
|> Enum.reduce(%{files: [], dirs: %{}}, fn entry, acc ->
50+
full_path = Path.join(path ++ [entry])
51+
52+
cond do
53+
File.dir?(full_path) ->
54+
nested_content = list_content_rec_helper(path ++ [entry], root_dir)
55+
put_in(acc, [:dirs, entry], nested_content)
56+
57+
String.ends_with?(entry, ".md") ->
58+
# Get the path relative to base_path
59+
relative_path =
60+
full_path
61+
|> Path.relative_to(base_path())
62+
|> Path.rootname(".md")
63+
64+
path_segments =
65+
relative_path
66+
|> Path.split()
67+
|> Enum.drop(1)
68+
69+
directory = Path.dirname(relative_path)
70+
slug = Path.basename(relative_path)
71+
72+
case load_content(directory, slug) do
73+
{:ok, content} ->
74+
content_with_path = Map.put(content, :path, path_segments)
75+
Map.update!(acc, :files, &[content_with_path | &1])
76+
77+
_ ->
78+
acc
79+
end
80+
81+
true ->
82+
acc
83+
end
84+
end)
85+
|> Map.update!(:files, &Enum.sort_by(&1, fn file -> file.date end, :desc))
86+
87+
{:error, _} ->
88+
%{files: [], dirs: %{}}
89+
end
90+
end
91+
4192
def format_date(date_string) when is_binary(date_string) do
4293
case Date.from_iso8601(date_string) do
4394
{:ok, date} -> Calendar.strftime(date, "%B %d, %Y")

0 commit comments

Comments
 (0)