252

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?

2

3 Answers 3

431

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.

8
  • 3
    I tried it in this gist. Hope it helps. gist.github.com/salmedina/… Dec 27, 2016 at 6:03
  • 136
    Do you know how to get the right syntax highlighting AND the diff highlighting?
    – math2001
    Dec 31, 2016 at 3:17
  • 2
    It doesn't seem to be supported as of now. Here is a cheatsheet of what is supported. github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code. Hope this helps. Jan 1, 2017 at 19:20
  • 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.mdfile.
    – Roald
    Aug 22, 2020 at 6: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, 2021 at 2: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!

3
  • If someone is looking just for the git format, without the colors... stackoverflow.com/a/4857407/3196753
    – tresf
    Mar 5, 2018 at 6:54
  • 4
    I couldn't really get this to work. Do you happen to know where I could read more?
    – rmobis
    Jan 24, 2021 at 2:44
  • I can't get this to work either. Mayb eI'm doing it wrong? diff diff --git a/filea.js b/fileb.js index d28nd309d..b3nu834uj 111111 --- a/filea.js +++ b/fileb.js @@ -1,6 +1,6 @@ -export let tada = "hello world" +export const tada = "Hello World"
    – Snailedlt
    Sep 26 at 14:12
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

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.