forked from REditorSupport/sublime-ide-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
R Markdown.sublime-syntax
1824 lines (1801 loc) · 73.5 KB
/
R Markdown.sublime-syntax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%YAML 1.2
---
# http://www.sublimetext.com/docs/3/syntax.html
name: R Markdown
file_extensions:
- rmd
- rmdown
- rmarkdown
- rmarkdn
scope: text.html.markdown.rmarkdown
comment: |-
this definition aims to meet CommonMark specifications http://spec.commonmark.org/
with GitHub Formatted Markdown extensions https://github.github.com/gfm/
and has a few extras like Markdown Extra's footnotes https://michelf.ca/projects/php-markdown/extra/#footnotes
the scope suffix should indicate which flavor of Markdown the feature came from, to help make this syntax definition easier to maintain
variables:
thematic_break: |-
(?x:
[ ]{,3} # between 0 to 3 spaces
(?: # followed by one of the following:
[-](?:[ ]{,2}[-]){2,} # - a dash, followed by the following at least twice: between 0 to 2 spaces followed by a dash
| [*](?:[ ]{,2}[*]){2,} # - a star, followed by the following at least twice: between 0 to 2 spaces followed by a star
| [_](?:[ ]{,2}[_]){2,} # - an underscore, followed by the following at least twice: between 0 to 2 spaces followed by an underscore
)
[ \t]*$ # followed by any number of tabs or spaces, followed by the end of the line
)
block_quote: (?:[ ]{,3}>(?:.|$)) # between 0 and 3 spaces, followed by a greater than sign, followed by any character or the end of the line
atx_heading: (?:[#]{1,6}\s*) # between 1 and 6 hashes, followed by any amount of whitespace
indented_code_block: (?:[ ]{4}|\t) # 4 spaces or a tab
list_item: (?:[ ]{,3}(?=\d+\.|[*+-])\d*([*+.-])\s) # between 0 and 3 spaces, followed by either: at least one integer and a full stop, or (a star, plus or dash), followed by whitespace
escape: '\\[-`*_#+.!(){}\[\]\\>|~]'
backticks: |-
(?x:
(`{4})(?![\s`])(?:[^`]+(?=`)|(?!`{4})`+(?!`))+(`{4})(?!`) # 4 backticks, followed by at least one non whitespace, non backtick character, followed by (less than 4 backticks, or at least one non backtick character) at least once, followed by exactly 4 backticks
| (`{3})(?![\s`])(?:[^`]+(?=`)|(?!`{3})`+(?!`))+(`{3})(?!`) # 3 backticks, followed by at least one non whitespace, non backtick character, followed by (less than 3 backticks, or at least one non backtick character) at least once, followed by exactly 3 backticks
| (`{2})(?![\s`])(?:[^`]+(?=`)|(?!`{2})`+(?!`))+(`{2})(?!`) # 2 backticks, followed by at least one non whitespace, non backtick character, followed by (less than 2 backticks, or at least one non backtick character) at least once, followed by exactly 2 backticks
| (`{1})(?![\s`])(?:[^`]+(?=`)|(?!`{1})`+(?!`))+(`{1})(?!`) # 1 backtick, followed by at least one non whitespace, non backtick character, followed by ( at least one non backtick character) at least once, followed by exactly 1 backtick
)
balance_square_brackets: |-
(?x:
(?:
{{escape}}+ # escape characters
| [^\[\]`\\]+(?=[\[\]`\\]|$) # anything that isn't a square bracket or a backtick or the start of an escape character
| {{backticks}} # inline code
| \[(?: # nested square brackets (one level deep)
[^\[\]`]+(?=[\[\]`]) # anything that isn't a square bracket or a backtick
{{backticks}}? # balanced backticks
)*\] # closing square bracket
)+ # at least one character
)
html_entity: '&([a-zA-Z0-9]+|#\d+|#x\h+);'
skip_html_tags: (?:<[^>]+>)
balance_square_brackets_and_emphasis: |-
(?x:
(?:
{{escape}}+ # escape characters
| [^\[\]`\\_*]+(?=[\[\]`\\_*]|$) # anything that isn't a square bracket, a backtick, the start of an escape character, or an emphasis character
| {{backticks}} # inline code
| \[(?: # nested square brackets (one level deep)
[^\[\]`]+(?=[\[\]`]) # anything that isn't a square bracket or a backtick
{{backticks}}? # balanced backticks
)*\] # closing square bracket
)+ # at least one character
)
balance_square_brackets_pipes_and_emphasis: |-
(?x:
(?:
{{escape}}+ # escape characters
| [^\[\]`\\_*|]+(?=[\[\]`\\_*|]|$) # anything that isn't a square bracket, a backtick, the start of an escape character, or an emphasis character
| {{backticks}} # inline code
| \[(?: # nested square brackets (one level deep)
[^\[\]`]+(?=[\[\]`]) # anything that isn't a square bracket or a backtick
{{backticks}}? # balanced backticks
)*\] # closing square bracket
)+ # at least one character
)
balanced_emphasis: |-
(?x:
\* (?!\*){{balance_square_brackets_and_emphasis}}+\* (?!\*)
| \*\* {{balance_square_brackets_and_emphasis}}+\*\*
| _ (?!_) {{balance_square_brackets_and_emphasis}}+_ (?!_)
| __ {{balance_square_brackets_and_emphasis}}+__
)
balanced_table_cell: |- # Pipes inside other inline spans (such as emphasis, code, etc.) will not break a cell, emphasis in table cells can't span multiple lines
(?x:
(?:
{{balance_square_brackets_pipes_and_emphasis}}
| {{balanced_emphasis}}
)+ # at least one character
)
table_first_row: |-
(?x:
(?:{{balanced_table_cell}}?\|){2} # at least 2 non-escaped pipe chars on the line
| (?!\s+\|){{balanced_table_cell}}\|(?!\s+$) # something other than whitespace followed by a pipe char, followed by something other than whitespace and the end of the line
)
fenced_code_block_start: |-
(?x:
([ \t]*)
(
(`){3,} # 3 or more backticks
(?![^`]*`) # not followed by any more backticks on the same line
| # or
(~){3,} # 3 or more tildas
(?![^~]*~) # not followed by any more tildas on the same line
)
\s* # allow for whitespace between code block start and info string
)
fenced_code_block_trailing_infostring_characters: |-
(?x:
(
\s* # any whitespace, or ..
|
\s[^`]* # any characters (except backticks), separated by whitespace ...
)
$\n? # ... until EOL
)
code_fence_escape: |-
(?x:
^ # the beginning of the line
[ \t]*
(
\2 # the backtick/tilde combination that opened the code fence
(?:\3|\4)* # plus optional additional closing characters
)
\s*$ # any amount of whitespace until EOL
)
html_tag_open_commonmark: |-
(?xi:
<
[a-z] # A tag name consists of an ASCII letter
[a-z0-9-]* # followed by zero or more ASCII letters, digits, or hyphens (-)
(?: # An attribute consists of whitespace, an attribute name, and an optional attribute value specification
\s+
[a-z_:] # An attribute name consists of an ASCII letter, _, or :
[a-z0-9_.:-]* # followed by zero or more ASCII letters, digits, _, ., :, or -
(?: # An attribute value specification consists of optional whitespace, a = character, optional whitespace, and an attribute value
\s*
=
\s*
(?:
[^ @'=<>`]+ # An unquoted attribute value is a nonempty string of characters not including spaces, ", ', =, <, >, or `
| '[^']*' # A single-quoted attribute value consists of ', zero or more characters not including ', and a final '
| "[^"]*" # A double-quoted attribute value consists of ", zero or more characters not including ", and a final "
)
)?
)*
\s*
/?
>
)
html_tag_close_commonmark: |-
(?xi:
</
[a-z] # A tag name consists of an ASCII letter
[a-z0-9-]* # followed by zero or more ASCII letters, digits, or hyphens (-)
\s*
>
)
html_tag_block_end_at_close_tag: |-
(?xi:
(script|style|pre)\b
)
html_tag_block_end_at_blank_line: |-
(?x:
/?
(?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|$|/?>)
)
ascii_space: '\t\n\f '
tag_attribute_name_start: (?=[^{{ascii_space}}=/>}])
tag_attribute_name_break: (?=[{{ascii_space}}=/>}])
tag_unquoted_attribute_start: (?=[^{{ascii_space}}=/>}])
tag_unquoted_attribute_break: (?=[{{ascii_space}}}]|/?>)
# 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: (?=^\s*(?:---|\+\+\+)\s*$)
push: front-matter
- match: ""
set: body
front-matter:
- match: ^\s*(---)\s*$
captures:
1: punctuation.definition.frontmatter
embed: scope:source.yaml
embed_scope: markup.raw.yaml.front-matter
escape: ^\s*(---)\s*$
escape_captures:
1: punctuation.definition.frontmatter
- match: ^\s*(\+\+\+)\s*$
captures:
1: punctuation.definition.frontmatter
embed: scope:source.toml
embed_scope: markup.raw.toml.front-matter
escape: ^\s*(\+\+\+)\s*$
escape_captures:
1: punctuation.definition.frontmatter
- match: ""
pop: true
set: body
body:
- match: |-
(?x)^
(?= {{block_quote}}
| {{indented_code_block}}(?!$)
| {{atx_heading}}
| {{thematic_break}}
| {{table_first_row}}
)
comment: |
We could also use an empty end match and set
applyEndPatternLast, but then we must be sure that the begin
pattern will only match stuff matched by the sub-patterns.
push:
- meta_scope: meta.block-level.markdown
- include: ligatures
- include: block-quote
- include: indented-code-block
- include: atx-heading
- include: thematic-break
- include: table
- match: ''
pop: true
- match: '^([ ]{0,3})([*+-])(?=\s)'
captures:
1: markup.list.unnumbered.markdown
2: markup.list.unnumbered.bullet.markdown punctuation.definition.list_item.markdown
push:
- meta_content_scope: markup.list.unnumbered.markdown
- match: ^(?=\S)
pop: true
- include: list-paragraph
- match: '^([ ]{0,3})(\d+(\.))(?=\s)'
captures:
1: markup.list.numbered.markdown
2: markup.list.numbered.bullet.markdown
3: punctuation.definition.list_item.markdown
push:
- meta_content_scope: markup.list.numbered.markdown
- match: ^(?=\S)
pop: true
- include: list-paragraph
- match: '^[ ]{0,3}(?=<((?i:pre))\b)'
comment: Markdown formatting is disabled inside block-level tags.
push: [disabled-markdown-pop-at-eol, disable-markdown-pop-at-tag]
- match: '^[ ]{0,3}(?=<{{html_tag_block_end_at_close_tag}})'
comment: Markdown formatting is disabled inside block-level tags.
push: [disabled-markdown-pop-at-eol, disable-markdown-pop-after-tag]
- match: '^[ ]{0,3}(?=<\?)'
comment: Markdown formatting is disabled inside preprocessor instructions.
push: [disabled-markdown-pop-at-eol, disable-markdown-pop-at-php]
- match: '^[ ]{0,3}(?=<!(?:[A-Z]|--))'
comment: Markdown formatting is disabled inside doctype declarations and comments.
push: [disabled-markdown-pop-at-eol, disable-markdown-pop-after-html-doctype]
- match: '^[ ]{0,3}(?=<!\[CDATA\[)'
comment: Markdown formatting is disabled inside CDATA.
push: [disabled-markdown-pop-at-eol, disable-markdown-pop-after-cdata]
- match: '^[ ]{0,3}(?=(?:{{html_tag_open_commonmark}}|{{html_tag_close_commonmark}})\s*$|<{{html_tag_block_end_at_blank_line}})'
comment: Markdown formatting is disabled inside block level tags and if a complete HTML tag is the only thing on the line.
push: [disabled-markdown-pop-at-eol, disable-markdown-pop-at-blank-line]
- match: |-
(?x:
\s* # Leading whitespace
(\[)(\^[^]]*)(\])(:) # Reference name
[ \t]* # Optional whitespace
)
captures:
1: punctuation.definition.constant.begin.markdown
2: entity.name.reference.link.markdown
3: punctuation.definition.constant.end.markdown
4: punctuation.separator.key-value.markdown
push:
- meta_scope: meta.link.reference.def.footnote.markdown-extra
- match: ^(?![ ]{4}|$)
pop: true
- include: inline-bold-italic
- match: |-
(?x:
\s* # Leading whitespace
(\[)([^]]*)(\])(:) # Reference name
[ \t]* # Optional whitespace
(?:
(<)([^>]+)(>) # The url
| (\S+) # The url
)
)
captures:
1: punctuation.definition.constant.begin.markdown
2: entity.name.reference.link.markdown
3: punctuation.definition.constant.end.markdown
4: punctuation.separator.key-value.markdown
5: punctuation.definition.link.begin.markdown
6: markup.underline.link.markdown
7: punctuation.definition.link.end.markdown
8: markup.underline.link.markdown
push: [link-ref-def, after-link-title, link-title]
- match: '^(?=\S)(?![=-]{3,}\s*$)'
push:
- meta_scope: meta.paragraph.markdown
- match: |-
(?x) # pop out of this context when one of the following conditions are met:
^(?:
\s*$ # the line is blank (or only contains whitespace)
| (?=
{{block_quote}} # a block quote begins the line
| [ ]{,3}[*+-][ ] # an unordered list item begins the line
| [ ]{,3}1[.][ ] # an ordered list item with number "1" begins the line
| \# # an ATX heading begins the line
| [ ]{,3}<( # all types of HTML blocks except type 7 may interrupt a paragraph
{{html_tag_block_end_at_close_tag}} # 1
| !-- # 2
| \? # 3
| ![A-Z] # 4
| !\[CDATA\[ # 5
| {{html_tag_block_end_at_blank_line}} # 6
)
)
)
pop: true
- include: inline-bold-italic-linebreak
- include: scope:text.html.basic
- match: '^(={3,})(?=[ \t]*$)'
scope: markup.heading.1.setext.markdown
captures:
1: punctuation.definition.heading.setext.markdown
pop: true
- match: '^(-{3,})(?=[ \t]*$)'
scope: markup.heading.2.setext.markdown
captures:
1: punctuation.definition.heading.setext.markdown
pop: true
link-ref-def:
- meta_scope: meta.link.reference.def.markdown
- match: ''
pop: true
ampersand:
- match: (?!{{html_entity}})&
comment: |
Markdown will convert this for us. We match it so that the
HTML grammar will not mark it up as invalid.
scope: meta.other.valid-ampersand.markdown
ligatures:
- match: '<=>' # <=>
- match: '<[-=<]' # <- <= <<
block-quote:
- match: '[ ]{,3}(>)[ ]?'
comment: |
We terminate the block quote when seeing an empty line, a
separator or a line with leading > characters. The latter is
to “reset” the quote level for quoted lines.
The idea here is to match block level elements first, then once
we have confirmed there are no block level elements left, move to
matching inline markdown. This prevents block level elements being
detected when they shouldn't be.
captures:
1: punctuation.definition.blockquote.markdown
push:
- meta_scope: markup.quote.markdown
- match: |-
(?x)^
(?= \s*$
| {{thematic_break}}
| {{block_quote}}
)
pop: true
- match: |-
(?x)
(?= {{block_quote}}
)
push:
- match: ^
pop: true
- include: block-quote
- match: |-
(?x)
(?= {{indented_code_block}}
| {{atx_heading}}
| {{thematic_break}}
| {{list_item}}
)
push:
- include: indented-code-block
- include: atx-heading
- include: thematic-break
- match: ([ ]{,3})(\d+)(\.)(\s)
captures:
1: markup.list.numbered.markdown
2: markup.list.numbered.bullet.markdown
3: markup.list.numbered.bullet.markdown punctuation.definition.list_item.markdown
4: markup.list.numbered.markdown
push:
- meta_content_scope: markup.list.numbered.markdown meta.paragraph.list.markdown
- include: list-content
- match: ([ ]{,3})([*+-])(\s)
captures:
1: markup.list.unnumbered.markdown
2: markup.list.unnumbered.bullet.markdown punctuation.definition.list_item.markdown
3: markup.list.unnumbered.markdown
push:
- meta_content_scope: markup.list.unnumbered.markdown meta.paragraph.list.markdown
- include: list-content
- match: ^
pop: true
- include: list-paragraph
- match: ''
push:
- match: $|^
pop: true
- include: inline-bold-italic-linebreak
indented-code-block:
- match: '{{indented_code_block}}.*$\n?'
scope: markup.raw.block.markdown
bold:
- include: ligatures
- match: '(\*\*)(\*)(?=\S)(?!\*)'
captures:
1: punctuation.definition.bold.begin.markdown
2: markup.italic.markdown punctuation.definition.italic.begin.markdown
push:
- meta_scope: markup.bold.markdown
- meta_content_scope: markup.italic.markdown
- match: |-
(?x)
[ \t]*\*{4,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+\*(?!\*) # whitespace followed by 1 is also not applicable (but whitespace followed by 2 could be bold punctuation)
| ^\*(?!\*) # emphasis can't be closed at the start of the line
- match: (\*)(\*\*)
captures:
1: markup.italic.markdown punctuation.definition.italic.end.markdown
2: punctuation.definition.bold.end.markdown
pop: true
- match: \*\*
scope: punctuation.definition.bold.end.markdown
set:
- meta_content_scope: markup.italic.markdown
- match: |-
(?x)
[ \t]*\*{3,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+\*\*+ # whitespace followed by 1 is also not applicable (but whitespace followed by 2 could be bold punctuation)
| ^\*\* # emphasis can't be closed at the start of the line
- match: \*
scope: markup.italic.markdown punctuation.definition.italic.end.markdown
pop: true
- include: inline
- include: bold
- include: supersubscript
- include: bold-italic-trailing
- match: \*
scope: punctuation.definition.italic.end.markdown
set:
- meta_content_scope: markup.bold.markdown
- match: |-
(?x)
[ \t]*\*{3,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+\*\*+ # whitespace followed by 1 is also not applicable (but whitespace followed by 2 could be bold punctuation)
| ^\*\* # emphasis can't be closed at the start of the line
- match: \*\*
scope: markup.bold.markdown punctuation.definition.bold.end.markdown
pop: true
- include: inline
- include: italic
- include: supersubscript
- include: bold-italic-trailing
- include: inline
- include: bold-italic-trailing
- match: '\*\*(?=\S)(?!\*\*|\*\s)'
scope: punctuation.definition.bold.begin.markdown
push:
- meta_scope: markup.bold.markdown
- match: |-
(?x)
[ \t]*\*{4,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+\*\*+ # whitespace followed by 2 or more is also not applicable
| ^\*\* # emphasis can't be closed at the start of the line
- match: (?:_)?(\*\*)
captures:
1: punctuation.definition.bold.end.markdown
pop: true
- include: inline
- match: \b_(?=[^\s_])(?=[^*_]*\*\*)
comment: eat the underscore that has no corresponding underscore before the closing bold punctuation on the same line, as it won't be treated as italic by CommonMark
- include: italic
- include: supersubscript
- include: bold-italic-trailing
- match: '\b(__)(_)(?=\S)(?!_)'
captures:
1: punctuation.definition.bold.begin.markdown
2: markup.italic.markdown punctuation.definition.italic.begin.markdown
push:
- meta_scope: markup.bold.markdown
- meta_content_scope: markup.italic.markdown
- match: |-
(?x)
[ \t]*_{4,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+_(?!_) # whitespace followed by 1 is also not applicable (but whitespace followed by 2 could be bold punctuation)
| ^_(?!_) # emphasis can't be closed at the start of the line
- match: (_)(__)\b
captures:
1: markup.italic.markdown punctuation.definition.italic.end.markdown
2: punctuation.definition.bold.end.markdown
pop: true
- match: _\b
scope: punctuation.definition.italic.end.markdown
set:
- meta_content_scope: markup.bold.markdown
- match: |-
(?x)
[ \t]*_{3,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+__+ # whitespace followed by 1 is also not applicable (but whitespace followed by 2 could be bold punctuation)
| ^__ # emphasis can't be closed at the start of the line
- match: __\b
scope: markup.bold.markdown punctuation.definition.bold.end.markdown
pop: true
- include: inline
- include: italic
- include: supersubscript
- include: bold-italic-trailing
- match: __\b
scope: punctuation.definition.bold.end.markdown
set:
- meta_content_scope: markup.italic.markdown
- match: |-
(?x)
[ \t]*_{3,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+__+ # whitespace followed by 1 is also not applicable (but whitespace followed by 2 could be bold punctuation)
| ^__ # emphasis can't be closed at the start of the line
- match: _\b
scope: markup.italic.markdown punctuation.definition.italic.end.markdown
pop: true
- include: inline
- include: bold
- include: supersubscript
- include: bold-italic-trailing
- include: inline
- include: bold-italic-trailing
- match: '\b__(?=\S)(?!_[_\s])'
scope: punctuation.definition.bold.begin.markdown
push:
- meta_scope: markup.bold.markdown
- match: |-
(?x)
[ \t]*_{4,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+__+ # whitespace followed by 2 or more is also not applicable
| ^__ # emphasis can't be closed at the start of the line
- match: (?:\*)?(__\b)
captures:
1: punctuation.definition.bold.end.markdown
pop: true
- include: inline
- match: \*(?=[^\s*])(?=[^*_]*__\b)
comment: eat the asterisk that has no corresponding asterisk before the closing bold punctuation on the same line, as it won't be treated as italic by CommonMark
- include: italic
- include: supersubscript
- include: bold-italic-trailing
bracket:
- match: '<(?![A-Za-z/?!$])'
comment: |
Markdown will convert this for us. We match it so that the
HTML grammar will not mark it up as invalid.
scope: meta.other.valid-bracket.markdown
escape:
- match: '{{escape}}'
scope: constant.character.escape.markdown
atx-heading-terminator:
- match: '[ ]*(#*)[ ]*($\n?)' # \n is optional so ## is matched as end punctuation in new document (at eof)
captures:
1: punctuation.definition.heading.end.markdown
2: meta.whitespace.newline.markdown
pop: true
atx-heading:
- match: '(#)(?!#)\s*(?=\S)'
captures:
1: punctuation.definition.heading.begin.markdown
push:
- meta_scope: markup.heading.1.markdown
- meta_content_scope: entity.name.section.markdown
- include: atx-heading-terminator
- include: inline-bold-italic
- match: '(##)(?!#)\s*(?=\S)'
captures:
1: punctuation.definition.heading.begin.markdown
push:
- meta_scope: markup.heading.2.markdown
- meta_content_scope: entity.name.section.markdown
- include: atx-heading-terminator
- include: inline-bold-italic
- match: '(#{3,6})(?!#)\s*(?=\S)'
captures:
1: punctuation.definition.heading.begin.markdown
push:
- meta_scope: markup.heading.markdown
- meta_content_scope: entity.name.section.markdown
- include: atx-heading-terminator
- include: inline-bold-italic
image-inline:
- match: |-
(?x:
(\!\[) # Images start with ![
(?= {{balance_square_brackets}}? # balanced square brackets, backticks, taking into account escapes etc.
\] # Closing square bracket
[ ]? # Space not allowed, but we check for it anyway to mark it as invalid
\( # Open paren
)
)
captures:
1: meta.image.inline.markdown punctuation.definition.image.begin.markdown
push: [image-inline-attr, image-inline-after-text, image-link-text]
image-link-text:
- meta_content_scope: meta.image.inline.description.markdown
- include: link-text
- match: \]
scope: meta.image.inline.markdown punctuation.definition.image.end.markdown
pop: true
image-inline-after-text:
- match: '([ ]*)(\()' # spaces not allowed before the open paren, but we check for it anyway to mark it as invalid
captures:
1: invalid.illegal.whitespace.markdown
2: punctuation.definition.metadata.begin.markdown
set:
- meta_scope: meta.image.inline.markdown
- match: \)
scope: punctuation.definition.metadata.end.markdown
pop: true
- match: <(?=[^>)]*>)
scope: punctuation.definition.link.begin.markdown
push:
- meta_content_scope: markup.underline.link.image.markdown
- match: \>
scope: punctuation.definition.link.end.markdown
set: link-title
- match: \s+
scope: invalid.illegal.unexpected-whitespace.markdown
- match: (?=\S)
set:
- meta_scope: meta.image.inline.markdown
- match: '[^\s)]+'
scope: markup.underline.link.image.markdown
- match: \)
scope: punctuation.definition.metadata.end.markdown
pop: true
- match: (?!\n)\s+(?!\s*(?:[)('"]|$))
scope: invalid.illegal.unexpected-whitespace.markdown
- match: (?=\s*(?!\)))
push: link-title
image-inline-attr:
- match: '([ ]*)(\{)(?=[^}]*\})'
captures:
1: invalid.illegal.whitespace.markdown
2: punctuation.definition.attributes.begin.markdown
set:
- meta_scope: meta.image.inline.markdown
- include: tag-attributes
- include: immediately-pop
image-ref:
- match: |-
(?x:
(\!\[) # Images start with ![
(?= {{balance_square_brackets}}? # balanced square brackets, backticks, taking into account escapes etc.
\] # Closing square bracket
[ ]? # Space
\[ # [
[^\]]+ # anything other than ]
\] # ]
)
)
captures:
1: meta.image.reference.markdown punctuation.definition.image.begin.markdown
push: [image-ref-attr, image-ref-after-text, image-ref-text]
image-ref-text:
- meta_content_scope: meta.image.reference.description.markdown
- include: link-text
- match: \]
scope: meta.image.reference.markdown punctuation.definition.image.end.markdown
pop: true
image-ref-after-text:
- match: '[ ]?(\[)([^\]]+)(\])'
captures:
1: punctuation.definition.constant.begin.markdown
2: constant.other.reference.link.markdown
3: punctuation.definition.constant.end.markdown
scope: meta.image.reference.markdown
pop: true
image-ref-attr:
- match: '([ ]*)(\{)(?=[^}]*\})'
captures:
1: invalid.illegal.whitespace.markdown
2: punctuation.definition.attributes.begin.markdown
set:
- meta_scope: meta.image.reference.markdown
- include: tag-attributes
- include: immediately-pop
inline:
- include: escape
- include: ampersand
- include: ligatures
- include: bracket
- include: latex-math
- include: raw
- include: image-inline
- include: link-inline
- include: autolink-inet
- include: autolink-email
- include: image-ref
- include: link-ref-literal
- include: link-ref
- include: link-ref-footnote
- include: citations
inline-bold-italic:
- include: inline
- include: bold
- include: italic
- include: strikethrough
- include: supersubscript
inline-bold-italic-linebreak:
- include: inline-bold-italic
- include: hard-line-break
italic:
- match: '\*(?=\S)(?!\*)'
scope: punctuation.definition.italic.begin.markdown
push:
- meta_scope: markup.italic.markdown
- match: |-
(?x)
[ \t]*\*{4,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+\*(?!\*) # whitespace followed by 1 is also not applicable (but whitespace followed by 2 could be bold punctuation)
| ^\*(?!\*) # emphasis can't be closed at the start of the line
- match: \*(?!\*[^*])
scope: punctuation.definition.italic.end.markdown
pop: true
- include: inline
- include: bold
- include: supersubscript
- match: '\*+'
- include: bold-italic-trailing
- match: '\b_(?=\S)(?!_)'
scope: punctuation.definition.italic.begin.markdown
push:
- meta_scope: markup.italic.markdown
- match: |-
(?x)
[ \t]*_{4,} # if there are more than 3 its not applicable to be bold or italic
| [ \t]+_(?!_) # whitespace followed by 1 is also not applicable (but whitespace followed by 2 could be bold punctuation)
| ^_(?!_) # emphasis can't be closed at the start of the line
- match: _\b
scope: punctuation.definition.italic.end.markdown
pop: true
- include: inline
- include: bold
- include: supersubscript
- include: bold-italic-trailing
- match: '[*_]+'
supersubscript:
- match: '(~)(?=([^~ \n]|\\ )+~)'
scope: punctuation.definition.subscript.begin.markdown
push:
- meta_scope: markup.subscript.markdown-pandoc
- match: '~'
scope: punctuation.definition.subscript.end.markdown
pop: true
- include: inline
- include: bold
- include: italic
- include: bold-italic-trailing
- match: '(\^)(?=([^\^ \n]|\\ )+\^)'
scope: punctuation.definition.superscript.begin.markdown
push:
- meta_scope: markup.superscript.markdown-pandoc
- match: \^
scope: punctuation.definition.superscript.end.markdown
pop: true
- include: inline
- include: bold
- include: italic
- include: bold-italic-trailing
strikethrough:
- match: '(~~+)(?=\S)(?!~)'
scope: punctuation.definition.strikethrough.begin.markdown
push:
- meta_scope: markup.strikethrough.markdown-gfm
- match: ~~+
scope: punctuation.definition.strikethrough.end.markdown
pop: true
- include: inline
- include: bold
- include: italic
- include: supersubscript
- include: bold-italic-trailing
bold-italic-trailing:
- include: scope:text.html.basic
- match: ^\s*$\n?
scope: invalid.illegal.non-terminated.bold-italic.markdown
pop: true
- match: '^(?={{list_item}})'
pop: true
- include: hard-line-break
hard-line-break:
- match: '[ ]{2,}$'
scope: meta.hard-line-break.markdown punctuation.definition.hard-line-break.markdown
- match: '(\\)$\n'
scope: meta.hard-line-break.markdown
captures:
1: constant.character.escape.markdown
autolink-email:
- match: '(<)((?:mailto:)?[-+.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(>)'
scope: meta.link.email.lt-gt.markdown
captures:
1: punctuation.definition.link.begin.markdown
2: markup.underline.link.markdown
4: punctuation.definition.link.end.markdown
- match: '[\w.+-]+@[\w-]+(\.((?![._-][\W])[\w_-])+)+(?![_-])'
scope: markup.underline.link.markdown
autolink-inet:
- match: (<)((?:https?|ftp)://.*?)(>)
scope: meta.link.inet.markdown
captures:
1: punctuation.definition.link.begin.markdown
2: markup.underline.link.markdown
3: punctuation.definition.link.end.markdown
- match: (((https|http|ftp)://)|www\.)[\w-]+(\.[\w-]+)+
scope: markup.underline.link.markdown-gfm
push: # After a valid domain, zero or more non-space non-< characters may follow
- match: (?=[?!.,:*_~]*[\s<]) # Trailing punctuation (specifically, ?, !, ., ,, :, *, _, and ~) will not be considered part of the autolink, though they may be included in the interior of the link
pop: true
- match: (?={{html_entity}}[?!.,:*_~]*[\s<]) # If an autolink ends in a semicolon (;), we check to see if it appears to resemble an entity reference; if the preceding text is & followed by one or more alphanumeric characters. If so, it is excluded from the autolink
pop: true
- match: \( # When an autolink ends in ), we scan the entire autolink for the total number of parentheses. If there is a greater number of closing parentheses than opening ones, we don’t consider the last character part of the autolink, in order to facilitate including an autolink inside a parenthesis
push:
- meta_scope: markup.underline.link.markdown-gfm
- match: (?=[?!.,:*_~]*[\s<])
pop: true
- match: \)
pop: true
- match: (?=\)[?!.,:*_~]*[\s<])
pop: true
- match: '[^?!.,:*_~\s<&()]+|\S'
scope: markup.underline.link.markdown-gfm
link-inline:
- match: |-
(?x:
(\[)
(?=
{{balance_square_brackets}}?
\]
[ ]?\(
)
)
captures:
1: meta.link.inline.markdown punctuation.definition.link.begin.markdown
push: [link-inline-attr, link-inline-after-text, link-inline-link-text]
link-inline-after-text:
- match: '([ ]*)(\()' # spaces not allowed before the open paren, but we check for it anyway to mark it as invalid
captures:
1: invalid.illegal.whitespace.markdown
2: punctuation.definition.metadata.begin.markdown
set:
- meta_scope: meta.link.inline.markdown
- match: \)
scope: punctuation.definition.metadata.end.markdown
pop: true
- match: <(?=[^>)]*>)
scope: punctuation.definition.link.begin.markdown
push:
- match: \>
scope: punctuation.definition.link.end.markdown
set: link-title
- match: \s+
scope: invalid.illegal.unexpected-whitespace.markdown
- match: (?=\S)
set:
- meta_scope: meta.link.inline.markdown
- match: '[^\s)]+'
scope: markup.underline.link.markdown
- match: \)
scope: punctuation.definition.metadata.end.markdown
pop: true
- match: (?!\n)\s+(?!\s*(?:[)('"]|$))
scope: invalid.illegal.unexpected-whitespace.markdown
- match: (?=\s*(?!\)))
push: link-title
link-inline-attr:
- match: '([ ]*)(\{)(?=[^}]*\})'
captures:
1: invalid.illegal.whitespace.markdown
2: punctuation.definition.attributes.begin.markdown
set:
- meta_scope: meta.link.inline.markdown
- include: tag-attributes
- include: immediately-pop
link-inline-link-text:
- meta_content_scope: meta.link.inline.description.markdown
- include: link-text-allow-image
- match: \]
scope: meta.link.inline.markdown punctuation.definition.link.end.markdown
pop: true
link-ref:
- match: |-
(?x:
(\[)
(?= {{balance_square_brackets}}? # balanced square brackets, backticks, taking into account escapes etc.
\] # Closing square bracket
[ ]? # Space
\[ # [
[^\]]+ # anything other than ]
\] # ]
)
)
captures:
1: meta.link.reference.markdown punctuation.definition.link.begin.markdown
push: [link-ref-attr, link-ref-after-text, link-ref-link-text]
link-ref-link-text:
- meta_content_scope: meta.link.reference.description.markdown
- include: link-text-allow-image
- match: \]
scope: meta.link.reference.markdown punctuation.definition.link.end.markdown
pop: true
link-ref-after-text:
- match: '[ ]?(\[)([^\]]+)(\])'
captures:
1: punctuation.definition.constant.begin.markdown
2: constant.other.reference.link.markdown
3: punctuation.definition.constant.end.markdown
scope: meta.link.reference.markdown
pop: true
link-ref-attr:
- match: '([ ]*)(\{)(?=[^}]*\})'
captures:
1: invalid.illegal.whitespace.markdown
2: punctuation.definition.attributes.begin.markdown
set:
- meta_scope: meta.link.reference.markdown
- include: tag-attributes
- include: immediately-pop
link-ref-literal:
- match: |-
(?x:
(\[)
(?=
{{balance_square_brackets}}? # balanced square brackets, backticks, taking into account escapes etc.
\] # Closing square bracket
[ ]? # Space
\[ # [
\] # ]
)
)
captures:
1: meta.link.reference.literal.markdown punctuation.definition.link.begin.markdown
push: [link-ref-literal-attr, link-ref-literal-after-text, link-ref-literal-link-text]
link-ref-literal-link-text:
- meta_content_scope: meta.link.reference.literal.description.markdown
- include: link-text-allow-image
- match: \]
scope: meta.link.reference.literal.markdown punctuation.definition.link.end.markdown
pop: true
link-ref-literal-after-text:
- match: '[ ]?(\[)(\])'
scope: meta.link.reference.literal.markdown
captures:
1: punctuation.definition.constant.begin.markdown
2: punctuation.definition.constant.end.markdown
pop: true
link-ref-literal-attr:
- match: '([ ]*)(\{)(?=[^}]*\})'
captures:
1: invalid.illegal.whitespace.markdown
2: punctuation.definition.attributes.begin.markdown
set:
- meta_scope: meta.link.reference.literal.markdown
- include: tag-attributes
- include: immediately-pop
link-ref-footnote:
- match: |-
(?x:
(\[\^)
([^]]+)
(\])
)
captures:
0: meta.link.reference.footnote.markdown-extra
1: punctuation.definition.link.begin.markdown
2: meta.link.reference.literal.footnote-id.markdown
3: punctuation.definition.link.end.markdown
citations:
- match: '(@)([\w:-]+)'
scope: meta.link.reference.markdown-pandoc
captures:
1: constant.other.cite.markdown-pandoc
2: keyword.other.cite.markdown-pandoc
list-paragraph:
- match: '^(?=(?:[ ]{4}|\t){2,}(?![>+*\s-]))(?={{indented_code_block}})'
push:
- include: indented-code-block
- match: $
pop: true
- match: '^(?=\s+{{block_quote}})'