问题
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);
回答1:
As you may noticed in Markdown Cheatsheet, GitHub does not show line numbers in code blocks.
回答2:
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.
回答3:
So, you will need to help yourself by adding css to your html page. As a code goes into <pre> </pre>
block in markdown.
You could apply your logic to this block to put line number against each line.
See https://codepen.io/heiswayi/pen/jyKYyg for reference.
回答4:
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>
```
回答5:
Now here is the solution for adding line numbers in Markdown.
https://shd101wyy.github.io/markdown-preview-enhanced/#/markdown-basics?id=line-numbers
You can enable line number for a code block by adding line-numbers class.
来源:https://stackoverflow.com/questions/36122803/how-to-display-line-numbers-using-github-flavored-markdown-code