Skip to content

Commit cedb8e8

Browse files
Markdown: Added tables (#1848)
This adds support for Markdown tables and improves the matching of headers and code blocks.
2 parents 8b5d67a + a3a6d9e commit cedb8e8

File tree

4 files changed

+226
-6
lines changed

4 files changed

+226
-6
lines changed

components/prism-markdown.js

+44-4
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,63 @@
2323
}
2424

2525

26+
var tableCell = /(?:\\.|``.+?``|`[^`\r\n]+`|[^\\|\r\n`])+/.source;
27+
var tableRow = /\|?__(?:\|__)+\|?(?:(?:\r?\n|\r)|$)/.source.replace(/__/g, tableCell);
28+
var tableLine = /\|?[ \t]*:?-{3,}:?[ \t]*(?:\|[ \t]*:?-{3,}:?[ \t]*)+\|?(?:\r?\n|\r)/.source;
29+
30+
2631
Prism.languages.markdown = Prism.languages.extend('markup', {});
2732
Prism.languages.insertBefore('markdown', 'prolog', {
2833
'blockquote': {
2934
// > ...
3035
pattern: /^>(?:[\t ]*>)*/m,
3136
alias: 'punctuation'
3237
},
38+
'table': {
39+
pattern: RegExp('^' + tableRow + tableLine + '(?:' + tableRow + ')*', 'm'),
40+
inside: {
41+
'table-data-rows': {
42+
pattern: RegExp('^(' + tableRow + tableLine + ')(?:' + tableRow + ')*$'),
43+
lookbehind: true,
44+
inside: {
45+
'table-data': {
46+
pattern: RegExp(tableCell),
47+
inside: Prism.languages.markdown
48+
},
49+
'punctuation': /\|/
50+
}
51+
},
52+
'table-line': {
53+
pattern: RegExp('^(' + tableRow + ')' + tableLine + '$'),
54+
lookbehind: true,
55+
inside: {
56+
'punctuation': /\||:?-{3,}:?/
57+
}
58+
},
59+
'table-header-row': {
60+
pattern: RegExp('^' + tableRow + '$'),
61+
inside: {
62+
'table-header': {
63+
pattern: RegExp(tableCell),
64+
alias: 'important',
65+
inside: Prism.languages.markdown
66+
},
67+
'punctuation': /\|/
68+
}
69+
}
70+
}
71+
},
3372
'code': [
3473
{
35-
// Prefixed by 4 spaces or 1 tab
36-
pattern: /^(?: {4}|\t).+/m,
74+
// Prefixed by 4 spaces or 1 tab and preceded by an empty line
75+
pattern: /(^[ \t]*(?:\r?\n|\r))(?: {4}|\t).+(?:(?:\r?\n|\r)(?: {4}|\t).+)*/m,
76+
lookbehind: true,
3777
alias: 'keyword'
3878
},
3979
{
4080
// `code`
4181
// ``code``
42-
pattern: /``.+?``|`[^`\n]+`/,
82+
pattern: /``.+?``|`[^`\r\n]+`/,
4383
alias: 'keyword'
4484
},
4585
{
@@ -68,7 +108,7 @@
68108

69109
// title 2
70110
// -------
71-
pattern: /\S.*(?:\r?\n|\r)(?:==+|--+)/,
111+
pattern: /\S.*(?:\r?\n|\r)(?:==+|--+)(?=[ \t]*$)/m,
72112
alias: 'important',
73113
inside: {
74114
punctuation: /==+$|--+$/

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/code_feature.test

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
foobar
55

66
foobar
7+
continuous
78

89
``` js
910
var a = 0;
@@ -15,7 +16,7 @@ var a = 0;
1516
["code", "`foo bar baz`"],
1617
["code", "``foo `bar` baz``"],
1718
["code", " foobar"],
18-
["code", "\tfoobar"],
19+
["code", "\tfoobar\r\n\tcontinuous"],
1920

2021
["code", [
2122
["punctuation", "```"],
+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
| Tables | Are | Cool |
2+
| ------------- |:-------------:| -----:|
3+
| col 3 is | right-aligned | $1600 |
4+
| col 2 is | centered | $12 |
5+
| zebra stripes | are neat | $1 |
6+
7+
Markdown | Less | Pretty
8+
--- | --- | ---
9+
*Still* | `renders` | **nicely**
10+
1 | 2 | 3
11+
12+
|Abc | Def |
13+
--- | ---
14+
|`` `. ``|2|
15+
16+
----------------------------------------------------
17+
18+
[
19+
["table", [
20+
["table-header-row", [
21+
["punctuation", "|"],
22+
["table-header", [
23+
" Tables "
24+
]],
25+
["punctuation", "|"],
26+
["table-header", [
27+
" Are "
28+
]],
29+
["punctuation", "|"],
30+
["table-header", [
31+
" Cool "
32+
]],
33+
["punctuation", "|"]
34+
]],
35+
["table-line", [
36+
["punctuation", "|"],
37+
["punctuation", "-------------"],
38+
["punctuation", "|"],
39+
["punctuation", ":-------------:"],
40+
["punctuation", "|"],
41+
["punctuation", "-----:"],
42+
["punctuation", "|"]
43+
]],
44+
["table-data-rows", [
45+
["punctuation", "|"],
46+
["table-data", [
47+
" col 3 is "
48+
]],
49+
["punctuation", "|"],
50+
["table-data", [
51+
" right-aligned "
52+
]],
53+
["punctuation", "|"],
54+
["table-data", [
55+
" $1600 "
56+
]],
57+
["punctuation", "|"],
58+
["punctuation", "|"],
59+
["table-data", [
60+
" col 2 is "
61+
]],
62+
["punctuation", "|"],
63+
["table-data", [
64+
" centered "
65+
]],
66+
["punctuation", "|"],
67+
["table-data", [
68+
" $12 "
69+
]],
70+
["punctuation", "|"],
71+
["punctuation", "|"],
72+
["table-data", [
73+
" zebra stripes "
74+
]],
75+
["punctuation", "|"],
76+
["table-data", [
77+
" are neat "
78+
]],
79+
["punctuation", "|"],
80+
["table-data", [
81+
" $1 "
82+
]],
83+
["punctuation", "|"]
84+
]]
85+
]],
86+
87+
["table", [
88+
["table-header-row", [
89+
["table-header", [
90+
"Markdown "
91+
]],
92+
["punctuation", "|"],
93+
["table-header", [
94+
" Less "
95+
]],
96+
["punctuation", "|"],
97+
["table-header", [
98+
" Pretty"
99+
]]
100+
]],
101+
["table-line", [
102+
["punctuation", "---"],
103+
["punctuation", "|"],
104+
["punctuation", "---"],
105+
["punctuation", "|"],
106+
["punctuation", "---"]
107+
]],
108+
["table-data-rows", [
109+
["table-data", [
110+
["italic", [
111+
["punctuation", "*"],
112+
["content", [
113+
"Still"
114+
]],
115+
["punctuation", "*"]
116+
]]
117+
]],
118+
["punctuation", "|"],
119+
["table-data", [
120+
["code", "`renders`"]
121+
]],
122+
["punctuation", "|"],
123+
["table-data", [
124+
["bold", [
125+
["punctuation", "**"],
126+
["content", [
127+
"nicely"
128+
]],
129+
["punctuation", "**"]
130+
]]
131+
]],
132+
["table-data", [
133+
"1 "
134+
]],
135+
["punctuation", "|"],
136+
["table-data", [
137+
" 2 "
138+
]],
139+
["punctuation", "|"],
140+
["table-data", [
141+
" 3"
142+
]]
143+
]]
144+
]],
145+
146+
["table", [
147+
["table-header-row", [
148+
["punctuation", "|"],
149+
["table-header", [
150+
"Abc "
151+
]],
152+
["punctuation", "|"],
153+
["table-header", [
154+
" Def "
155+
]],
156+
["punctuation", "|"]
157+
]],
158+
["table-line", [
159+
["punctuation", "---"],
160+
["punctuation", "|"],
161+
["punctuation", "---"]
162+
]],
163+
["table-data-rows", [
164+
["punctuation", "|"],
165+
["table-data", [
166+
["code", "`` `. ``"]
167+
]],
168+
["punctuation", "|"],
169+
["table-data", [
170+
"2"
171+
]],
172+
["punctuation", "|"]
173+
]]
174+
]]
175+
]
176+
177+
----------------------------------------------------
178+
179+
Checks for tables.

0 commit comments

Comments
 (0)