Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use syntax highlighting in PHP within a markdown github gist?

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.

like image 240
derekdreery Avatar asked Feb 12 '14 13:02

derekdreery


People also ask

Should you use GitHub Gists for syntax highlighting in WordPress?

The problem with the WordPress syntax highlighting plugins is that they load a lot of css and java scripts and wreck your site’s speed scores. As an alternative, I started using Github Gists to show code examples. But I didn’t like the default Gist syntax highlighting. I needed Github Gists custom syntax highlighting.

How do I enable syntax highlighting in code blocks on GitHub?

If you are frequently editing code snippets and tables, you may benefit from enabling a fixed-width font in all comment fields on GitHub. For more information, see " Enabling fixed-width fonts in the editor ." You can add an optional language identifier to enable syntax highlighting in your fenced code block.

How to get HTML highlighted in PHP?

We can use Heredoc syntax to get HTML highlighted inside PHP. Heredoc in PHP is a way to write large blocks of strings inside PHP, without the classic single quote, double quotes delimiters. It relies on <<< and an identifier that will also mark the end of the string.

How can I create diagrams in Markdown?

You can also use code blocks to create diagrams in Markdown. GitHub supports Mermaid, GeoJSON, TopoJSON, and ASCII STL syntax. For more information, see " Creating diagrams ." Help us make these docs great!


1 Answers

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 {
    ...
like image 157
Chris Avatar answered Oct 03 '22 00:10

Chris