Skip to content

Commit

Permalink
[bugfix] Fix edge case in Kramdown converter
Browse files Browse the repository at this point in the history
weird combination of LI and P tags can cause the converter to crash

fixes gettalong#809
  • Loading branch information
honzasterba committed Jul 18, 2024
1 parent bd678ec commit 407ea50
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/kramdown/converter/kramdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def convert_li(el, opts)
if el.children.first && el.children.first.type == :p && !el.children.first.options[:transparent]
res = +"#{sym}#{text}"
res << "^\n" if el.children.size == 1 && @stack.last.children.last == el &&
(@stack.last.children.any? {|c| c.children.first.type != :p } || @stack.last.children.size == 1)
(@stack.last.children.any? {|c| c.children.first && c.children.first.type != :p } || @stack.last.children.size == 1)
res
elsif el.children.first && el.children.first.type == :codeblock
"#{sym}\n #{text}"
Expand Down
37 changes: 37 additions & 0 deletions test/test_converter_kramdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8; frozen_string_literal: true -*-
#
#--
# Copyright (C) 2009-2019 Thomas Leitner <[email protected]>
#
# This file is part of kramdown which is licensed under the MIT.
#++
#

require 'minitest/autorun'
require 'kramdown/converter/kramdown'

describe Kramdown::Converter::Kramdown do
it "converts weird html to kramdown" do
html = <<~HTML
<ul>
<li></li>
<li>
<p>one</p>
</li>
<li>
<p>two</p>
</li>
</ul>
HTML
doc, = Kramdown::Parser::Html.parse(html)
md, = Kramdown::Converter::Kramdown.convert(doc)
expected = <<~MD
*
* one
* two
MD
assert_equal expected, md
end
end

0 comments on commit 407ea50

Please sign in to comment.