Skip to content

Commit 1e5c09f

Browse files
committed
refactor: rename to_markdown_string to to_markdown and update related function calls
1 parent 7297cd3 commit 1e5c09f

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

lib/ex_doc/doc_ast.ex

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,22 @@ defmodule ExDoc.DocAST do
7171
The optional `fun` argument allows post-processing each AST node
7272
after it's been converted to markdown.
7373
"""
74-
def to_markdown_string(ast, fun \\ fn _ast, string -> string end)
74+
def to_markdown(ast, fun \\ fn _ast, string -> string end)
7575

76-
def to_markdown_string(binary, _fun) when is_binary(binary) do
76+
def to_markdown(binary, _fun) when is_binary(binary) do
7777
ExDoc.Utils.h(binary)
7878
end
7979

80-
def to_markdown_string(list, fun) when is_list(list) do
81-
result = Enum.map_join(list, "", &to_markdown_string(&1, fun))
80+
def to_markdown(list, fun) when is_list(list) do
81+
result = Enum.map_join(list, "", &to_markdown(&1, fun))
8282
fun.(list, result)
8383
end
8484

85-
def to_markdown_string({:comment, _attrs, inner, _meta} = ast, fun) do
85+
def to_markdown({:comment, _attrs, inner, _meta} = ast, fun) do
8686
fun.(ast, "<!--#{inner}-->")
8787
end
8888

89-
def to_markdown_string({:code, attrs, inner, _meta} = ast, fun) do
89+
def to_markdown({:code, attrs, inner, _meta} = ast, fun) do
9090
lang = attrs[:class] || ""
9191

9292
result = """
@@ -98,44 +98,50 @@ defmodule ExDoc.DocAST do
9898
fun.(ast, result)
9999
end
100100

101-
def to_markdown_string({:a, attrs, inner, _meta} = ast, fun) do
102-
result = "[#{to_markdown_string(inner, fun)}](#{attrs[:href]})"
101+
def to_markdown({:a, attrs, inner, _meta} = ast, fun) do
102+
result = "[#{to_markdown(inner, fun)}](#{attrs[:href]})"
103103
fun.(ast, result)
104104
end
105105

106-
def to_markdown_string({:hr, _attrs, _inner, _meta} = ast, fun) do
106+
def to_markdown({:hr, _attrs, _inner, _meta} = ast, fun) do
107107
result = "\n\n---\n\n"
108108
fun.(ast, result)
109109
end
110110

111-
def to_markdown_string({tag, _attrs, _inner, _meta} = ast, fun) when tag in [:p, :br] do
111+
def to_markdown({:p, _attrs, inner, _meta} = ast, fun) do
112+
result = to_markdown(inner, fun) <> "\n\n"
113+
fun.(ast, result)
114+
end
115+
116+
def to_markdown({:br, _attrs, _inner, _meta} = ast, fun) do
112117
result = "\n\n"
113118
fun.(ast, result)
114119
end
115120

116-
def to_markdown_string({:img, attrs, _inner, _meta} = ast, fun) do
121+
def to_markdown({:img, attrs, _inner, _meta} = ast, fun) do
117122
alt = attrs[:alt] || ""
118123
title = attrs[:title] || ""
119124
result = "![#{alt}](#{attrs[:src]} \"#{title}\")"
120125
fun.(ast, result)
121126
end
122127

123128
# ignoring these: area base col command embed input keygen link meta param source track wbr
124-
def to_markdown_string({tag, _attrs, _inner, _meta} = ast, fun) when tag in @void_elements do
129+
def to_markdown({tag, _attrs, _inner, _meta} = ast, fun) when tag in @void_elements do
125130
result = ""
126131
fun.(ast, result)
127132
end
128133

129-
def to_markdown_string({_tag, _attrs, inner, %{verbatim: true}} = ast, fun) do
134+
def to_markdown({_tag, _attrs, inner, %{verbatim: true}} = ast, fun) do
130135
result = Enum.join(inner, "")
131136
fun.(ast, result)
132137
end
133138

134-
def to_markdown_string({_tag, _attrs, inner, _meta} = ast, fun) do
135-
result = to_markdown_string(inner, fun)
139+
def to_markdown({_tag, _attrs, inner, _meta} = ast, fun) do
140+
result = to_markdown(inner, fun)
136141
fun.(ast, result)
137142
end
138143

144+
139145
## parse markdown
140146

141147
defp parse_markdown(markdown, opts) do

lib/ex_doc/formatter.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ defmodule ExDoc.Formatter do
137137
mod_id <> "." <> id
138138
end
139139

140-
defp autolink_and_render(doc, ".md", language, autolink_opts, _opts) do
140+
defp autolink_and_render(doc, ".md", language, autolink_opts, opts) do
141141
doc
142142
|> language.autolink_doc(autolink_opts)
143-
|> ExDoc.DocAST.to_markdown_string()
143+
|> ExDoc.DocAST.highlight(language, opts)
144144
end
145145

146146
defp autolink_and_render(doc, _html_ext, language, autolink_opts, opts) do

lib/ex_doc/formatter/markdown/templates.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ defmodule ExDoc.Formatter.MARKDOWN.Templates do
3939
static_file |> Path.basename() |> text_to_id()
4040
end
4141

42+
def node_doc(%{doc: doc}) when is_list(doc) do
43+
# Handle DocAST by converting to markdown
44+
ExDoc.DocAST.to_markdown(doc)
45+
end
46+
47+
def node_doc(%{doc: doc}) when is_binary(doc), do: doc
4248
def node_doc(%{source_doc: %{"en" => source}}) when is_binary(source), do: source
4349
def node_doc(%{rendered_doc: source}) when is_binary(source), do: source
4450

0 commit comments

Comments
 (0)