Ruby Kramdown在markdown中分解代码块和表



当我使用to_kramdown时,看起来Kramdown将代码块的分隔符从```更改为`。最小代码如下:

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

这给你:

```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")
`

如何用```恢复代码块?

我检查KD:Documnt对象,它有:codespan_delimiter=>"```"。当我恢复时,有什么方法可以使用它吗?

<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="rbnnum = 0nwhile num < 2 don   print("num = ", num)nendnprint("End")n" options={:codespan_delimiter=>"```", :location=>1}>]>]> warnings=[]>

我也尝试过Kramdown GFM Parser,但它转换代码块的方式不同,如下所示。

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

根据Kramdown规范:

  • 反引号(`)用于内联代码
  • tildas~~~用于代码块。

Code Span用于内联代码,与代码块。

你目前使用的代码块(```)的味道是Github风味Markdown,这就是为什么GFM解析器/转换器工作。例如

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

是正确的输出,因为4个空格也是有效的代码块语法。

如果你想使用kramdowngem,你必须将其更改为:

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

在这种情况下输出与GFM Parser相同。

最新更新