-
Notifications
You must be signed in to change notification settings - Fork 351
feat: add Markdown formatter #2148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yordis
wants to merge
1
commit into
elixir-lang:main
Choose a base branch
from
yordis:yordis/add-markdown-formatter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
defmodule ExDoc.Formatter.MARKDOWN do | ||
@moduledoc false | ||
|
||
alias __MODULE__.{Templates} | ||
alias ExDoc.Formatter | ||
alias ExDoc.Utils | ||
|
||
@doc """ | ||
Generates Markdown documentation for the given modules. | ||
""" | ||
@spec run([ExDoc.ModuleNode.t()], [ExDoc.ModuleNode.t()], ExDoc.Config.t()) :: String.t() | ||
def run(project_nodes, filtered_modules, config) when is_map(config) do | ||
Utils.unset_warned() | ||
|
||
config = normalize_config(config) | ||
File.rm_rf!(config.output) | ||
File.mkdir_p!(config.output) | ||
|
||
extras = Formatter.build_extras(config, ".md") | ||
|
||
project_nodes = | ||
project_nodes | ||
|> Formatter.render_all(filtered_modules, ".md", config, highlight_tag: "samp") | ||
|
||
nodes_map = %{ | ||
modules: Formatter.filter_list(:module, project_nodes), | ||
tasks: Formatter.filter_list(:task, project_nodes) | ||
} | ||
|
||
config = %{config | extras: extras} | ||
|
||
generate_nav(config, nodes_map) | ||
generate_extras(config) | ||
generate_list(config, nodes_map.modules) | ||
generate_list(config, nodes_map.tasks) | ||
generate_llm_index(config, nodes_map) | ||
|
||
config.output |> Path.join("index.md") |> Path.relative_to_cwd() | ||
end | ||
|
||
defp normalize_config(config) do | ||
output = | ||
config.output | ||
|> Path.expand() | ||
|> Path.join("markdown") | ||
|
||
%{config | output: output} | ||
end | ||
|
||
defp normalize_output(output) do | ||
output | ||
|> String.replace(~r/\r\n|\r|\n/, "\n") | ||
|> String.replace(~r/\n{3,}/, "\n\n") | ||
end | ||
|
||
defp generate_nav(config, nodes) do | ||
nodes = | ||
Map.update!(nodes, :modules, fn modules -> | ||
modules |> Enum.chunk_by(& &1.group) |> Enum.map(&{hd(&1).group, &1}) | ||
end) | ||
|
||
content = | ||
Templates.nav_template(config, nodes) | ||
|> normalize_output() | ||
|
||
File.write("#{config.output}/index.md", content) | ||
end | ||
|
||
defp generate_extras(config) do | ||
for {_title, extras} <- config.extras do | ||
Enum.each(extras, fn %{id: id, source: content} -> | ||
output = "#{config.output}/#{id}.md" | ||
|
||
if File.regular?(output) do | ||
Utils.warn("file #{Path.relative_to_cwd(output)} already exists", []) | ||
end | ||
|
||
File.write!(output, normalize_output(content)) | ||
end) | ||
end | ||
end | ||
|
||
defp generate_list(config, nodes) do | ||
nodes | ||
|> Task.async_stream(&generate_module_page(&1, config), timeout: :infinity) | ||
|> Enum.map(&elem(&1, 1)) | ||
end | ||
|
||
## Helpers | ||
|
||
defp generate_module_page(module_node, config) do | ||
content = | ||
Templates.module_page(config, module_node) | ||
|> normalize_output() | ||
|
||
File.write("#{config.output}/#{module_node.id}.md", content) | ||
end | ||
|
||
defp generate_llm_index(config, nodes_map) do | ||
content = generate_llm_index_content(config, nodes_map) | ||
File.write("#{config.output}/llms.txt", content) | ||
end | ||
|
||
defp generate_llm_index_content(config, nodes_map) do | ||
project_info = """ | ||
# #{config.project} #{config.version} | ||
|
||
#{config.project} documentation index for Large Language Models. | ||
|
||
## Modules | ||
|
||
""" | ||
|
||
modules_info = | ||
nodes_map.modules | ||
|> Enum.map(fn module_node -> | ||
"- **#{module_node.title}** (#{module_node.id}.md): #{module_node.doc |> extract_summary()}" | ||
end) | ||
|> Enum.join("\n") | ||
|
||
tasks_info = | ||
if length(nodes_map.tasks) > 0 do | ||
tasks_list = | ||
nodes_map.tasks | ||
|> Enum.map(fn task_node -> | ||
"- **#{task_node.title}** (#{task_node.id}.md): #{task_node.doc |> extract_summary()}" | ||
end) | ||
|> Enum.join("\n") | ||
|
||
"\n\n## Mix Tasks\n\n" <> tasks_list | ||
else | ||
"" | ||
end | ||
|
||
extras_info = | ||
if is_list(config.extras) and length(config.extras) > 0 do | ||
extras_list = | ||
config.extras | ||
|> Enum.flat_map(fn | ||
{_group, extras} when is_list(extras) -> extras | ||
_ -> [] | ||
end) | ||
|> Enum.map(fn extra -> | ||
"- **#{extra.title}** (#{extra.id}.md): #{extra.title}" | ||
end) | ||
|> Enum.join("\n") | ||
|
||
if extras_list == "" do | ||
"" | ||
else | ||
"\n\n## Guides\n\n" <> extras_list | ||
end | ||
else | ||
"" | ||
end | ||
|
||
project_info <> modules_info <> tasks_info <> extras_info | ||
end | ||
|
||
defp extract_summary(nil), do: "No documentation available" | ||
defp extract_summary(""), do: "No documentation available" | ||
|
||
defp extract_summary(doc) when is_binary(doc) do | ||
doc | ||
|> String.split("\n") | ||
|> Enum.find("", fn line -> String.trim(line) != "" end) | ||
|> String.trim() | ||
|> case do | ||
"" -> | ||
"No documentation available" | ||
|
||
summary -> | ||
summary | ||
|> String.slice(0, 150) | ||
|> then(fn s -> if String.length(s) == 150, do: s <> "...", else: s end) | ||
end | ||
end | ||
|
||
defp extract_summary(doc_ast) when is_list(doc_ast) do | ||
# For DocAST (which is a list), extract the first text node | ||
extract_first_text_from_ast(doc_ast) | ||
end | ||
|
||
defp extract_summary(_), do: "No documentation available" | ||
|
||
defp extract_first_text_from_ast([]), do: "No documentation available" | ||
|
||
defp extract_first_text_from_ast([{:p, _, content} | _rest]) do | ||
extract_text_from_content(content) | ||
|> String.slice(0, 150) | ||
|> then(fn s -> if String.length(s) == 150, do: s <> "...", else: s end) | ||
end | ||
|
||
defp extract_first_text_from_ast([_node | rest]) do | ||
extract_first_text_from_ast(rest) | ||
end | ||
|
||
defp extract_text_from_content([]), do: "" | ||
defp extract_text_from_content([text | _rest]) when is_binary(text), do: text | ||
|
||
defp extract_text_from_content([{_tag, _attrs, content} | rest]) do | ||
case extract_text_from_content(content) do | ||
"" -> extract_text_from_content(rest) | ||
text -> text | ||
end | ||
end | ||
|
||
defp extract_text_from_content([_node | rest]) do | ||
extract_text_from_content(rest) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add the lang here: