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

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

Open
Thom1729 opened this issue Oct 25, 2021 · 5 comments
Open

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

Thom1729 opened this issue Oct 25, 2021 · 5 comments

Comments

@Thom1729
Copy link

Migrated from gettalong/kramdown#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
Copy link
Contributor

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
Copy link

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;
}
```
</details>
{::options parse_block_html="false" /}

Expected output:

<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
Copy link

mzagaja commented Jul 7, 2022

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
Copy link

Phrogz commented Jul 23, 2022

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
Copy link

Phrogz commented Jul 23, 2022

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"

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

No branches or pull requests

5 participants