Skip to content

Commit 1faf08a

Browse files
committed
Add blocks function
This returns the raw DraftJS blocks as Elixir data structures. Also add docs to the public interface.
1 parent f448a63 commit 1faf08a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

lib/draft.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
defmodule Draft do
2-
def to_html(input) do
2+
@moduledoc """
3+
Provides functions for parsing DraftJS content.
4+
"""
5+
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
318
Poison.Parser.parse!(input)["blocks"]
19+
end
20+
21+
@doc """
22+
Renders the given DraftJS input as html.
23+
24+
## Examples
25+
iex> draft = ~s({"entityMap":{},"blocks":[{"key":"1","text":"Hello","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]})
26+
iex> Draft.to_html draft
27+
"<p>Hello</p>"
28+
"""
29+
def to_html(input) do
30+
input
31+
|> blocks
432
|> Enum.map(&process_block/1)
533
|> Enum.join("")
634
end

mix.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ defmodule Draft.Mixfile do
2828
# Type "mix help deps" for more examples and options
2929
defp deps do
3030
[
31-
{:poison, "~> 2.0"},
32-
{:credo, "~> 0.3", only: [:dev, :test]}
31+
{:credo, "~> 0.3", only: [:dev, :test]},
32+
{:ex_doc, "~> 0.14", only: :dev},
33+
{:poison, "~> 2.0"}
3334
]
3435
end
3536
end

mix.lock

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

0 commit comments

Comments
 (0)