Skip to content

Commit e9477d8

Browse files
Markdown: Improved code snippets (#2967)
1 parent 4b55bd6 commit e9477d8

9 files changed

+136
-83
lines changed

components/prism-markdown.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@
8585
lookbehind: true,
8686
alias: 'keyword'
8787
},
88-
{
89-
// `code`
90-
// ``code``
91-
pattern: /``.+?``|`[^`\r\n]+`/,
92-
alias: 'keyword'
93-
},
9488
{
9589
// ```optional language
9690
// code block
@@ -218,6 +212,14 @@
218212
'punctuation': /~~?/
219213
}
220214
},
215+
'code-snippet': {
216+
// `code`
217+
// ``code``
218+
pattern: /(^|[^\\`])(?:``[^`\r\n]+(?:`[^`\r\n]+)*``(?!`)|`[^`\r\n]+`(?!`))/,
219+
lookbehind: true,
220+
greedy: true,
221+
alias: ['code', 'keyword']
222+
},
221223
'url': {
222224
// [example](http://example.com "Optional title")
223225
// [example][id]
@@ -249,7 +251,7 @@
249251
});
250252

251253
['url', 'bold', 'italic', 'strike'].forEach(function (token) {
252-
['url', 'bold', 'italic', 'strike'].forEach(function (inside) {
254+
['url', 'bold', 'italic', 'strike', 'code-snippet'].forEach(function (inside) {
253255
if (token !== inside) {
254256
Prism.languages.markdown[token].inside.content.inside[inside] = Prism.languages.markdown[inside];
255257
}

components/prism-markdown.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/markdown/bold_feature.test

+18
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ __foo _bar_ baz__
1010
__foo ~bar~ baz__
1111
__foo ~~bar~~ baz__
1212
__foo[bar](baz)__
13+
__foo `bar`__
1314

1415
**foo *bar* baz**
1516
**foo _bar_ baz**
1617
**foo ~bar~ baz**
1718
**foo ~~bar~~ baz**
1819
**foo[bar](baz)**
20+
**foo `bar`**
1921

2022
not__bold__ __this__either
2123

@@ -109,6 +111,14 @@ not__bold__ __this__either
109111
]],
110112
["punctuation", "__"]
111113
]],
114+
["bold", [
115+
["punctuation", "__"],
116+
["content", [
117+
"foo ",
118+
["code-snippet", "`bar`"]
119+
]],
120+
["punctuation", "__"]
121+
]],
112122

113123
["bold", [
114124
["punctuation", "**"],
@@ -176,6 +186,14 @@ not__bold__ __this__either
176186
]],
177187
["punctuation", "**"]
178188
]],
189+
["bold", [
190+
["punctuation", "**"],
191+
["content", [
192+
"foo ",
193+
["code-snippet", "`bar`"]
194+
]],
195+
["punctuation", "**"]
196+
]],
179197

180198
"\r\n\r\nnot__bold__ __this__either"
181199
]

tests/languages/markdown/code_feature.test

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ var a = 0;
1313
----------------------------------------------------
1414

1515
[
16-
["code", "`foo bar baz`"],
17-
["code", "``foo `bar` baz``"],
16+
["code-snippet", "`foo bar baz`"],
17+
["code-snippet", "``foo `bar` baz``"],
18+
1819
["code", " foobar"],
20+
1921
["code", "\tfoobar\r\n\tcontinuous"],
2022

2123
["code", [
22-
["punctuation", "```"],
23-
["code-language", " js"],
24+
["punctuation", "```"], ["code-language", " js"],
2425
["code-block", "var a = 0;"],
2526
["punctuation", "```"]
2627
]]
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
* foo
2+
* `asd` afsdfsdfsdf
3+
* foo
4+
* foo
5+
* `REM`
6+
* foo
7+
8+
----------------------------------------------------
9+
10+
[
11+
["list", "*"], " foo\r\n",
12+
["list", "*"], ["code-snippet", "`asd`"], " afsdfsdfsdf\r\n",
13+
["list", "*"], " foo\r\n ",
14+
["list", "*"], " foo\r\n ",
15+
["list", "*"], ["code-snippet", "`REM`"],
16+
["list", "*"], " foo"
17+
]

tests/languages/markdown/italic_feature.test

+18
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ _foo **bar** baz_
1010
_foo ~bar~ baz_
1111
_foo ~~bar~~ baz_
1212
_foo[bar](baz)_
13+
_foo `bar`_
1314

1415
*foo __bar__ baz*
1516
*foo **bar** baz*
1617
*foo ~bar~ baz*
1718
*foo ~~bar~~ baz*
1819
*foo[bar](baz)*
20+
*foo `bar`*
1921

2022
not_italic_ _this_either
2123

@@ -109,6 +111,14 @@ not_italic_ _this_either
109111
]],
110112
["punctuation", "_"]
111113
]],
114+
["italic", [
115+
["punctuation", "_"],
116+
["content", [
117+
"foo ",
118+
["code-snippet", "`bar`"]
119+
]],
120+
["punctuation", "_"]
121+
]],
112122

