@@ -230,7 +230,7 @@ defmodule ExDoc.Formatter.HTML do
230230 end
231231
232232 defp copy_extras ( config , extras ) do
233- for % { source_path: source_path , id: id } <- extras ,
233+ for % { source_path: source_path , id: id } when source_path != nil <- extras ,
234234 ext = extension_name ( source_path ) ,
235235 ext == ".livemd" do
236236 output = "#{ config . output } /#{ id } #{ ext } "
@@ -293,7 +293,16 @@ defmodule ExDoc.Formatter.HTML do
293293
294294 defp build_api_reference ( nodes_map , config ) do
295295 api_reference = Templates . api_reference_template ( config , nodes_map )
296- % { id: "api-reference" , title: "API Reference" , group: "" , content: api_reference }
296+
297+ % {
298+ id: "api-reference" ,
299+ title: "API Reference" ,
300+ group: "" ,
301+ title_content: "API Reference" ,
302+ content: api_reference ,
303+ source_path: nil ,
304+ source_url: nil
305+ }
297306 end
298307
299308 @ doc """
@@ -346,14 +355,40 @@ defmodule ExDoc.Formatter.HTML do
346355 "file extension not recognized, allowed extension is either .md, .txt or no extension"
347356 end
348357
358+ { title_ast , ast } =
359+ case ExDoc.DocAST . extract_title ( ast ) do
360+ { :ok , title_ast , ast } -> { title_ast , ast }
361+ :error -> { nil , ast }
362+ end
363+
364+ title_text = title_ast && ExDoc.DocAST . text_from_ast ( title_ast )
365+ title_html = title_ast && ExDoc.DocAST . to_string ( title_ast )
366+
349367 # TODO: don't hardcode Elixir for extras?
350368 language = ExDoc.Language.Elixir
351- html_content = autolink_and_render ( ast , language , autolink_opts , opts )
369+ content_html = autolink_and_render ( ast , language , autolink_opts , opts )
352370
353371 group = GroupMatcher . match_extra ( groups , input )
354- title = title || extract_title ( html_content ) || filename_to_title ( input )
372+ title = title || title_text || filename_to_title ( input )
373+
374+ source_path = Path . relative_to ( input , "." )
375+
376+ source_url =
377+ if url = config . source_url_pattern do
378+ url
379+ |> String . replace ( "%{path}" , source_path )
380+ |> String . replace ( "%{line}" , "1" )
381+ end
355382
356- % { id: id , title: title , group: group , content: html_content , source_path: input }
383+ % {
384+ id: id ,
385+ title: title ,
386+ group: group ,
387+ title_content: title_html ,
388+ content: content_html ,
389+ source_path: source_path ,
390+ source_url: source_url
391+ }
357392 end
358393
359394 defp extension_name ( input ) do
@@ -362,20 +397,6 @@ defmodule ExDoc.Formatter.HTML do
362397 |> String . downcase ( )
363398 end
364399
365- @ tag_regex ~r/ <[^>]*>/ m
366- defp strip_html ( header ) do
367- Regex . replace ( @ tag_regex , header , "" )
368- end
369-
370- @ h1_regex ~r/ <h1.*?>(.+?)<\/ h1>/ m
371- defp extract_title ( content ) do
372- title = Regex . run ( @ h1_regex , content , capture: :all_but_first )
373-
374- if title do
375- title |> List . first ( ) |> strip_html ( ) |> String . trim ( )
376- end
377- end
378-
379400 @ doc """
380401 Convert the input file name into a title
381402 """
0 commit comments