mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-01-01 12:41:43 +08:00
Preserve Markdown modifiers in headings (#933)
This commit is contained in:
parent
23cd59713f
commit
928cb5c592
2 changed files with 14 additions and 19 deletions
|
@ -108,11 +108,11 @@ defmodule Livebook.LiveMarkdown.Import do
|
|||
|
||||
defp group_elements([], elems), do: elems
|
||||
|
||||
defp group_elements([{"h1", _, content, %{}} | ast], elems) do
|
||||
defp group_elements([{"h1", _, [content], %{}} | ast], elems) do
|
||||
group_elements(ast, [{:notebook_name, content} | elems])
|
||||
end
|
||||
|
||||
defp group_elements([{"h2", _, content, %{}} | ast], elems) do
|
||||
defp group_elements([{"h2", _, [content], %{}} | ast], elems) do
|
||||
group_elements(ast, [{:section_name, content} | elems])
|
||||
end
|
||||
|
||||
|
@ -265,13 +265,12 @@ defmodule Livebook.LiveMarkdown.Import do
|
|||
end
|
||||
|
||||
defp build_notebook(
|
||||
[{:section_name, content} | elems],
|
||||
[{:section_name, name} | elems],
|
||||
cells,
|
||||
sections,
|
||||
messages,
|
||||
output_counter
|
||||
) do
|
||||
name = text_from_markdown(content)
|
||||
{metadata, elems} = grab_metadata(elems)
|
||||
attrs = section_metadata_to_attrs(metadata)
|
||||
section = %{Notebook.Section.new() | name: name, cells: cells} |> Map.merge(attrs)
|
||||
|
@ -280,7 +279,7 @@ defmodule Livebook.LiveMarkdown.Import do
|
|||
|
||||
# If there are section-less cells, put them in a default one.
|
||||
defp build_notebook(
|
||||
[{:notebook_name, _content} | _] = elems,
|
||||
[{:notebook_name, _name} | _] = elems,
|
||||
cells,
|
||||
sections,
|
||||
messages,
|
||||
|
@ -297,8 +296,7 @@ defmodule Livebook.LiveMarkdown.Import do
|
|||
build_notebook(elems, [], [section | sections], messages, output_counter)
|
||||
end
|
||||
|
||||
defp build_notebook([{:notebook_name, content} | elems], [], sections, messages, output_counter) do
|
||||
name = text_from_markdown(content)
|
||||
defp build_notebook([{:notebook_name, name} | elems], [], sections, messages, output_counter) do
|
||||
{metadata, elems} = grab_metadata(elems)
|
||||
# If there are any non-metadata comments we keep them
|
||||
{comments, elems} = grab_leading_comments(elems)
|
||||
|
@ -334,13 +332,6 @@ defmodule Livebook.LiveMarkdown.Import do
|
|||
{notebook, messages}
|
||||
end
|
||||
|
||||
defp text_from_markdown(markdown) do
|
||||
markdown
|
||||
|> MarkdownHelpers.markdown_to_ast()
|
||||
|> elem(1)
|
||||
|> MarkdownHelpers.text_from_ast()
|
||||
end
|
||||
|
||||
# Takes optional leading metadata JSON object and returns {metadata, rest}.
|
||||
defp grab_metadata([{:metadata, metadata} | elems]) do
|
||||
{metadata, elems}
|
||||
|
|
|
@ -202,21 +202,25 @@ defmodule Livebook.LiveMarkdown.ImportTest do
|
|||
assert ["Downgrading all headings, because 2 instances of heading 1 were found"] == messages
|
||||
end
|
||||
|
||||
test "ignores markdown modifiers in notebok/section names" do
|
||||
test "preserves markdown modifiers in notebok/section names" do
|
||||
markdown = """
|
||||
# My *Notebook*
|
||||
|
||||
## [Section 1](https://example.com)
|
||||
|
||||
## ---
|
||||
|
||||
## # Section
|
||||
"""
|
||||
|
||||
{notebook, []} = Import.notebook_from_markdown(markdown)
|
||||
|
||||
assert %Notebook{
|
||||
name: "My Notebook",
|
||||
name: "My *Notebook*",
|
||||
sections: [
|
||||
%Notebook.Section{
|
||||
name: "Section 1"
|
||||
}
|
||||
%Notebook.Section{name: "[Section 1](https://example.com)"},
|
||||
%Notebook.Section{name: "---"},
|
||||
%Notebook.Section{name: "# Section"}
|
||||
]
|
||||
} = notebook
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue