-
Notifications
You must be signed in to change notification settings - Fork 588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Markdown] add syntax highlighting in code fences for a few languages #1274
Merged
wbond
merged 14 commits into
sublimehq:master
from
STealthy-and-haSTy:markdown_codefences
Apr 15, 2018
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e8c4848
[Markdown] add syntax highlighting in code fences for a few languages
keith-hall c82a614
[Markdown] tweaks to code fence support
keith-hall 48201e5
[Markdown] allow highlighting "js" code fences as JavaScript
keith-hall 3eef4c1
[Markdown] add Rust code fence
keith-hall a86b8bc
[Markdown] add code fence highlighting for bash shell
keith-hall 2d08eda
[Markdown] add code fence highlighting for PHP
keith-hall 4b2868d
[Markdown] add codefence support for more languages
keith-hall 6eb9494
[Markdown] add regexp code block highlighting
keith-hall 1382bc4
[Markdown] scope code fences with GFM suffix
keith-hall fb2266d
Merge branch 'master' into markdown_codefences
keith-hall b75bdd7
[Markdown] don't scope the code fence punctuation as markup.raw
keith-hall a1771a4
[Markdown] add json to syntax highlighted code fence support
keith-hall e90ee37
Merge branch 'master' into markdown_codefences
keith-hall 2816db2
[Markdown] add syntax tests for bash (prototype context in) code fences
keith-hall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,7 +112,8 @@ variables: | |
) | ||
fenced_code_block_start: |- | ||
(?x: | ||
[ ]{0,3} # up to 3 spaces | ||
[ ]* | ||
([ ]{0,3}) # up to 3 spaces | ||
( | ||
`{3,} # 3 or more backticks | ||
(?![^`]*`) # not followed by any more backticks on the same line | ||
|
@@ -121,6 +122,15 @@ variables: | |
(?![^~]*~) # not followed by any more tildas on the same line | ||
) | ||
) | ||
code_fence_escape: |- | ||
(?x: | ||
^ # the beginning of the line | ||
#\1 # whitespace previously captured | ||
#[ ]{0,3} # up to 3 spaces - doesn't have to be the same as the opening fence, but within 3 spaces | ||
[ ]* | ||
(\2) # the backtick/tilde combination that opened the code fence | ||
\s*$ # any amount of whitespace until EOL | ||
) | ||
html_tag_open_commonmark: |- | ||
(?xi: | ||
< | ||
|
@@ -163,6 +173,13 @@ variables: | |
(?i:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul) | ||
(?:\s|$|/?>) | ||
) | ||
# code_fence_escape: |- | ||
# (?x: | ||
# ^ # the beginning of the line | ||
# [ ]{0,3} # up to 3 spaces | ||
# (\1) # the backtick/tilde combination that opened the code fence | ||
# \s*$ # any amount of whitespace until EOL | ||
# ) | ||
contexts: | ||
main: | ||
- match: |- | ||
|
@@ -847,7 +864,7 @@ contexts: | |
- include: block-quote | ||
- match: $ | ||
pop: true | ||
- match: '^(?=\s+{{fenced_code_block_start}})' | ||
- match: '^(?={{fenced_code_block_start}})' | ||
push: | ||
- include: fenced-code-block | ||
- match: '' | ||
|
@@ -881,7 +898,7 @@ contexts: | |
pop: true | ||
list-content: | ||
- meta_content_scope: meta.paragraph.list.markdown | ||
- match: ^\s*(?={{fenced_code_block_start}}) | ||
- match: ^(?={{fenced_code_block_start}}) | ||
push: | ||
- match: $ | ||
pop: true | ||
|
@@ -898,18 +915,285 @@ contexts: | |
- match: $ | ||
pop: true | ||
fenced-code-block: | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:xml)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.xml.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.xml.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:text.xml | ||
embed_scope: markup.raw.code-fence.xml.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.xml.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:sql)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.sql.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.sql.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.sql | ||
embed_scope: markup.raw.code-fence.sql.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.sql.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:python|py)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.python.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.python.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.python | ||
embed_scope: markup.raw.code-fence.python.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.python.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:graphviz)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.graphviz.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.graphviz.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.dot | ||
embed_scope: markup.raw.code-fence.graphviz.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.graphviz.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:javascript|js)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.javascript.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.javascript.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.javascript | ||
embed_scope: markup.raw.code-fence.javascript.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.javascript.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:java)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.java.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.java.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.java | ||
embed_scope: markup.raw.code-fence.java.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.java.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:csharp|c\#|cs)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.csharp.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.csharp.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.cs | ||
embed_scope: markup.raw.code-fence.csharp.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.csharp.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:rust)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.rust.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.rust.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.rust | ||
embed_scope: markup.raw.code-fence.rust.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.rust.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:shell-script|sh|bash|zsh)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.shell-script.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.shell-script.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.shell.bash | ||
embed_scope: markup.raw.code-fence.shell-script.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.shell-script.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:php|inc)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.php.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.php.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.php | ||
embed_scope: markup.raw.code-fence.php.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.php.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:html\+php)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.html-php.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.html-php.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:embedding.php | ||
embed_scope: markup.raw.code-fence.html-php.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.html-php.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:rscript|r|splus)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.r.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.r.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.r | ||
embed_scope: markup.raw.code-fence.r.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.r.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:golang)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.go.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.go.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.go | ||
embed_scope: markup.raw.code-fence.go.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.go.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:ruby|rb|rbx)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.ruby.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.ruby.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.ruby | ||
embed_scope: markup.raw.code-fence.ruby.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.ruby.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:objc|obj-c|objectivec|objective-c)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.objc.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.objc.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.objc | ||
embed_scope: markup.raw.code-fence.objc.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.objc.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:objc\+\+|obj-c\+\+|objectivec\+\+|objective-c\+\+)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.objc++.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.objc++.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.objc++ | ||
embed_scope: markup.raw.code-fence.objc++.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.objc++.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:c)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.c.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.c.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.c | ||
embed_scope: markup.raw.code-fence.c.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.c.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:c\+\+|cpp)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
2: markup.raw.code-fence.c++.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
3: markup.raw.code-fence.c++.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.c++ | ||
embed_scope: markup.raw.code-fence.c++.markdown-gfm | ||
escape: '{{code_fence_escape}}' | ||
escape_captures: | ||
0: markup.raw.code-fence.c++.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
((?i:regexp|regex)) | ||
\s*$ # any whitespace until EOL | ||
captures: | ||
1: markup.raw.code-fence.regexp.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown | ||
2: markup.raw.code-fence.regexp.markdown-gfm constant.other.language-name.markdown | ||
embed: scope:source.regexp | ||
embed_scope: markup.raw.code-fence.regexp.markdown-gfm | ||
escape: '^[ ]{,3}(\1)\s*$' | ||
escape_captures: | ||
0: markup.raw.code-fence.regexp.markdown-gfm | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
- match: |- | ||
(?x) | ||
{{fenced_code_block_start}} | ||
([\w-]*) # any number of word characters or dashes | ||
.*$ # all characters until eol | ||
.*$ # all characters until EOL | ||
captures: | ||
1: punctuation.definition.raw.code-fence.begin.markdown | ||
2: constant.other.language-name.markdown | ||
2: punctuation.definition.raw.code-fence.begin.markdown | ||
3: constant.other.language-name.markdown | ||
push: | ||
- meta_scope: markup.raw.code-fence.markdown-gfm | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the same reason, this should be |
||
- match: '[ ]{,3}\1\s*$' | ||
scope: punctuation.definition.raw.code-fence.end.markdown | ||
- match: '{{code_fence_escape}}' | ||
captures: | ||
1: punctuation.definition.raw.code-fence.end.markdown | ||
pop: true | ||
code-span: | ||
- match: (`+)(?!`) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these captures should be getting the
markup.raw
scope. They aren't part of the "raw" segment since they are not rendered verbatim. (Also, I have dimmed background formarkup.raw
and it would be nice if that started on the first line of the raw block.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the
escape_captures
for0
should be removed.