113123
["italic", [
114124
["punctuation", "*"],
@@ -176,6 +186,14 @@ not_italic_ _this_either
176186
]],
177187
["punctuation", "*"]
178188
]],
189+
["italic", [
190+
["punctuation", "*"],
191+
["content", [
192+
"foo ",
193+
["code-snippet", "`bar`"]
194+
]],
195+
["punctuation", "*"]
196+
]],
179197

180198
"\r\n\r\nnot_italic_ _this_either"
181199
]

tests/languages/markdown/strike_feature.test

+18
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ bar~
1010
~foo **bar** baz~
1111
~foo __bar__ baz~
1212
~foo[bar](baz)~
13+
~foo `bar`~
1314

1415
~~foo *bar* baz~~
1516
~~foo _bar_ baz~~
1617
~~foo **bar** baz~~
1718
~~foo __bar__ baz~~
1819
~~foo[bar](baz)~~
20+
~~foo `bar`~~
1921

2022
----------------------------------------------------
2123

@@ -107,6 +109,14 @@ bar~
107109
]],
108110
["punctuation", "~"]
109111
]],
112+
["strike", [
113+
["punctuation", "~"],
114+
["content", [
115+
"foo ",
116+
["code-snippet", "`bar`"]
117+
]],
118+
["punctuation", "~"]
119+
]],
110120

111121
["strike", [
112122
["punctuation", "~~"],
@@ -173,6 +183,14 @@ bar~
173183
]]
174184
]],
175185
["punctuation", "~~"]
186+
]],
187+
["strike", [
188+
["punctuation", "~~"],
189+
["content", [
190+
"foo ",
191+
["code-snippet", "`bar`"]
192+
]],
193+
["punctuation", "~~"]
176194
]]
177195
]
178196

tests/languages/markdown/table_feature.test

