11defmodule ExDoc.Formatter.MARKDOWN do
22 @ moduledoc false
33
4- alias __MODULE__ . { Templates }
4+ alias __MODULE__ . Templates
55 alias ExDoc.Formatter
66 alias ExDoc.Utils
77
@@ -20,8 +20,7 @@ defmodule ExDoc.Formatter.MARKDOWN do
2020 extras = Formatter . build_extras ( config , ".md" )
2121
2222 project_nodes =
23- project_nodes
24- |> Formatter . render_all ( filtered_modules , ".md" , config , highlight_tag: "samp" )
23+ Formatter . render_all ( project_nodes , filtered_modules , ".md" , config , highlight_tag: "samp" )
2524
2625 nodes_map = % {
2726 modules: Formatter . filter_list ( :module , project_nodes ) ,
@@ -30,14 +29,16 @@ defmodule ExDoc.Formatter.MARKDOWN do
3029
3130 config = % { config | extras: extras }
3231
33- all_files =
34- [ generate_nav ( config , nodes_map ) ] ++
35- generate_extras ( config ) ++
36- generate_list ( config , nodes_map . modules ) ++
37- generate_list ( config , nodes_map . tasks ) ++
38- [ generate_llm_index ( config , nodes_map ) ]
32+ [
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 )
38+ ]
39+ |> List . flatten ( )
40+ |> generate_build ( build )
3941
40- generate_build ( List . flatten ( all_files ) , build )
4142 config . output |> Path . join ( "index.md" ) |> Path . relative_to_cwd ( )
4243 end
4344
@@ -74,11 +75,10 @@ defmodule ExDoc.Formatter.MARKDOWN do
7475 defp generate_build ( files , build ) do
7576 entries =
7677 files
77- |> Enum . uniq ( )
7878 |> Enum . sort ( )
79- |> Enum . map ( & [ & 1 , "\n " ] )
79+ |> Enum . dedup ( )
80+ |> Enum . intersperse ( "\n " )
8081
81- File . mkdir_p! ( Path . dirname ( build ) )
8282 File . write! ( build , entries )
8383 end
8484
@@ -95,11 +95,16 @@ defmodule ExDoc.Formatter.MARKDOWN do
9595 end )
9696
9797 content =
98- Templates . nav_template ( config , nodes )
98+ config
99+ |> Templates . nav_template ( nodes )
99100 |> normalize_output ( )
100101
101102 filename = "index.md"
102- File . write ( "#{ config . output } /#{ filename } " , content )
103+
104+ config . output
105+ |> Path . join ( filename )
106+ |> File . write ( content )
107+
103108 filename
104109 end
105110
@@ -108,13 +113,13 @@ defmodule ExDoc.Formatter.MARKDOWN do
108113 % { id: id , source: content } <- extras ,
109114 not is_map_key ( % { id: id , source: content } , :url ) do
110115 filename = "#{ id } .md"
111- output = " #{ config . output } / #{ filename } "
116+ output = Path . join ( config . output , filename )
112117
113- if File . regular? ( output ) do
114- Utils . warn ( "file #{ Path . relative_to_cwd ( output ) } already exists" , [ ] )
115- end
118+ if File . regular? ( output ) ,
119+ do: Utils . warn ( "file #{ Path . relative_to_cwd ( output ) } already exists" , [ ] )
116120
117121 File . write! ( output , normalize_output ( content ) )
122+
118123 filename
119124 end
120125 end
@@ -129,18 +134,27 @@ defmodule ExDoc.Formatter.MARKDOWN do
129134
130135 defp generate_module_page ( module_node , config ) do
131136 content =
132- Templates . module_page ( config , module_node )
137+ config
138+ |> Templates . module_page ( module_node )
133139 |> normalize_output ( )
134140
135141 filename = "#{ module_node . id } .md"
136- File . write ( "#{ config . output } /#{ filename } " , content )
142+
143+ config . output
144+ |> Path . join ( filename )
145+ |> File . write ( content )
146+
137147 filename
138148 end
139149
140150 defp generate_llm_index ( config , nodes_map ) do
141151 content = generate_llm_index_content ( config , nodes_map )
142152 filename = "llms.txt"
143- File . write ( "#{ config . output } /#{ filename } " , content )
153+
154+ config . output
155+ |> Path . join ( filename )
156+ |> File . write ( content )
157+
144158 filename
145159 end
146160
@@ -155,57 +169,64 @@ defmodule ExDoc.Formatter.MARKDOWN do
155169 """
156170
157171 modules_info =
158- nodes_map . modules
159- |> Enum . map ( fn module_node ->
160- "- [#{ module_node . title } ](#{ module_node . id } .md): #{ module_node . doc |> ExDoc.DocAST . synopsis ( ) |> extract_plain_text ( ) } "
172+ Enum . map ( nodes_map . modules , fn module_node ->
173+ synopsis = synopsis ( module_node . doc )
174+
175+ [ "- [#{ module_node . title } ](#{ module_node . id } .md): " , synopsis , "\n " ]
161176 end )
162- |> Enum . join ( "\n " )
163177
164178 tasks_info =
165- if length ( nodes_map . tasks ) > 0 do
179+ if Enum . any? ( nodes_map . tasks ) do
166180 tasks_list =
167- nodes_map . tasks
168- |> Enum . map ( fn task_node ->
169- "- [#{ task_node . title } ](#{ task_node . id } .md): #{ task_node . doc |> ExDoc.DocAST . synopsis ( ) |> extract_plain_text ( ) } "
181+ Enum . map ( nodes_map . tasks , fn task_node ->
182+ synopsis = synopsis ( task_node . doc )
183+
184+ [ "- [#{ task_node . title } ](#{ task_node . id } .md): " , synopsis , "\n " ]
170185 end )
171- |> Enum . join ( "\n " )
172186
173- "\n \n ## Mix Tasks\n \n " <> tasks_list
187+ [ "\n ## Mix Tasks\n \n " | tasks_list ]
174188 else
175- ""
189+ [ ]
176190 end
177191
178192 extras_info =
179- if is_list ( config . extras ) and length ( config . extras ) > 0 do
193+ if is_list ( config . extras ) and Enum . any? ( config . extras ) do
180194 extras_list =
181195 config . extras
182196 |> Enum . flat_map ( fn
183197 { _group , extras } when is_list ( extras ) -> extras
184198 _ -> [ ]
185199 end )
186- |> Enum . map ( fn extra ->
187- "- [#{ extra . title } ](#{ extra . id } .md)"
188- end )
189- |> Enum . join ( "\n " )
200+ |> Enum . map ( & [ "- [#{ & 1 . title } ](#{ & 1 . id } .md)" , "\n " ] )
190201
191- if extras_list == "" do
192- ""
202+ if Enum . any? ( extras_list ) do
203+ [ " \n ## Guides \n \n " | extras_list ]
193204 else
194- " \n \n ## Guides \n \n " <> extras_list
205+ [ ]
195206 end
196207 else
197- ""
208+ [ ]
198209 end
199210
200- project_info <> modules_info <> tasks_info <> extras_info
211+ [ project_info , modules_info , tasks_info , extras_info ]
212+ end
213+
214+ defp synopsis ( doc ) do
215+ doc
216+ |> ExDoc.DocAST . synopsis ( )
217+ |> extract_plain_text ( )
201218 end
202219
220+ defp extract_plain_text ( "" ) , do: "No documentation available"
221+
203222 defp extract_plain_text ( html ) when is_binary ( html ) do
204- html
205- |> String . replace ( ~r/ <[^>]*>/ , "" )
206- |> String . replace ( ~r/ \s +/ , " " )
207- |> String . trim ( )
208- |> case do
223+ html =
224+ html
225+ |> String . replace ( ~r/ <[^>]*>/ , "" )
226+ |> String . replace ( ~r/ \s +/ , " " )
227+ |> String . trim ( )
228+
229+ case html do
209230 "" ->
210231 "No documentation available"
211232
@@ -217,6 +238,4 @@ defmodule ExDoc.Formatter.MARKDOWN do
217238 end
218239 end
219240 end
220-
221- defp extract_plain_text ( _ ) , do: "No documentation available"
222241end
0 commit comments