Line Highlighting on Code Fences
See original GitHub issueI like to add line highlights to my code fences using the syntax described here.
This breaks the highlighter with language-markdown. I’ve solved it by replacing the code fence “begin” regex in grammars/language-markdown.json
. e.g. for JavaScript:
{
- "begin": "^\\s*([`~]{3,})\\s*(\\{?)((?:\\.?)(?:javascript|js|jsx))(?=( |$))\\s*([^`\\}]*)(\\}?)$",
+ "begin": "^\\s*([`~]{3,})\\s*(\\{?)((?:\\.?)(?:javascript|js|jsx)(?:\\{[0-9,\\-\\s]*\\})?)(?=( |$))\\s*([^`\\}]*)(\\}?)$",
"beginCaptures": {
"1": {
"name": "punctuation.md"
},
"2": {
"name": "punctuation.md"
},
"3": {
"name": "language.constant.md"
},
"5": {
"patterns": [
{
"include": "#special-attribute-elements"
}
]
},
"6": {
"name": "punctuation.md"
}
},
Is this the best way to handle this? If so, I can submit a PR with these regexes if you have any interest in supporting this line highlights syntax. It’s not really part of any markdown standard, and I’m not sure how widespread this practice is, so I understand if it’s more trouble than it’s worth!
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Adding Line Highlights to Markdown Code Fences
One thing I needed recently was an unobtrusive way to highlight lines of code, while keeping it readable (and of course, inside Markdown)....
Read more >Provide hl_lines option in code fences - support - HUGO
I'm aware that Hugo allows the hl_lines option to be passed to Pygments using this syntax: {{< highlight js "linenos=inline,hl_lines=2 3" > ...
Read more >Extended Syntax - Markdown Guide
Many Markdown processors support syntax highlighting for fenced code blocks. This feature allows you to add color highlighting for whatever language your code...
Read more >Markdown fences / code blocks background highlighting in VS ...
In Sublime Text, the extension MarkdownEditing has an option to Set Syntax: Markdown GFM . This causes the markdown code fences to have...
Read more >Code with line numbers | Just the Docs - GitHub Pages
1 or earlier) for line numbers from highlighted code – both when using Kramdown code fences and when using Liquid highlight tags. To...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@eloquently See the commit above. This fixes your issue, but I’ve chosen not to apply any scopes to the line-numbers per se. I think it’s rather premature and as of yet unnecessary to make a decision in that area. I’m still working on a few other changes in the dev branch, but expect this change to be part of the next update to the package. I’m marking this as closed, because I think this solution is at least good enough.
@zpao Thanks for your input!
I think your last remark about allowing anything in the info-string is what I’m probably going for. I saw some interesting ideas about the
{key=value}
attribute syntax on talk.commonmark.org (wasn’t aware of that, thanks) too.