From 928cb5c59246fd3dee70eb19912f0797add1de16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Tue, 25 Jan 2022 16:38:52 +0100 Subject: [PATCH] Preserve Markdown modifiers in headings (#933) --- lib/livebook/live_markdown/import.ex | 19 +++++-------------- test/livebook/live_markdown/import_test.exs | 14 +++++++++----- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/livebook/live_markdown/import.ex b/lib/livebook/live_markdown/import.ex index c4a96c4f7..9a337c306 100644 --- a/lib/livebook/live_markdown/import.ex +++ b/lib/livebook/live_markdown/import.ex @@ -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} diff --git a/test/livebook/live_markdown/import_test.exs b/test/livebook/live_markdown/import_test.exs index 847d3635d..b03fb2d22 100644 --- a/test/livebook/live_markdown/import_test.exs +++ b/test/livebook/live_markdown/import_test.exs @@ -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