Skip to content

Commit

Permalink
Markdown: Added tables (#1848)
Browse files Browse the repository at this point in the history
This adds support for Markdown tables and improves the matching of headers and code blocks.
  • Loading branch information
RunDevelopment committed Jul 20, 2019
2 parents 8b5d67a + a3a6d9e commit cedb8e8
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 6 deletions.
48 changes: 44 additions & 4 deletions components/prism-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,63 @@
}


var tableCell = /(?:\\.|``.+?``|`[^`\r\n]+`|[^\\|\r\n`])+/.source;
var tableRow = /\|?__(?:\|__)+\|?(?:(?:\r?\n|\r)|$)/.source.replace(/__/g, tableCell);
var tableLine = /\|?[ \t]*:?-{3,}:?[ \t]*(?:\|[ \t]*:?-{3,}:?[ \t]*)+\|?(?:\r?\n|\r)/.source;


Prism.languages.markdown = Prism.languages.extend('markup', {});
Prism.languages.insertBefore('markdown', 'prolog', {
'blockquote': {
// > ...
pattern: /^>(?:[\t ]*>)*/m,
alias: 'punctuation'
},
'table': {
pattern: RegExp('^' + tableRow + tableLine + '(?:' + tableRow + ')*', 'm'),
inside: {
'table-data-rows': {
pattern: RegExp('^(' + tableRow + tableLine + ')(?:' + tableRow + ')*$'),
lookbehind: true,
inside: {
'table-data': {
pattern: RegExp(tableCell),
inside: Prism.languages.markdown
},
'punctuation': /\|/
}
},
'table-line': {
pattern: RegExp('^(' + tableRow + ')' + tableLine + '$'),
lookbehind: true,
inside: {
'punctuation': /\||:?-{3,}:?/
}
},
'table-header-row': {
pattern: RegExp('^' + tableRow + '$'),
inside: {
'table-header': {
pattern: RegExp(tableCell),
alias: 'important',
inside: Prism.languages.markdown
},
'punctuation': /\|/
}
}
}
},
'code': [
{
// Prefixed by 4 spaces or 1 tab
pattern: /^(?: {4}|\t).+/m,
// Prefixed by 4 spaces or 1 tab and preceded by an empty line
pattern: /(^[ \t]*(?:\r?\n|\r))(?: {4}|\t).+(?:(?:\r?\n|\r)(?: {4}|\t).+)*/m,
lookbehind: true,
alias: 'keyword'
},
{
// `code`
// ``code``
pattern: /``.+?``|`[^`\n]+`/,
pattern: /``.+?``|`[^`\r\n]+`/,
alias: 'keyword'
},
{
Expand Down Expand Up @@ -68,7 +108,7 @@

// title 2
// -------
pattern: /\S.*(?:\r?\n|\r)(?:==+|--+)/,
pattern: /\S.*(?:\r?\n|\r)(?:==+|--+)(?=[ \t]*$)/m,
alias: 'important',
inside: {
punctuation: /==+$|--+$/
Expand Down
2 changes: 1 addition & 1 deletion components/prism-markdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/languages/markdown/code_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
foobar

foobar
continuous

``` js
var a = 0;
Expand All @@ -15,7 +16,7 @@ var a = 0;
["code", "`foo bar baz`"],
["code", "``foo `bar` baz``"],
["code", " foobar"],
["code", "\tfoobar"],
["code", "\tfoobar\r\n\tcontinuous"],

["code", [
["punctuation", "```"],
Expand Down
179 changes: 179 additions & 0 deletions tests/languages/markdown/table_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |

Markdown | Less | Pretty
--- | --- | ---
*Still* | `renders` | **nicely**
1 | 2 | 3

|Abc | Def |
--- | ---
|`` `. ``|2|

----------------------------------------------------

[
["table", [
["table-header-row", [
["punctuation", "|"],
["table-header", [
" Tables "
]],
["punctuation", "|"],
["table-header", [
" Are "
]],
["punctuation", "|"],
["table-header", [
" Cool "
]],
["punctuation", "|"]
]],
["table-line", [
["punctuation", "|"],
["punctuation", "-------------"],
["punctuation", "|"],
["punctuation", ":-------------:"],
["punctuation", "|"],
["punctuation", "-----:"],
["punctuation", "|"]
]],
["table-data-rows", [
["punctuation", "|"],
["table-data", [
" col 3 is "
]],
["punctuation", "|"],
["table-data", [
" right-aligned "
]],
["punctuation", "|"],
["table-data", [
" $1600 "
]],
["punctuation", "|"],
["punctuation", "|"],
["table-data", [
" col 2 is "
]],
["punctuation", "|"],
["table-data", [
" centered "
]],
["punctuation", "|"],
["table-data", [
" $12 "
]],
["punctuation", "|"],
["punctuation", "|"],
["table-data", [
" zebra stripes "
]],
["punctuation", "|"],
["table-data", [
" are neat "
]],
["punctuation", "|"],
["table-data", [
" $1 "
]],
["punctuation", "|"]
]]
]],

["table", [
["table-header-row", [
["table-header", [
"Markdown "
]],
["punctuation", "|"],
["table-header", [
" Less "
]],
["punctuation", "|"],
["table-header", [
" Pretty"
]]
]],
["table-line", [
["punctuation", "---"],
["punctuation", "|"],
["punctuation", "---"],
["punctuation", "|"],
["punctuation", "---"]
]],
["table-data-rows", [
["table-data", [
["italic", [
["punctuation", "*"],
["content", [
"Still"
]],
["punctuation", "*"]
]]
]],
["punctuation", "|"],
["table-data", [
["code", "`renders`"]
]],
["punctuation", "|"],
["table-data", [
["bold", [
["punctuation", "**"],
["content", [
"nicely"
]],
["punctuation", "**"]
]]
]],
["table-data", [
"1 "
]],
["punctuation", "|"],
["table-data", [
" 2 "
]],
["punctuation", "|"],
["table-data", [
" 3"
]]
]]
]],

["table", [
["table-header-row", [
["punctuation", "|"],
["table-header", [
"Abc "
]],
["punctuation", "|"],
["table-header", [
" Def "
]],
["punctuation", "|"]
]],
["table-line", [
["punctuation", "---"],
["punctuation", "|"],
["punctuation", "---"]
]],
["table-data-rows", [
["punctuation", "|"],
["table-data", [
["code", "`` `. ``"]
]],
["punctuation", "|"],
["table-data", [
"2"
]],
["punctuation", "|"]
]]
]]
]

----------------------------------------------------

Checks for tables.

0 comments on commit cedb8e8

Please sign in to comment.