kramdown / parser-gfm

kramdown-parser-gfm provides a kramdown parser for the GFM dialect of Markdown
Other
55 stars 14 forks source link

Backtick-fenced code blocks are not syntax-highlighted #32

Open Thom1729 opened 2 years ago

Thom1729 commented 2 years ago

Migrated from https://github.com/gettalong/kramdown/issues/733.

echo '```python\nfoo()\n```' | \
kramdown \
  --extension=parser-gfm \
  --syntax-highlighter=rouge \
  --syntax-highlighter-opts "{}"

Result:

<p><code>python
foo()
</code></p>

echo '~~~python\nfoo()\n~~~' | \
kramdown \
  --extension=parser-gfm \
  --syntax-highlighter=rouge \
  --syntax-highlighter-opts "{}"

Result:

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">foo</span><span class="p">()</span>
</code></pre></div></div>
ashmaroli commented 2 years ago

Works as expected via the Ruby API.. Perhaps a bug with the kramdown executable or input string. Test:

# test.rb

require 'kramdown'

def convert(contents)
  puts "Kramdown: #{Kramdown::VERSION}"
  puts "Contents: #{contents.inspect}"
  puts "  Result: #{Kramdown::Document.new(contents, input: 'GFM').to_html}"
  puts
end

convert <<~TXT
```python\nfoo()\n```
TXT

convert <<~TXT
~~~python\nfoo()\n~~~
TXT
Kramdown: 2.3.1
Contents: "```python\nfoo()\n```\n"
  Result: <div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">Foo</span><span class="p">()</span>
</code></pre></div></div>

Kramdown: 2.3.1
Contents: "~~~python\nfoo()\n~~~\n"
  Result: <div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">Foo</span><span class="p">()</span>
</code></pre></div></div>
takase1121 commented 2 years ago

Sorry for hijacking this issue, but I do have a similiar issue with this but it only occurs in <details> block with parse_block_html enabled:

example input:

{::options parse_block_html="true" /}
<details>
<summary markdown='span'>If you insist, here's the details!</summary>
A command is roughly defined as:

```c
struct command_t {
  command_type_t type;
  RenRect bounding_rectangle;
  RenColor color;
  char *text;
}

{::options parse_block_html="false" /}


Expected output:
```html
<details>
  <summary>If you insist, here’s the details!</summary>

  <p>A command is roughly defined as:</p>

  <div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">struct</span> <span class="n">command_t</span> <span class="p">{</span>
  <span class="n">command_type_t</span> <span class="n">type</span><span class="p">;</span>
  <span class="n">RenRect</span> <span class="n">bounding_rectangle</span><span class="p">;</span>
  <span class="n">RenColor</span> <span class="n">color</span><span class="p">;</span>
  <span class="kt">char</span> <span class="o">*</span><span class="n">text</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>  </div>
</details>

Obtained output:

<details>
  <summary>If you insist, here’s the details!</summary>

  <p>A command is roughly defined as:</p>

  <p><code>c
struct command_t {
  command_type_t type;
  RenRect bounding_rectangle;
  RenColor color;
  char *text;
}
</code></p>
</details>

The expected output is actually obtained by switching from ```c to ~~~ c (I guess it does count as issue fixed) but if I were to write fenced code block kramdown's way I wouldn't install kramdown-parser-gfm and use backticks :)

mzagaja commented 2 years ago

I have this issue as well. Test case:

curl 'https://raw.githubusercontent.com/fastruby/fast-ruby/master/README.md' | kramdown -x parser-gfm --gfm_quirks no_auto_typographic

Tried removing the ruby after the backticks without luck. Not sure if there is anything else I can do to help debug here, but a bummer since I was hoping to use Kramdown to convert a bunch of GitHub flavored Markdown with code to HTML for Dash. So happy to help test further if there is anything I can do.

Phrogz commented 2 years ago

Here's what I'm up against; should this not work?

% cat test.rb
require 'kramdown'
s = "```ruby\np :hello\n```"
p RUBY_VERSION, Kramdown::VERSION
p s, Kramdown::Document.new(s).to_html

% ruby test.rb 
"3.1.2"
"2.4.0"
"```ruby\np :hello\n```"
"<p><code>ruby\np :hello\n</code></p>\n"

And if I try to add input:'GFM':

% cat test.rb 
require 'kramdown'
s = "```ruby\np :hello\n```"
p RUBY_VERSION, Kramdown::VERSION
p s, Kramdown::Document.new(s, input:'GFM').to_html

% ruby test.rb
"3.1.2"
"2.4.0"
/usr/local/lib/ruby/gems/3.1.0/gems/kramdown-2.4.0/lib/kramdown/document.rb:104:in `initialize': kramdown has no parser to handle the specified input format: GFM (Kramdown::Error)
    from test.rb:4:in `new'
    from test.rb:4:in `<main>'

It does work as hoped if I switch the fence from backticks to tildes:

 % ruby test.rb 
"3.1.2"
"2.4.0"
"~~~ruby\np :hello\n~~~"
"<pre><code class=\"language-ruby\">p :hello\n</code></pre>\n"
Phrogz commented 2 years ago

Ah! I didn't see which repo I was commenting in. Unlike the comment by @ashmaroli, it works if you install this gem (kramdown-parser-gfm), and require it, and use input:'GFM':

phrogz@GrayBook _src % cat test.rb 
require 'kramdown'
require 'kramdown-parser-gfm'
s = "```ruby\np :hello\n```"
p RUBY_VERSION, Kramdown::VERSION
p s, Kramdown::Document.new(s, input:'GFM').to_html
phrogz@GrayBook _src % ruby test.rb 
"3.1.2"
"2.4.0"
"```ruby\np :hello\n```"
"<pre><code class=\"language-ruby\">p :hello\n</code></pre>\n"