Fix a bug that kept us from adding the markdown attribute to HTML tags that contain valid markdown when parsing HTML#614
Closed
boblail wants to merge 1 commit intogettalong:masterfrom
Conversation
a9e16e7 to
a7b296d
Compare
ashmaroli
reviewed
Jun 24, 2019
lib/kramdown/converter/kramdown.rb
Outdated
| c.type != :html_element && (c.type != :p || !c.options[:transparent]) && | ||
| Element.category(c) == :block | ||
| c.type != :html_element && | ||
| !(c.type == :p && c.options[:transparent] && c.children.none? { |t| ![:entity, :text, :html_element].member?(t.type) }) && |
Contributor
There was a problem hiding this comment.
[:entity, :text, :html_element] is a static array. In order to avoid repeated allocations of this array on multiple iterations, I recommend hoisting the array as a private constant:
TYPES_ARRAY = [:entity, :text, :html_element]
private_constant :TYPES_ARRAY
def convert_html_element(el, opts)
markdown_attr = el.options[:category] == :block && el.children.any? do |c|
...
c.children.none? { |t| !TYPES_ARRAY.include?(t.type) }
...
end
end
Owner
There was a problem hiding this comment.
How did you determine these three element types?
Contributor
Author
There was a problem hiding this comment.
@gettalong, I added element types until the rest of the Kramdown spec suite passed without changes. 😄
I considered
c.children.none? { |t| MARKDOWN_TYPES.member?(t.type) }instead of
c.children.none? { |t| !NON_MARKDOWN_TYPES.member?(t.type) }but I think the list would be longer.
boblail
added a commit
to boblail/kramdown
that referenced
this pull request
Jun 24, 2019
…s that contain valid markdown when parsing HTML You can reproduce the bug like this: ```ruby html = <<~HTML <div> <img src="src.png" alt="alt" /> </div> HTML markdown = Kramdown::Document.new(html, input: :html).to_kramdown html = Kramdown::Document.new(markdown).to_html ``` The bug is that the call to `to_kramdown` converted the IMG tag but **_did not_** add `markdown="1"` to the DIV tag. As of Kramdown 2.1.0, the `markdown` attribute is added if the IMG tag is wrapped in a paragraph; and you can see that when the `markdown` attribute is present, the HTML roundtrips successfully: ```ruby html = <<~HTML <div> <p><img src="src.png" alt="alt" /></p> </div> HTML markdown = Kramdown::Document.new(html, input: :html).to_kramdown html = Kramdown::Document.new(markdown).to_html ```
a7b296d to
ed0791d
Compare
|
Nice bug fix @boblail! :) |
Contributor
Author
|
@gettalong, is this OK to merge? Or did you have any followup questions? |
Owner
|
Thank you for your contribution - I have cherry-picked your commit and adapted it a bit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You can reproduce the bug like this:
The bug is that the call to
to_kramdownconverted the IMG tag but did not addmarkdown="1"to the DIV tag.As of Kramdown 2.1.0, the
markdownattribute is added if the IMG tag is wrapped in a paragraph; and you can see that when themarkdownattribute is present, the HTML roundtrips successfully: