Skip to content

Commit

Permalink
Keep language class on livebook output code block (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Mar 16, 2023
1 parent e63e957 commit 4b5c414
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/ex_doc/markdown/earmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ defmodule ExDoc.Markdown.Earmark do
],
acc
) do
code_attrs = Enum.reject(code_attrs, &match?({"class", _}, &1))
new_code = {"code", [{"class", "output"} | code_attrs], [source], code_meta}
fixup_list([{"pre", pre_attrs, [new_code], pre_meta} | ast], acc)
code_attrs =
case Enum.split_with(code_attrs, &match?({"class", _}, &1)) do
{[], attrs} -> [{"class", "output"} | attrs]
{[{"class", class}], attrs} -> [{"class", "#{class} output"} | attrs]
end

code_node = {"code", code_attrs, [source], code_meta}
fixup_list([{"pre", pre_attrs, [code_node], pre_meta} | ast], acc)
end

defp fixup_list([head | tail], acc) do
Expand Down
9 changes: 8 additions & 1 deletion test/ex_doc/markdown/earmark_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ defmodule ExDoc.Markdown.EarmarkTest do
```
2
```
<!-- livebook:{"output":true} -->
```mermaid
graph TD; A-->B;
```
"""

assert Markdown.to_ast(md, []) == [
{:h1, [], ["Notebook"], %{}},
{:h2, [], ["Example"], %{}},
{:pre, [], [{:code, [class: "elixir"], ["1 + 1"], %{}}], %{}},
{:pre, [], [{:code, [class: "output"], ["2"], %{}}], %{}}
{:pre, [], [{:code, [class: "output"], ["2"], %{}}], %{}},
{:pre, [], [{:code, [class: "mermaid output"], ["graph TD; A-->B;"], %{}}], %{}}
]
end
end
Expand Down

0 comments on commit 4b5c414

Please # to comment.