0

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}
masaino
  • 71
  • 4

1 Answers1

1

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. e.g.

    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.

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • Thank you engineersmnky for your explanation. I understand that GFM code block can not simply revert back... – masaino Jan 05 '23 at 06:29
  • @masaino not sure I understand the comment the output for the 2 examples are identical and both produce valid Kramdown syntax. – engineersmnky Jan 05 '23 at 13:54
  • engineersmnky - thank you for your comments. Sorry, my explanation was not enough. I have many GFM files and wanted to parse them with Kramdown as it can manipulate document AST. After doing something on the doc, I wanted to revert back to the original format including `` ``` `` for code block for further process. Three spaces before each code line means code block but I wanted to have exactly the original text. – masaino Jan 06 '23 at 02:41