Current section

Files

Jump to
html_to_markdown lib html_to_markdown.ex
Raw

lib/html_to_markdown.ex

# This file is auto-generated by alef — DO NOT EDIT.
# alef:hash:46e7877d1faec35173787e7ec0835f7ef1b696329621414dca09842e94b230f2
# To regenerate: alef generate
# To verify freshness: alef verify --exit-code
# Issues & docs: https://github.com/kreuzberg-dev/alef
defmodule HtmlToMarkdown do
@moduledoc "High-level API for html_to_markdown"
@doc "Convert HTML to Markdown, returning a [`ConversionResult`] with content, metadata, images, and warnings."
@spec convert(String.t()) :: {:ok, map()} | {:error, atom, String.t()}
def convert(html) do
HtmlToMarkdown.Native.convert(html, nil)
end
@doc "Convert HTML to Markdown, returning a [`ConversionResult`] with content, metadata, images, and warnings."
@spec convert(String.t(), String.t() | nil) :: {:ok, map()} | {:error, atom, String.t()}
def convert(html, options) when is_map(options) do
{visitor, clean_opts} = Map.pop(options, :visitor)
if is_map(visitor) do
{:ok, _} =
HtmlToMarkdown.Native.convert_with_visitor(
html,
if(map_size(clean_opts) == 0, do: nil, else: Jason.encode!(clean_opts)),
visitor
)
do_visitor_receive_loop(visitor)
else
HtmlToMarkdown.Native.convert(html, if(map_size(clean_opts) == 0, do: nil, else: Jason.encode!(clean_opts)))
end
end
def convert(html, nil) do
HtmlToMarkdown.Native.convert(html, nil)
end
@doc false
defp do_visitor_receive_loop(visitor) do
receive do
{:visitor_callback, ref_id, callback_name, args_json} ->
result =
case Map.get(visitor, callback_name) do
nil -> "continue"
fun -> apply_visitor_callback(fun, args_json)
end
HtmlToMarkdown.Native.visitor_reply(ref_id, result)
do_visitor_receive_loop(visitor)
{:ok, result} ->
{:ok, result}
{:error, reason} ->
{:error, reason}
after
30_000 ->
{:error, "visitor callback timeout after 30s"}
end
end
@doc false
defp apply_visitor_callback(fun, args_json) do
args = Jason.decode!(args_json)
result = fun.(args)
case result do
:continue -> "continue"
:skip -> "skip"
:preserve_html -> "preserve_html"
{:custom, value} -> to_string(value)
binary when is_binary(binary) -> binary
_ -> "continue"
end
end
end