File tree Expand file tree Collapse file tree 3 files changed +73
-50
lines changed
Expand file tree Collapse file tree 3 files changed +73
-50
lines changed Original file line number Diff line number Diff line change @@ -29,56 +29,7 @@ defmodule Draft do
2929 def to_html ( input ) do
3030 input
3131 |> blocks
32- |> Enum . map ( & process_block / 1 )
32+ |> Enum . map ( & Draft.Block . to_html / 1 )
3333 |> Enum . join ( "" )
3434 end
35-
36- defp process_block ( % { "type" => "unstyled" ,
37- "text" => "" ,
38- "key" => _ ,
39- "data" => _ ,
40- "depth" => _ ,
41- "entityRanges" => _ ,
42- "inlineStyleRanges" => _ } ) do
43- "<br>"
44- end
45-
46- defp process_block ( % { "type" => "header-" <> header ,
47- "text" => text ,
48- "key" => _ ,
49- "data" => _ ,
50- "depth" => _ ,
51- "entityRanges" => _ ,
52- "inlineStyleRanges" => _ } ) do
53- tag = header_tags [ header ]
54- "<#{ tag } >#{ text } </#{ tag } >"
55- end
56-
57- defp process_block ( % { "type" => "blockquote" ,
58- "text" => text ,
59- "key" => _ ,
60- "data" => _ ,
61- "depth" => _ ,
62- "entityRanges" => _ ,
63- "inlineStyleRanges" => _ } ) do
64- "<blockquote>#{ text } </blockquote>"
65- end
66-
67- defp process_block ( % { "type" => "unstyled" ,
68- "text" => text ,
69- "key" => _ ,
70- "data" => _ ,
71- "depth" => _ ,
72- "entityRanges" => _ ,
73- "inlineStyleRanges" => _ } ) do
74- "<p>#{ text } </p>"
75- end
76-
77- defp header_tags do
78- % {
79- "one" => "h1" ,
80- "two" => "h2" ,
81- "three" => "h3"
82- }
83- end
8435end
Original file line number Diff line number Diff line change 1+ defmodule Draft.Block do
2+ @ moduledoc """
3+ Converts a single DraftJS block to html.
4+ """
5+
6+ @ doc """
7+ Renders the given DraftJS input as html.
8+
9+ ## Examples
10+ iex> block = %{"key" => "1", "text" => "Hello", "type" => "unstyled",
11+ ...> "depth" => 0, "inlineStyleRanges" => [], "entityRanges" => [],
12+ ...> "data" => %{}}
13+ iex> Draft.Block.to_html block
14+ "<p>Hello</p>"
15+ """
16+ def to_html ( block ) do
17+ process_block ( block )
18+ end
19+
20+ defp process_block ( % { "type" => "unstyled" ,
21+ "text" => "" ,
22+ "key" => _ ,
23+ "data" => _ ,
24+ "depth" => _ ,
25+ "entityRanges" => _ ,
26+ "inlineStyleRanges" => _ } ) do
27+ "<br>"
28+ end
29+
30+ defp process_block ( % { "type" => "header-" <> header ,
31+ "text" => text ,
32+ "key" => _ ,
33+ "data" => _ ,
34+ "depth" => _ ,
35+ "entityRanges" => _ ,
36+ "inlineStyleRanges" => _ } ) do
37+ tag = header_tags [ header ]
38+ "<#{ tag } >#{ text } </#{ tag } >"
39+ end
40+
41+ defp process_block ( % { "type" => "blockquote" ,
42+ "text" => text ,
43+ "key" => _ ,
44+ "data" => _ ,
45+ "depth" => _ ,
46+ "entityRanges" => _ ,
47+ "inlineStyleRanges" => _ } ) do
48+ "<blockquote>#{ text } </blockquote>"
49+ end
50+
51+ defp process_block ( % { "type" => "unstyled" ,
52+ "text" => text ,
53+ "key" => _ ,
54+ "data" => _ ,
55+ "depth" => _ ,
56+ "entityRanges" => _ ,
57+ "inlineStyleRanges" => _ } ) do
58+ "<p>#{ text } </p>"
59+ end
60+
61+ defp header_tags do
62+ % {
63+ "one" => "h1" ,
64+ "two" => "h2" ,
65+ "three" => "h3"
66+ }
67+ end
68+ end
Original file line number Diff line number Diff line change 1+ defmodule BlockTest do
2+ use ExUnit.Case
3+ doctest Draft.Block
4+ end
You can’t perform that action at this time.
0 commit comments