简体   繁体   中英

Ruby Kramdown breaks code block and table in a markdown

It looks that Kramdown changes the delimiter of a code block from ``` to ` when I use to_kramdown . The minimum code is below.

require 'kramdown'

code = <<END
```rb
num = 0
while num < 2 do
   print("num = ", num)
end
print("End")
```
END

puts code #1

doc = Kramdown::Document.new(code)

puts doc.to_kramdown #2

This gives you:

```rb
num = 0
while num < 2 do
   print("num = ", num)
end
print("End")
```

`rb
num = 0
while num < 2 do
   print("num = ", num)
end
print("End")
`

How can I restore the code block with ``` ?

I checked KD:Documnt object and it has :codespan_delimiter=>"```" . Is there any way to use it when I restore?

<KD:Document: options={:template=>"", :auto_ids=>true, :auto_id_stripping=>false, :auto_id_prefix=>"", :transliterated_header_ids=>false, :parse_block_html=>false, :parse_span_html=>true, :html_to_native=>false, :link_defs=>{}, :footnote_nr=>1, :entity_output=>:as_char, :toc_levels=>[1, 2, 3, 4, 5, 6], :line_width=>72, :latex_headers=>["section", "subsection", "subsubsection", "paragraph", "subparagraph", "subparagraph"], :smart_quotes=>["lsquo", "rsquo", "ldquo", "rdquo"], :typographic_symbols=>{}, :remove_block_html_tags=>true, :remove_span_html_tags=>false, :header_offset=>0, :syntax_highlighter=>:rouge, :syntax_highlighter_opts=>{}, :math_engine=>:mathjax, :math_engine_opts=>{}, :footnote_backlink=>"&#8617;", :footnote_backlink_inline=>false, :footnote_prefix=>"", :remove_line_breaks_for_cjk=>false, :forbidden_inline_options=>[:template], :list_indent=>2} root=<kd:root options={:encoding=>#<Encoding:UTF-8>, :location=>1, :options=>{}, :abbrev_defs=>{}, :abbrev_attr=>{}, :footnote_count=>0} children=[<kd:p options={:location=>1} children=[<kd:codespan value="rb\nnum = 0\nwhile num < 2 do\n   print(\"num = \", num)\nend\nprint(\"End\")\n" options={:codespan_delimiter=>"```", :location=>1}>]>]> warnings=[]>

I also tried Kramdown GFM Parser but it converts the code block differently as below.

    num = 0
    while num < 2 do
       print("num = ", num)
    end
    print("End")
{: .language-rb}

Per the Kramdown spec:

Code Span is for Inline Code and differs from Code Blocks .

The flavor you are currently using for code blocks ( ``` ) is Github Flavored Markdown which is why the GFM Parser/converter works. eg

    num = 0
    while num < 2 do
       print("num = ", num)
    end
    print("End")
{: .language-rb}

Is the correct output because 4 spaces is also valid Code Block syntax.

If you want to use the kramdown gem you will have to change this to:

require 'kramdown'

code = <<END
~~~ruby
num = 0
while num < 2 do
   print("num = ", num)
end
print("End")
~~~
END

doc = Kramdown::Document.new(code)
doc.to_kramdown

In which case the output is identical to the GFM Parser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM