Skip to content

Commit 84aca2b

Browse files
committed
Refactor markdown formatter to improve output setup and file generation logic. Introduced a build file to manage markdown file cleanup and generation, ensuring only relevant files are removed. Updated file writing methods for consistency and clarity.
1 parent cbc1367 commit 84aca2b

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

lib/ex_doc/formatter/markdown.ex

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ defmodule ExDoc.Formatter.Markdown do
1313
Utils.unset_warned()
1414

1515
config = normalize_config(config)
16-
File.rm_rf!(config.output)
17-
File.mkdir_p!(config.output)
16+
build = Path.join(config.output, ".build-markdown")
17+
output_setup(build, config)
1818

1919
extras = Formatter.build_extras(config, ".md")
2020

@@ -29,12 +29,14 @@ defmodule ExDoc.Formatter.Markdown do
2929

3030
config = %{config | extras: extras}
3131

32-
generate_nav(config, nodes_map)
33-
generate_extras(config)
34-
generate_list(config, nodes_map.modules)
35-
generate_list(config, nodes_map.tasks)
36-
generate_llm_index(config, nodes_map)
32+
all_files =
33+
[generate_nav(config, nodes_map)] ++
34+
generate_extras(config) ++
35+
generate_list(config, nodes_map.modules) ++
36+
generate_list(config, nodes_map.tasks) ++
37+
[generate_llm_index(config, nodes_map)]
3738

39+
generate_build(List.flatten(all_files), build)
3840
config.output |> Path.join("index.md") |> Path.relative_to_cwd()
3941
end
4042

@@ -43,6 +45,40 @@ defmodule ExDoc.Formatter.Markdown do
4345
%{config | output: output}
4446
end
4547

48+
defp output_setup(build, config) do
49+
if File.exists?(build) do
50+
build
51+
|> File.read!()
52+
|> String.split("\n", trim: true)
53+
|> Enum.map(&Path.join(config.output, &1))
54+
|> Enum.each(&File.rm/1)
55+
56+
File.rm(build)
57+
else
58+
# Only remove markdown files, not HTML/EPUB files
59+
File.mkdir_p!(config.output)
60+
if File.exists?(config.output) do
61+
config.output
62+
|> Path.join("*.md")
63+
|> Path.wildcard()
64+
|> Enum.each(&File.rm/1)
65+
66+
llms_file = Path.join(config.output, "llms.txt")
67+
if File.exists?(llms_file), do: File.rm(llms_file)
68+
end
69+
end
70+
end
71+
72+
defp generate_build(files, build) do
73+
entries =
74+
files
75+
|> Enum.uniq()
76+
|> Enum.sort()
77+
|> Enum.map(&[&1, "\n"])
78+
79+
File.write!(build, entries)
80+
end
81+
4682
defp normalize_output(output) do
4783
output
4884
|> String.replace(~r/\r\n?/, "\n")
@@ -59,20 +95,24 @@ defmodule ExDoc.Formatter.Markdown do
5995
Templates.nav_template(config, nodes)
6096
|> normalize_output()
6197

62-
File.write("#{config.output}/index.md", content)
98+
filename = "index.md"
99+
File.write("#{config.output}/#{filename}", content)
100+
filename
63101
end
64102

65103
defp generate_extras(config) do
66-
for {_title, extras} <- config.extras do
67-
Enum.each(extras, fn %{id: id, source: content} ->
68-
output = "#{config.output}/#{id}.md"
69-
70-
if File.regular?(output) do
71-
Utils.warn("file #{Path.relative_to_cwd(output)} already exists", [])
72-
end
104+
for {_title, extras} <- config.extras,
105+
%{id: id, source: content} <- extras,
106+
not is_map_key(%{id: id, source: content}, :url) do
107+
filename = "#{id}.md"
108+
output = "#{config.output}/#{filename}"
109+
110+
if File.regular?(output) do
111+
Utils.warn("file #{Path.relative_to_cwd(output)} already exists", [])
112+
end
73113

74-
File.write!(output, normalize_output(content))
75-
end)
114+
File.write!(output, normalize_output(content))
115+
filename
76116
end
77117
end
78118

@@ -89,12 +129,16 @@ defmodule ExDoc.Formatter.Markdown do
89129
Templates.module_page(config, module_node)
90130
|> normalize_output()
91131

92-
File.write("#{config.output}/#{module_node.id}.md", content)
132+
filename = "#{module_node.id}.md"
133+
File.write("#{config.output}/#{filename}", content)
134+
filename
93135
end
94136

95137
defp generate_llm_index(config, nodes_map) do
96138
content = generate_llm_index_content(config, nodes_map)
97-
File.write("#{config.output}/llms.txt", content)
139+
filename = "llms.txt"
140+
File.write("#{config.output}/#{filename}", content)
141+
filename
98142
end
99143

100144
defp generate_llm_index_content(config, nodes_map) do

0 commit comments

Comments
 (0)