Skip to content

Commit 22fd124

Browse files
authored
Merge pull request #759 from code-corps/add-syntax-highlighting-to-markdown
Add syntax highlighting to markdown
2 parents 7426c45 + ad6d705 commit 22fd124

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/code_corps/services/markdown_renderer.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ defmodule CodeCorps.Services.MarkdownRendererService do
1616

1717
@spec convert_into_html(String.t) :: String.t
1818
defp convert_into_html(markdown) do
19-
Earmark.as_html!(markdown)
19+
# Prism.js requires a `language-` prefix in code classes
20+
# See: https://github.com/pragdave/earmark#syntax-highlightning
21+
Earmark.as_html!(markdown, %Earmark.Options{code_class_prefix: "language-"})
2022
end
2123

2224
@spec put_into(String.t, Changeset.t, atom) :: Changeset.t

test/lib/code_corps/services/markdown_renderer_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ defmodule CodeCorps.Services.MarkdownRendererServiceTest do
2121
assert changeset |> Ecto.Changeset.get_change(:body) == "<p>A <strong>strong</strong> body</p>\n"
2222
end
2323

24+
test "adds the right css class prefixes" do
25+
attrs = @valid_attrs |> Map.merge(%{markdown: "```css\nspan {}\n```"})
26+
changeset =
27+
%Task{}
28+
|> Task.changeset(attrs)
29+
|> render_markdown_to_html(:markdown, :body)
30+
31+
assert changeset |> Ecto.Changeset.get_change(:body) == "<pre><code class=\"css language-css\">span {}</code></pre>\n"
32+
end
33+
2434
test "returns changeset when changeset is invalid" do
2535
changeset =
2636
%Task{}

0 commit comments

Comments
 (0)