311

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?

1

3 Answers 3

520

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I tried it in this gist. Hope it helps. gist.github.com/salmedina/…
Do you know how to get the right syntax highlighting AND the diff highlighting?
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.
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.
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.
|
38

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!

4 Comments

If someone is looking just for the git format, without the colors... stackoverflow.com/a/4857407/3196753
I couldn't really get this to work. Do you happen to know where I could read more?
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"
Maybe this worked in 2017 but it doesn't work now. The GitHub Docs for code blocks points at Linguist as the implementation, where lack of support for both language and diff highlighting is a recent discussion item.
15

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.