-
Notifications
You must be signed in to change notification settings - Fork 352
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
Open
yordis
wants to merge
33
commits 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.
Open
Changes from 8 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
c6e237a
feat: add Markdown formatter
yordis 7297cd3
fix
yordis 1e5c09f
refactor: rename to_markdown_string to to_markdown and update related…
yordis b4a4ba5
refactor: simplify to_markdown function by removing post-processing a…
yordis 9770ce7
feat: implement node_synopsis function to extract and format document…
yordis 97cdfa0
style: update comments for clarity and improve code readability in do…
yordis c1970a7
style: add period to documentation comments for consistency in EPUB a…
yordis abe0e26
style: add period to documentation comment for module_page function i…
yordis 68d0b02
Update lib/ex_doc/formatter/markdown/templates/detail_template.eex
yordis 2c69df3
Update lib/ex_doc/formatter/markdown/templates/module_template.eex
yordis e21f6f3
Update lib/ex_doc/formatter/markdown/templates/module_template.eex
yordis daf8d8d
Update lib/ex_doc/formatter/markdown/templates/summary_template.eex
yordis fb2cb64
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 06cf685
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 4180268
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 71bf958
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 8bd9430
Update test/ex_doc/formatter/markdown_test.exs
yordis 7e5d02b
Update lib/ex_doc/formatter/markdown/templates/nav_template.eex
yordis 431d6ad
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 066af42
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 76f7228
Update lib/ex_doc/formatter/markdown/templates.ex
yordis bdf6826
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 6bcdbf4
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 9906ad5
Update lib/ex_doc/formatter/markdown.ex
yordis 4059a8d
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 5f451e2
Update lib/ex_doc/formatter/markdown/templates.ex
yordis 0e8111b
Update lib/ex_doc/doc_ast.ex
yordis c9ab715
Update test/ex_doc/formatter/markdown_test.exs
yordis 6cdb643
Update test/ex_doc/formatter/markdown_test.exs
yordis f71c480
Refactor normalize_output function in markdown formatter to use regex…
yordis 512fac1
Refactor formatter module name handling and improve markdown template…
yordis 2994e6c
Add tests for DocAST.to_markdown/1 functionality
yordis 2286b77
Refactor text truncation logic in markdown formatter for improved rea…
yordis 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
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,177 @@ | ||
| defmodule ExDoc.Formatter.MARKDOWN do | ||
yordis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @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\n", "\n"], "\n") | ||
yordis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| |> 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 |> ExDoc.DocAST.synopsis() |> extract_plain_text()}" | ||
| 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 |> ExDoc.DocAST.synopsis() |> extract_plain_text()}" | ||
| 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_plain_text(html) when is_binary(html) do | ||
| html | ||
| |> String.replace(~r/<[^>]*>/, "") | ||
| |> String.replace(~r/\s+/, " ") | ||
| |> String.trim() | ||
| |> case do | ||
| "" -> | ||
| "No documentation available" | ||
|
|
||
| text -> | ||
| text | ||
| |> String.slice(0, 150) | ||
| |> then(fn s -> if String.length(s) == 150, do: s <> "...", else: s end) | ||
yordis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
|
|
||
| defp extract_plain_text(_), do: "No documentation available" | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.