Packages

Furlex is a structured data extraction tool written in Elixir. It currently supports unfurling oEmbed, Twitter Card, Facebook Open Graph, JSON-LD and plain ole' HTML `<meta />` data out of any url you supply.

Current section

Files

Jump to
furlex lib furlex parser json_ld.ex
Raw

lib/furlex/parser/json_ld.ex

defmodule Furlex.Parser.JsonLD do
@behaviour Furlex.Parser
@json_library Application.get_env(:furlex, :json_library, Jason)
@spec parse(String.t) :: nil | {:ok, List.t}
def parse(html) do
meta = "script[type=\"application/ld+json\"]"
case Floki.find(html, meta) do
nil ->
{:ok, []}
elements ->
json_ld =
elements
|> Enum.map(&decode/1)
|> List.flatten()
{:ok, json_ld}
end
end
defp decode(element) do
element
|> Floki.text(js: true)
|> @json_library.decode!()
end
end