mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-01 09:14:38 +08:00
Fix markdown formatting to properly indent long list nested items (#2663)
This commit is contained in:
parent
6f7a8e3e79
commit
12ccf3c683
2 changed files with 25 additions and 4 deletions
|
@ -375,15 +375,21 @@ defmodule Livebook.LiveMarkdown.MarkdownHelpers do
|
|||
|
||||
defp render_unordered_list(content) do
|
||||
marker_fun = fn _index -> "* " end
|
||||
render_list(content, marker_fun, " ")
|
||||
indent_fun = fn _index -> 2 end
|
||||
render_list(content, marker_fun, indent_fun)
|
||||
end
|
||||
|
||||
defp render_ordered_list(content) do
|
||||
marker_fun = fn index -> "#{index + 1}. " end
|
||||
render_list(content, marker_fun, " ")
|
||||
indent_fun = fn index -> number_of_digits(index + 1) + 2 end
|
||||
render_list(content, marker_fun, indent_fun)
|
||||
end
|
||||
|
||||
defp render_list(items, marker_fun, indent) do
|
||||
defp number_of_digits(n) do
|
||||
n |> Integer.digits() |> length()
|
||||
end
|
||||
|
||||
defp render_list(items, marker_fun, indent_fun) do
|
||||
spaced? = spaced_list_items?(items)
|
||||
item_separator = if(spaced?, do: "\n\n", else: "\n")
|
||||
|
||||
|
@ -398,7 +404,7 @@ defmodule Livebook.LiveMarkdown.MarkdownHelpers do
|
|||
lines =
|
||||
Enum.map(lines, fn
|
||||
"" -> ""
|
||||
line -> [indent, line]
|
||||
line -> [String.duplicate(" ", indent_fun.(index)), line]
|
||||
end)
|
||||
|
||||
Enum.intersperse([first_line | lines], "\n")
|
||||
|
|
|
@ -328,6 +328,21 @@ defmodule Livebook.LiveMarkdown.MarkdownHelpersTest do
|
|||
assert markdown == reformat(markdown)
|
||||
end
|
||||
|
||||
test "properly indents nested lists when the parent list has double digit items" do
|
||||
markdown = """
|
||||
#{Enum.map_join(1..9, "\n", fn n -> "#{n}. Item #{n}" end)}
|
||||
10. Item 10
|
||||
* Child item 1
|
||||
* Child item 2
|
||||
11. Item 11
|
||||
```
|
||||
Enum.to_list(1..10)
|
||||
```\
|
||||
"""
|
||||
|
||||
assert markdown == reformat(markdown)
|
||||
end
|
||||
|
||||
test "surprise ordered list" do
|
||||
markdown = "1986\\. What a great season."
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue