PhiaUi.Editor.ExportHelpers (phia_ui v0.1.17)

Copy Markdown View Source

Pure Elixir helpers for converting editor JSON documents to and from various text formats.

These functions operate on the TipTap/ProseMirror JSON document format used by PhiaUI editor components. No JavaScript or external dependencies required.

Supported Conversions

Export (JSON to format)

Import (format to JSON)

Document Format

The editor JSON format follows the TipTap/ProseMirror schema:

%{
  "type" => "doc",
  "content" => [
    %{"type" => "paragraph", "content" => [
      %{"type" => "text", "text" => "Hello ", "marks" => [%{"type" => "bold"}]},
      %{"type" => "text", "text" => "world"}
    ]}
  ]
}

Summary

Functions

Parses an HTML string into an editor JSON document.

Parses a Markdown string into an editor JSON document.

Converts an editor JSON document to a basic LaTeX string.

Converts an editor JSON document to a Markdown string.

Functions

from_html(html)

@spec from_html(String.t()) :: map()

Parses an HTML string into an editor JSON document.

Supports <p>, <h1>-<h6>, <strong>/<b>, <em>/<i>, <a>, <ul>/<ol>/<li>, <pre><code>, <blockquote>, <hr>, and <img>.

Uses basic regex/string parsing — not a full HTML parser. Suitable for simple, well-formed HTML from editor output.

Examples

iex> PhiaUi.Editor.ExportHelpers.from_html("<p>Hello <strong>world</strong></p>")
%{"type" => "doc", "content" => [
  %{"type" => "paragraph", "content" => [
    %{"type" => "text", "text" => "Hello "},
    %{"type" => "text", "text" => "world", "marks" => [%{"type" => "bold"}]}
  ]}
]}

from_markdown(markdown)

@spec from_markdown(String.t()) :: map()

Parses a Markdown string into an editor JSON document.

Supports headings (#), paragraphs, bold (**), italic (*), code (`), links ([text](url)), bullet lists (- or *), ordered lists (1.), code blocks (triple backticks), blockquotes (>), horizontal rules (---), and images (![alt](src)).

Examples

iex> PhiaUi.Editor.ExportHelpers.from_markdown("## Hello\n\nWorld")
%{"type" => "doc", "content" => [
  %{"type" => "heading", "attrs" => %{"level" => 2}, "content" => [
    %{"type" => "text", "text" => "Hello"}
  ]},
  %{"type" => "paragraph", "content" => [
    %{"type" => "text", "text" => "World"}
  ]}
]}

to_latex(arg1)

@spec to_latex(map() | nil) :: String.t()

Converts an editor JSON document to a basic LaTeX string.

Produces a minimal LaTeX document body (no preamble). Suitable for embedding in a \begin{document} block.

Examples

iex> doc = %{"type" => "doc", "content" => [
...>   %{"type" => "heading", "attrs" => %{"level" => 1}, "content" => [
...>     %{"type" => "text", "text" => "Title"}
...>   ]},
...>   %{"type" => "paragraph", "content" => [
...>     %{"type" => "text", "text" => "Hello"}
...>   ]}
...> ]}
iex> PhiaUi.Editor.ExportHelpers.to_latex(doc)
"\\section{Title}\n\nHello\n\n"

to_markdown(arg1)

@spec to_markdown(map() | nil) :: String.t()

Converts an editor JSON document to a Markdown string.

Walks the document tree and converts each node type to its Markdown equivalent.

Examples

iex> doc = %{"type" => "doc", "content" => [
...>   %{"type" => "heading", "attrs" => %{"level" => 2}, "content" => [
...>     %{"type" => "text", "text" => "Hello"}
...>   ]},
...>   %{"type" => "paragraph", "content" => [
...>     %{"type" => "text", "text" => "World"}
...>   ]}
...> ]}
iex> PhiaUi.Editor.ExportHelpers.to_markdown(doc)
"## Hello\n\nWorld\n\n"