Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write diff code with syntax highlight in Github

Github supports syntax highlight as follows:

```javascript
let message = 'hello world!'
```

And it supports diff as follows: (but WITHOUT syntax highlight)

```diff
-let message = 'hello world!'
+let message = 'hello stackoverflow!'
```

How can I get both 'syntax hightlight' AND 'diff' ?

like image 888
Eason Wang Avatar asked Nov 05 '18 01:11

Eason Wang


People also ask

How do I highlight code in github?

To link to multiple lines of highlighted code, select your first line of code and then CTRL+SHIFT click on the last line of code you want to highlight.

How do you highlight code in Markdown?

To highlight code, write the name of the language the code is written in after the initial triple backticks. In the above example, the code is written in JavaScript, and hence javascript is added after triple backticks.

Can you highlight in Markdown?

Many Markdown processors support syntax highlighting for fenced code blocks. This feature allows you to add color highlighting for whatever language your code was written in. To add syntax highlighting, specify a language next to the backticks before the fenced code block.

How do I create a code snippet in github?

To add a code snippet: select the lines you want to reference, open the inline toolbar, click Copy permalink, and paste it anywhere.


2 Answers

No, this is not a supported feature at this time.

GitHub documents their processing of lightweight markup languages (including Markdown, among others) in github/markup. Note step 3:

  1. Syntax highlighting is performed on code blocks. See github/linguist for more information about syntax highlighting.

If we follow that link, we find a list of grammars that Linguist uses to provide syntax highlighting on GitHub. Linguist can only apply one of the grammars in that list to a block of code at a time. Of course, one of the grammars is Diff. However, that grammar knows nothing about the language of code being diffed, so you don't get syntax highlighting of that.

Of course, there are other languages which are often combined. For example, HTML is often included in a templating language. Therefore, in addition to the HTML grammar, we also find grammars for HTML+Django, HTML+ECR HTML+EEX, HTML+ERB, and HTML+PHP. In each case, the single grammar is aware of two languages. Both the specific templating language and the HTML which is interspersed within the template.

To accomplish the same thing with a diff, you would need a separate "diff" grammar for every single language listed. In other words, the number of grammars would double. Of course, a way to avoid this might be to treat diff differently. When diff is specified, they could run the block through the syntax highlighter twice, once for diff and once for the source language. However, at least when processing code blocks in lightweight markup languages, they have not implemented such a feature.

And if they ever were to implement such a feature in the future, it would likely be more complicated that simply running the code block through twice. After all, every line of the diff has diff specific content which would confuse the other language grammar. Therefore, every grammar would need to be diff aware, or each line would need to be fed to the grammar separately with the diff parts removed. The problem with the later is that the grammar would not have the context of each line and is more likely to get things wrong. Whether such a solution is possible is outside this cope of this answer, but the point is that it is reasonable to expect that such a feature would be much lower priority to support due to the complexity involved.

So why does GitHub do syntax highlighting in other places on its website? Because, in those cases, it has access to the two source files being diffed and it generates the diff itself. Each source is first highlighted (avoiding the complexity mentioned above), then the diff is created from the two highlighted source files. However, a diff included in a Markdown code block is already a diff when GitHub first sees it. There is no way for them to highlight the pre-diff code first. In other words, the process they currently use would not be transferable to supporting the requested feature.

like image 149
Waylan Avatar answered Oct 19 '22 08:10

Waylan


You would need to post-process the output of the git diff in order to add syntax highlighting for the right language of the file being diff'ed.

But since you are asking for GitHub, that post-processing is not in your control, and is not provided by GitHub at the moment in its GFM (GitHub Flavored Markdown Spec).

It is supported for source files, in a regular diff like this one or in a PR: GitHub does the syntax highlighting of the two versions of the file, and then computes the diff.

It is not supported in a regular markdown fenced code block, where the +/- of a diff would throw off the syntax highlighting engine, considering there is no "diff" operation done here (just the writer trying to add diff +/- symbols)

like image 45
VonC Avatar answered Oct 19 '22 08:10

VonC