+28-71
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,11 @@ Markdown | Less | Pretty
1919
["table", [
2020
["table-header-row", [
2121
["punctuation", "|"],
22-
["table-header", [
23-
" Tables "
24-
]],
22+
["table-header", [" Tables "]],
2523
["punctuation", "|"],
26-
["table-header", [
27-
" Are "
28-
]],
24+
["table-header", [" Are "]],
2925
["punctuation", "|"],
30-
["table-header", [
31-
" Cool "
32-
]],
26+
["table-header", [" Cool "]],
3327
["punctuation", "|"]
3428
]],
3529
["table-line", [
@@ -43,60 +37,38 @@ Markdown | Less | Pretty
4337
]],
4438
["table-data-rows", [
4539
["punctuation", "|"],
46-
["table-data", [
47-
" col 3 is "
48-
]],
40+
["table-data", [" col 3 is "]],
4941
["punctuation", "|"],
50-
["table-data", [
51-
" right-aligned "
52-
]],
42+
["table-data", [" right-aligned "]],
5343
["punctuation", "|"],
54-
["table-data", [
55-
" $1600 "
56-
]],
44+
["table-data", [" $1600 "]],
5745
["punctuation", "|"],
46+
5847
["punctuation", "|"],
59-
["table-data", [
60-
" col 2 is "
61-
]],
48+
["table-data", [" col 2 is "]],
6249
["punctuation", "|"],
63-
["table-data", [
64-
" centered "
65-
]],
50+
["table-data", [" centered "]],
6651
["punctuation", "|"],
67-
["table-data", [
68-
" $12 "
69-
]],
52+
["table-data", [" $12 "]],
7053
["punctuation", "|"],
54+
7155
["punctuation", "|"],
72-
["table-data", [
73-
" zebra stripes "
74-
]],
56+
["table-data", [" zebra stripes "]],
7557
["punctuation", "|"],
76-
["table-data", [
77-
" are neat "
78-
]],
58+
["table-data", [" are neat "]],
7959
["punctuation", "|"],
80-
["table-data", [
81-
" $1 "
82-
]],
60+
["table-data", [" $1 "]],
8361
["punctuation", "|"]
8462
]]
8563
]],
8664

8765
["table", [
8866
["table-header-row", [
89-
["table-header", [
90-
"Markdown "
91-
]],
67+
["table-header", ["Markdown "]],
9268
["punctuation", "|"],
93-
["table-header", [
94-
" Less "
95-
]],
69+
["table-header", [" Less "]],
9670
["punctuation", "|"],
97-
["table-header", [
98-
" Pretty"
99-
]]
71+
["table-header", [" Pretty"]]
10072
]],
10173
["table-line", [
10274
["punctuation", "---"],
@@ -109,50 +81,37 @@ Markdown | Less | Pretty
10981
["table-data", [
11082
["italic", [
11183
["punctuation", "*"],
112-
["content", [
113-
"Still"
114-
]],
84+
["content", ["Still"]],
11585
["punctuation", "*"]
11686
]]
11787
]],
11888
["punctuation", "|"],
11989
["table-data", [
120-
["code", "`renders`"]
90+
["code-snippet", "`renders`"]
12191
]],
12292
["punctuation", "|"],
12393
["table-data", [
12494
["bold", [
12595
["punctuation", "**"],
126-
["content", [
127-
"nicely"
128-
]],
96+
["content", ["nicely"]],
12997
["punctuation", "**"]
13098
]]
13199
]],
132-
["table-data", [
133-
"1 "
134-
]],
100+
101+
["table-data", ["1 "]],
135102
["punctuation", "|"],
136-
["table-data", [
137-
" 2 "
138-
]],
103+
["table-data", [" 2 "]],
139104
["punctuation", "|"],
140-
["table-data", [
141-
" 3"
142-
]]
105+
["table-data", [" 3"]]
143106
]]
144107
]],
145108

146109
["table", [
147110
["table-header-row", [
148111
["punctuation", "|"],
149-
["table-header", [
150-
"Abc "
151-
]],
112+
["table-header", ["Abc "]],
152113
["punctuation", "|"],
153-
["table-header", [
154-
" Def "
155-
]],
114+
["table-header", [" Def "]],
156115
["punctuation", "|"]
157116
]],
158117
["table-line", [
@@ -163,12 +122,10 @@ Markdown | Less | Pretty
163122
["table-data-rows", [
164123
["punctuation", "|"],
165124
["table-data", [
166-
["code", "`` `. ``"]
125+
["code-snippet", "`` `. ``"]
167126
]],
168127
["punctuation", "|"],
169-
["table-data", [
170-
"2"
171-
]],
128+
["table-data", ["2"]],
172129
["punctuation", "|"]
173130
]]
174131
]]

0 commit comments

Comments
 (0)