Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird backslash insertion in kramdown writer #751

Closed
cabo opened this issue Mar 29, 2022 · 11 comments
Closed

Weird backslash insertion in kramdown writer #751

cabo opened this issue Mar 29, 2022 · 11 comments
Assignees
Labels

Comments

@cabo
Copy link
Contributor

cabo commented Mar 29, 2022

No description provided.

@cabo
Copy link
Contributor Author

cabo commented Mar 29, 2022

$ kramdown -o kramdown << HERE
AA: foo

*[AA]: bar
HERE

AA\: foo

*[AA]: bar

I can't find the code that does this backslash insertion.
The inserted backslash is stable over further kramdown -o kramdown sequences, but other processors don't like a backslash there (i.e., show it).

@gettalong
Copy link
Owner

The backslash insert is done here: https://github.com/gettalong/kramdown/blob/master/lib/kramdown/converter/kramdown.rb#L78-L80

I think the code should actually read:

          end.gsub(/\s+/, ' ').gsub(ESCAPED_CHAR_RE) do
            $1 || !opts[:prev] || opts[:prev].type == :br ? "\\#{$1 || $2}" : $&
          end

But have to think a bit more about it.

@gettalong gettalong self-assigned this Mar 29, 2022
@gettalong gettalong added the bug label Mar 29, 2022
@cabo
Copy link
Contributor Author

cabo commented Mar 30, 2022

Ah. I now understand the bug.

$ kramdown -o kramdown <<HERE
a:

*a*:

a*:*
HERE

➔

a:

*a*\:

a*\:*

So the code is trying to protect against spuriously generating definition lists?
But ^[ ]{0,3}(:) is matching against the text item, so starting a new text item with a ":" triggers the backslash.
Instead, it needs to see whether the text item starting with a ":" lands on a new line on the output.

@cabo
Copy link
Contributor Author

cabo commented Mar 30, 2022

Couldn't a ":" start a new line in convert_p with :line_width set?

Yep:

kramdown -o kramdown <<HERE | kramdown
aa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa : aa aaaa aaaa aaaa
HERE

➔ 

<dl>
  <dt>aa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa</dt>
  <dd>aa aaaa aaaa aaaa</dd>
</dl>

It's really not that easy to generate markdown...

@cabo
Copy link
Contributor Author

cabo commented Mar 30, 2022

I'd probably do this two-pass:

  1. generate markdown, but output a special sentinel instead of : for the dt
    • convert all leading : to \: and
    • convert all sentinels to :

@gettalong
Copy link
Owner

Thanks for the second test case which needed another fix. I will push the commit with the fix later (when it is actually committed and tested ;-)

@gettalong
Copy link
Owner

@cabo The latest commit contains the fixes for the bugs.

@cabo
Copy link
Contributor Author

cabo commented Mar 31, 2022

I can report that the fix works for my use case. Thank you!

@cabo
Copy link
Contributor Author

cabo commented Apr 21, 2022

Now what I need is a release so this bug fix trickles into a few production servers...

@gettalong
Copy link
Owner

@cabo Will do when I have time, probably on the weekend

@gettalong
Copy link
Owner

@cabo Release follows shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants