Question

I want to include code in a markdown gist on github, and cannot work out how to do syntax highlighting.

github flavoured markdown - e.g.

```php
    Class::function($param);
```

would highlight the syntax as php in an issue, for instance, but it seems not in a gist.

Was it helpful?

Solution

Fenced code blocks do work in Markdown Gists, and in fact your code is being rendered that way. If you inspect the blocks you'll see that they are contained in divs with class="highlight highlight-PHP".

The problem is that PHP code is only recognized for highlighting by GFM if it includes the <?php delimiter (much like PHP code only runs inside a <?php block). Add this to the top of each PHP code block and you should be good to go, e.g.:

...

```php
<?php
class GO_Example_Model_Thing extends GO_Base_Db_ActiveRecord {
    ...

OTHER TIPS

Use this HTML comment tag before the block:

     
<!-- language: php -->

then your block of code and the rest of que answer/question:

     
    Class::function($param);
    // more code...

Important rules:

  • Don't indent the HTML comment.
  • Enter a new empty line after the comment.
  • If don't work insert a new empty line before the comment and the indented code too.

You can check the Stack Overflow's Markdown help and a more elaborated meta's answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top