Skip to content

Commit f1b5e64

Browse files
committed
Remove json parsing
This solves a couple problems: 1. Poison was pretty out of date (so trying to use the lib caused version conflicts with the version we were already using in our app) 2. Our draftjs content wasn't stored as a string, it was stored as a map. Removing json parsing will let the user parse it if they need to (in whatever way they want), and let those who don't need to use it out of the box.
1 parent 67a922c commit f1b5e64

File tree

4 files changed

+12
-27
lines changed

4 files changed

+12
-27
lines changed

lib/draft.ex

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,17 @@ defmodule Draft do
33
Provides functions for parsing DraftJS content.
44
"""
55

6-
@doc """
7-
Parses the given DraftJS input and returns the blocks as a list of
8-
maps.
9-
10-
## Examples
11-
iex> draft = ~s({"entityMap":{},"blocks":[{"key":"1","text":"Hello","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
12-
iex> Draft.blocks draft
13-
[%{"key" => "1", "text" => "Hello", "type" => "unstyled",
14-
"depth" => 0, "inlineStyleRanges" => [], "entityRanges" => [],
15-
"data" => %{}}]
16-
"""
17-
def blocks(input) do
18-
Poison.Parser.parse!(input)["blocks"]
19-
end
20-
216
@doc """
227
Renders the given DraftJS input as html.
238
249
## Examples
25-
iex> draft = ~s({"entityMap":{},"blocks":[{"key":"1","text":"Hello","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
10+
iex> draft = %{"entityMap"=>%{},"blocks"=>[%{"key"=>"1","text"=>"Hello","type"=>"unstyled","depth"=>0,"inlineStyleRanges"=>[],"entityRanges"=>[],"data"=>%{}}]}
2611
iex> Draft.to_html draft
2712
"<p>Hello</p>"
2813
"""
2914
def to_html(input) do
3015
input
31-
|> blocks
16+
|> Map.get("blocks")
3217
|> Enum.map(&Draft.Block.to_html/1)
3318
|> Enum.join("")
3419
end

mix.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ defmodule Draft.Mixfile do
2929
defp deps do
3030
[
3131
{:credo, "~> 0.3", only: [:dev, :test]},
32-
{:ex_doc, "~> 0.14", only: :dev},
33-
{:poison, "~> 2.0"}
32+
{:ex_doc, "~> 0.14", only: :dev}
3433
]
3534
end
3635
end

mix.lock

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
%{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], []},
1+
%{
2+
"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], []},
23
"credo": {:hex, :credo, "0.5.2", "92e8c9f86e0ffbf9f688595e9f4e936bc96a52e5606d2c19713e9e4d191d5c74", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, optional: false]}]},
34
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
45
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
5-
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []}}
6+
}

test/draft_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@ defmodule DraftTest do
33
doctest Draft
44

55
test "generate a <p>" do
6-
input = ~s({"entityMap":{},"blocks":[{"key":"9d21d","text":"Hello","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
6+
input = %{"entityMap"=>%{},"blocks"=>[%{"key"=>"9d21d","text"=>"Hello","type"=>"unstyled","depth"=>0,"inlineStyleRanges"=>[],"entityRanges"=>[],"data"=>%{}}]}
77
output = "<p>Hello</p>"
88
assert Draft.to_html(input) == output
99
end
1010

1111
test "generate a <h1>" do
12-
input = ~s({"entityMap":{},"blocks":[{"key":"9d21d","text":"Hello","type":"header-one","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
12+
input = %{"entityMap"=>%{},"blocks"=>[%{"key"=>"9d21d","text"=>"Hello","type"=>"header-one","depth"=>0,"inlineStyleRanges"=>[],"entityRanges"=>[],"data"=>%{}}]}
1313
output = "<h1>Hello</h1>"
1414
assert Draft.to_html(input) == output
1515
end
1616

1717
test "generate a <h2>" do
18-
input = ~s({"entityMap":{},"blocks":[{"key":"9d21d","text":"Hello","type":"header-two","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
18+
input = %{"entityMap"=>%{},"blocks"=>[%{"key"=>"9d21d","text"=>"Hello","type"=>"header-two","depth"=>0,"inlineStyleRanges"=>[],"entityRanges"=>[],"data"=>%{}}]}
1919
output = "<h2>Hello</h2>"
2020
assert Draft.to_html(input) == output
2121
end
2222

2323
test "generate a <h3>" do
24-
input = ~s({"entityMap":{},"blocks":[{"key":"9d21d","text":"Hello","type":"header-three","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
24+
input = %{"entityMap"=>%{},"blocks"=>[%{"key"=>"9d21d","text"=>"Hello","type"=>"header-three","depth"=>0,"inlineStyleRanges"=>[],"entityRanges"=>[],"data"=>%{}}]}
2525
output = "<h3>Hello</h3>"
2626
assert Draft.to_html(input) == output
2727
end
2828

2929
test "generate a <blockquote>" do
30-
input = ~s({"entityMap":{},"blocks":[{"key":"9d21d","text":"Hello","type":"blockquote","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
30+
input = %{"entityMap"=>%{},"blocks"=>[%{"key"=>"9d21d","text"=>"Hello","type"=>"blockquote","depth"=>0,"inlineStyleRanges"=>[],"entityRanges"=>[],"data"=>%{}}]}
3131
output = "<blockquote>Hello</blockquote>"
3232
assert Draft.to_html(input) == output
3333
end
3434

3535
test "generate a <br>" do
36-
input = ~s({"entityMap":{},"blocks":[{"key":"9d21d","text":"","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
36+
input = %{"entityMap"=>%{},"blocks"=>[%{"key"=>"9d21d","text"=>"","type"=>"unstyled","depth"=>0,"inlineStyleRanges"=>[],"entityRanges"=>[],"data"=>%{}}]}
3737
output = "<br>"
3838
assert Draft.to_html(input) == output
3939
end

0 commit comments

Comments
 (0)