Skip to content

Commit abdd86c

Browse files
committed
feat: add convert!/2 function for HTML to Markdown conversion with error handling
1 parent 9cf0de4 commit abdd86c

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/htmd.ex

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defmodule Htmd do
6363
## Options
6464
6565
* `:heading_style` - `:atx` (default) or `:setex`
66-
* `:hr_style` - `:dashes` (default), `:underscores`, or `:stars`
66+
* `:hr_style` - `:dashes` (default), `:underscores`, or `:stars`
6767
* `:br_style` - `:two_spaces` (default) or `:backslash`
6868
* `:link_style` - `:inlined` (default), `:inlined_prefer_autolinks`, or `:referenced`
6969
* `:link_reference_style` - `:full` (default), `:collapsed`, or `:shortcut`
@@ -90,6 +90,25 @@ defmodule Htmd do
9090
Native.convert_with_options(html, convert_options)
9191
end
9292

93+
@doc """
94+
Converts HTML string to Markdown, silently returning an empty string on error.
95+
## Examples
96+
97+
iex> Htmd.convert!("<h1>Hello World</h1>")
98+
"# Hello World"
99+
100+
iex> Htmd.convert!("<p>Simple paragraph</p>")
101+
"Simple paragraph"
102+
103+
"""
104+
@spec convert!(html, options) :: binary()
105+
def convert!(html, options \\ []) when is_binary(html) do
106+
case convert(html, options) do
107+
{:ok, markdown} -> markdown
108+
{:error, _} -> ""
109+
end
110+
end
111+
93112
defp build_options(options) when is_list(options) do
94113
struct(ConvertOptions, options)
95114
end

0 commit comments

Comments
 (0)