Skip to content

Commit ae05a4f

Browse files
Do not parse math expressions as markdown (#1826)
1 parent d43e5fa commit ae05a4f

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

lib/ex_doc/markdown/earmark.ex

+12-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ defmodule ExDoc.Markdown.Earmark do
2929
line: 1,
3030
file: "nofile",
3131
breaks: false,
32-
pure_links: true
32+
pure_links: true,
33+
math: true
3334
]
3435

3536
options = Keyword.merge(options, opts)
@@ -63,6 +64,16 @@ defmodule ExDoc.Markdown.Earmark do
6364
fixup({tag, attrs, ast, %{}})
6465
end
6566

67+
# Rewrite math back to the original syntax, it's up to the user to render it
68+
69+
defp fixup({"code", [{"class", "math-inline"}], [content], _}) do
70+
"$#{content}$"
71+
end
72+
73+
defp fixup({"code", [{"class", "math-display"}], [content], _}) do
74+
"$$\n#{content}\n$$"
75+
end
76+
6677
defp fixup({tag, attrs, ast, meta}) when is_binary(tag) and is_list(attrs) and is_map(meta) do
6778
{fixup_tag(tag), Enum.map(attrs, &fixup_attr/1), fixup(ast), meta}
6879
end

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule ExDoc.Mixfile do
3434

3535
defp deps do
3636
[
37-
{:earmark_parser, "~> 1.4.38"},
37+
{:earmark_parser, "~> 1.4.39"},
3838
{:makeup_elixir, "~> 0.14"},
3939
{:makeup_erlang, "~> 0.1"},
4040
# Add other makeup lexers as optional for the executable

mix.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%{
2-
"earmark_parser": {:hex, :earmark_parser, "1.4.38", "b42252eddf63bda05554ba8be93a1262dc0920c721f1aaf989f5de0f73a2e367", [:mix], [], "hexpm", "2cd0907795aaef0c7e8442e376633c5b3bd6edc8dbbdc539b22f095501c1cdb6"},
2+
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
33
"easyhtml": {:hex, :easyhtml, "0.2.0", "0cd913f920392b5b5f74ff94c1795f8fc5c0812213ef4c466af7705348e1c05d", [:mix], [{:floki, "~> 0.30", [hex: :floki, repo: "hexpm", optional: false]}], "hexpm", "f827387bbafe03b70df5e392f5e536b579a1dfa315714fb827ffde4e620d44a9"},
44
"floki": {:hex, :floki, "0.34.2", "5fad07ef153b3b8ec110b6b155ec3780c4b2c4906297d0b4be1a7162d04a7e02", [:mix], [], "hexpm", "26b9d50f0f01796bc6be611ca815c5e0de034d2128e39cc9702eee6b66a4d1c8"},
55
"jason": {:hex, :jason, "1.2.0", "10043418c42d2493d0ee212d3fddd25d7ffe484380afad769a0a38795938e448", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "116747dbe057794c3a3e4e143b7c8390b29f634e16c78a7f59ba75bfa6852e7f"},

test/ex_doc/markdown/earmark_test.exs

+10
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,15 @@ defmodule ExDoc.Markdown.EarmarkTest do
7474
{:pre, [], [{:code, [class: "mermaid output"], ["graph TD; A-->B;"], %{}}], %{}}
7575
]
7676
end
77+
78+
test "keeps math syntax without interpreting math as markdown" do
79+
assert Markdown.to_ast("Math $x *y* y$", []) == [
80+
{:p, [], ["Math ", "$x *y* y$"], %{}}
81+
]
82+
83+
assert Markdown.to_ast("Math $$x$$", []) == [
84+
{:p, [], ["Math ", "$$\nx\n$$"], %{}}
85+
]
86+
end
7787
end
7888
end

0 commit comments

Comments
 (0)