247

I'm writing documents that should explain code in C# using Markdown.

I use the ```csharp to get csharp highlighting.

I sometimes want to highlight something specific in the code using bold or anything.

I know about <pre> etc... but it takes away my csharp highlighting.

Best case scenario - some way to highlight code in the ```csharp section.

Next best thing - I can write the code as diff - using + and - to highlight stuff, but how do I tell Github to highlight diff syntax with the red and green backcolor?

Is there a way to use both diff and csharp syntax highlighting?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Noam
  • 4,472
  • 5
  • 30
  • 47
  • 1
    If someone else came here looking for `diff` highlighting support in SE, the answer is [no](https://meta.stackexchange.com/questions/286367/is-there-a-language-syntax-highlighting-setting-for-diff). – Scrooge McDuck Aug 10 '21 at 22:49
  • [I opened this discussion on the matter](https://github.com/github/linguist/discussions/5758) – Nino Filiu Feb 02 '22 at 18:01

3 Answers3

426

Github's markdown supports diff when formatting code. For example:

```diff
public class Hello1
{
   public static void Main()
   {
-      System.Console.WriteLine("Hello, World!");
+      System.Console.WriteLine("Rock all night long!");
   }
}
```

Output:

enter image description here

and it should give you the Diff looks you are looking for, highlighting in red what has been removed and in green what has been added.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Salvador Medina
  • 5,833
  • 1
  • 19
  • 19
  • 3
    I tried it in this gist. Hope it helps. https://gist.github.com/salmedina/ad8bea4f46de97ea132f71b0bca73663#file-markdowndiffexample-md – Salvador Medina Dec 27 '16 at 06:03
  • Wow! My bad. I was searching for "diff syntax highlighting in stackoverflow code blocks" and was lead here. Yes! it works on github and that's why I was hoping it would work on stackoverflow. Alas, it does not and it appears there is not enough interest in making it work. http://meta.stackoverflow.com/questions/272207/add-syntax-highlighting-language-for-diff – Bruno Bronosky Dec 27 '16 at 15:08
  • 130
    Do you know how to get the right syntax highlighting AND the diff highlighting? – math2001 Dec 31 '16 at 03:17
  • 2
    It doesn't seem to be supported as of now. Here is a cheatsheet of what is supported. https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code. Hope this helps. – Salvador Medina Jan 01 '17 at 19:20
  • @math2001 have you find anything to do that? both syntax highlighting and diff? – Ali Ghanavatian May 08 '17 at 13:02
  • 1
    Unfortunately, no... http://stackoverflow.com/questions/41405248/syntax-highlighting-combine-diff-and-xxx – math2001 May 15 '17 at 02:54
  • 8
    Tip: in order to easily generate these diffs you can do `git diff filename_with_diffs.txt > diffs.md` and then you only need to add the `\`\`\`diff` at the start and a `\`\`\`` at the end in the `diffs.md`file. – Roald Aug 22 '20 at 06:45
  • 2
    That's a hot ‍ tip. The only thing missing is how to avoid all of the extra junk on top and **just** get the diffs. – CodeFinity Jul 29 '21 at 02:59
29

Salvador's response is correct, however, I found out that you should add the diff header to the code snippet in order to highlight it:

``` diff
diff --git a/filea.extension b/fileb.extension
index d28nd309d..b3nu834uj 111111
--- a/filea.extension
+++ b/fileb.extension
@@ -1,6 +1,6 @@
-oldLine
+newLine
```

I hope that helps!

Fdiazreal
  • 785
  • 1
  • 8
  • 13
  • If someone is looking just for the git format, without the colors... https://stackoverflow.com/a/4857407/3196753 – tresf Mar 05 '18 at 06:54
  • 3
    I couldn't really get this to work. Do you happen to know where I could read more? – rmobis Jan 24 '21 at 02:44
3

Try this:

  1. Just add ```diff on start, and ``` on end.
  2. Removed line, put a - on start
  3. Added line, put a + on start

The final you will get some like this:

-$a = 14;
+$a = 12;

-function myTest()
+function test()

enter image description here

Blockquote [Source](https://github.com/forem/forem/issues/2087#issuecomment-1203089191

)

DEV Tiago França
  • 1,271
  • 9
  • 9