Add public function for converting .livemd to .exs (#573)

This commit is contained in:
Jonatan Kłosko 2021-10-05 00:26:41 +02:00 committed by GitHub
parent a57927ec2a
commit 25d90eabf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

27
lib/livebook.ex Normal file
View file

@ -0,0 +1,27 @@
defmodule Livebook do
@moduledoc """
Livebook is an interactive notebook system for Elixir.
This module includes the public API.
"""
@doc """
Parses the given Live Markdown document and converts it to Elixir
source code.
## Limitations
Note that the resulting script may not compile in some cases, for
example if you define a macro in one cell and import it in another
cell, it works fine in Livebook, because each cell is compiled
separately. However, when running the script it gets compiled as a
whole and consequently doing so doesn't work.
Additionally, branching sections are commented out.
"""
@spec live_markdown_to_elixir(String.t()) :: String.t()
def live_markdown_to_elixir(markdown) do
{notebook, _messages} = Livebook.LiveMarkdown.Import.notebook_from_markdown(markdown)
Livebook.Notebook.Export.Elixir.notebook_to_elixir(notebook)
end
end

27
test/livebook_test.exs Normal file
View file

@ -0,0 +1,27 @@
defmodule LivebookTest do
use ExUnit.Case, async: true
test "live_markdown_to_elixir/1" do
markdown = """
# Lists
## Introduction
Let's generate a list of numbers:
```elixir
Enum.to_list(1..10)
```
"""
assert Livebook.live_markdown_to_elixir(markdown) == """
# Title: Lists
# ── Introduction ──
# Let's generate a list of numbers:
Enum.to_list(1..10)
"""
end
end