How to display line numbers using GitHub Flavored Markdown "code"?

Viewed 56639

I know I can use "code" in GitHub Flavored Markdown to highlight a code snippet. But I am not able to display line numbers for a snippet. Is there a way to do so?

```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```

I want a line number to be put at the beginning of each line, like this:

1    var s = "JavaScript syntax highlighting";
2    alert(s);
8 Answers

As a hack, you could save a pic of your code at https://carbon.now.sh and post it; they support line numbers as an option.

You can get something similar that you need using awk '{printf("% 4d %s\n", NR, $0)}' StartDsl.scala where StartDsl.scala is your source code file. Paste the result between

```scala
<your code here>
```

Although it is not available in GitHub as the question asks, I have discovered today if you add an = sign after the opening line, on some Markdown editors, it gives the desired result.

eg:

```javascript=
var s = "JavaScript syntax highlighting";
alert(s);
```

This works on Markdown editors such as HackMD

See your example on HackMD

I use RStudio with RMarkdown to render my Markdown (.md) files. It works great. Using the RMarkdown, the specification is made this way:

```{.javascript .numberLines .lineAnchors}
var s = "JavaScript syntax highlighting";
alert(s);
```

Yes, there are many markdown editors available and I'm not sure that this will work with all the editors, but RStudio/RMarkdown is a really great tool, which I use since a long time ago (IMHO).

Just add an = after the language you choose !

```java=
java code exemple:
int i = 5
```java=
Related