Skip to content

Commit

Permalink
kramdown converter: Add list_indent option
Browse files Browse the repository at this point in the history
Some markdown dialects need to have nested lists indented by 4 spaces.
The kramdown converter writes the 2 spaces needed by kramdown.
The option list_indent allows setting this to 4 so that other markdown
readers can use the output.
  • Loading branch information
cabo authored and gettalong committed Apr 25, 2022
1 parent 21b448c commit 94f4d06
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/kramdown/converter/kramdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def initialize(root, options)
@footnotes = []
@abbrevs = []
@stack = []
@list_indent = options[:list_indent] || 2
@list_spacing = ' ' * (@list_indent - 2)
end

def convert(el, opts = {indent: 0})
Expand Down Expand Up @@ -127,7 +129,7 @@ def convert_ul(el, opts)

def convert_li(el, opts)
sym, width = if @stack.last.type == :ul
[+'* ', el.children.first && el.children.first.type == :codeblock ? 4 : 2]
['* ' + @list_spacing, el.children.first && el.children.first.type == :codeblock ? 4 : @list_indent]
else
["#{opts[:index] + 1}.".ljust(4), 4]
end
Expand All @@ -154,7 +156,7 @@ def convert_li(el, opts)
end

def convert_dd(el, opts)
sym, width = +": ", (el.children.first && el.children.first.type == :codeblock ? 4 : 2)
sym, width = ": " + @list_spacing, (el.children.first && el.children.first.type == :codeblock ? 4 : @list_indent)
if (ial = ial_for_element(el))
sym << ial << " "
end
Expand Down
3 changes: 3 additions & 0 deletions test/test_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ def tidy_output(out)
kdtext = Kramdown::Document.new(File.read(text_file), options).to_kramdown
html = Kramdown::Document.new(kdtext, options).to_html
assert_equal(tidy_output(File.read(html_file)), tidy_output(html))
kdtext4 = Kramdown::Document.new(File.read(text_file), options.merge({list_indent: 4})).to_kramdown
html = Kramdown::Document.new(kdtext4, options).to_html
assert_equal(tidy_output(File.read(html_file)), tidy_output(html))
end
end
end
Expand Down

0 comments on commit 94f4d06

Please sign in to comment.