@@ -52,7 +52,7 @@ defmodule ExDoc.Autolink do
5252 :language ,
5353 file: "nofile" ,
5454 apps: [ ] ,
55- extras: [ ] ,
55+ extras: % { } ,
5656 deps: [ ] ,
5757 ext: ".html" ,
5858 current_kfa: nil ,
@@ -217,7 +217,7 @@ defmodule ExDoc.Autolink do
217217 with % { scheme: nil , host: nil , path: path } = uri <- URI . parse ( link ) ,
218218 true <- is_binary ( path ) and path != "" and not ( path =~ ref_regex ( ) ) ,
219219 true <- Path . extname ( path ) in @ builtin_ext do
220- if file = config . extras [ Path . basename ( path ) ] do
220+ if file = resolve_extra_path ( path , config ) do
221221 append_fragment ( file <> config . ext , uri . fragment )
222222 else
223223 maybe_warn ( config , nil , nil , % { file_path: path , original_text: link } )
@@ -228,6 +228,47 @@ defmodule ExDoc.Autolink do
228228 end
229229 end
230230
231+ defp resolve_extra_path ( path , config ) do
232+ has_directory? = path != Path . basename ( path )
233+
234+ exact_match = maybe_resolve_extra_source_path ( path , config ) |> then ( & config . extras [ & 1 ] )
235+
236+ cond do
237+ exact_match ->
238+ exact_match
239+
240+ has_directory? ->
241+ nil
242+
243+ true ->
244+ config . extras [ Path . basename ( path ) ]
245+ end
246+ end
247+
248+ defp maybe_resolve_extra_source_path ( path , config ) do
249+ cond do
250+ String . starts_with? ( path , "/" ) ->
251+ normalize_extra_source_path ( String . trim_leading ( path , "/" ) , File . cwd! ( ) )
252+
253+ true ->
254+ normalize_extra_source_path ( path , base_dir_for_extra_resolution ( config . file ) )
255+ end
256+ end
257+
258+ defp normalize_extra_source_path ( path , base_dir ) do
259+ path
260+ |> Path . expand ( base_dir )
261+ |> Path . relative_to ( File . cwd! ( ) )
262+ end
263+
264+ defp base_dir_for_extra_resolution ( file ) when is_binary ( file ) do
265+ file
266+ |> Path . expand ( File . cwd! ( ) )
267+ |> Path . dirname ( )
268+ end
269+
270+ defp base_dir_for_extra_resolution ( _ ) , do: File . cwd! ( )
271+
231272 defp maybe_remove_link ( nil , :custom_link ) do
232273 :remove_link
233274 end
0 commit comments