Skip to content

Commit

Permalink
Markdown: Improved code snippets (#2967)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jun 27, 2021
1 parent 4b55bd6 commit e9477d8
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 83 deletions.
16 changes: 9 additions & 7 deletions components/prism-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@
lookbehind: true,
alias: 'keyword'
},
{
// `code`
// ``code``
pattern: /``.+?``|`[^`\r\n]+`/,
alias: 'keyword'
},
{
// ```optional language
// code block
Expand Down Expand Up @@ -218,6 +212,14 @@
'punctuation': /~~?/
}
},
'code-snippet': {
// `code`
// ``code``
pattern: /(^|[^\\`])(?:``[^`\r\n]+(?:`[^`\r\n]+)*``(?!`)|`[^`\r\n]+`(?!`))/,
lookbehind: true,
greedy: true,
alias: ['code', 'keyword']
},
'url': {
// [example](http://example.com "Optional title")
// [example][id]
Expand Down Expand Up @@ -249,7 +251,7 @@
});

['url', 'bold', 'italic', 'strike'].forEach(function (token) {
['url', 'bold', 'italic', 'strike'].forEach(function (inside) {
['url', 'bold', 'italic', 'strike', 'code-snippet'].forEach(function (inside) {
if (token !== inside) {
Prism.languages.markdown[token].inside.content.inside[inside] = Prism.languages.markdown[inside];
}
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.

18 changes: 18 additions & 0 deletions tests/languages/markdown/bold_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ __foo _bar_ baz__
__foo ~bar~ baz__
__foo ~~bar~~ baz__
__foo[bar](baz)__
__foo `bar`__

**foo *bar* baz**
**foo _bar_ baz**
**foo ~bar~ baz**
**foo ~~bar~~ baz**
**foo[bar](baz)**
**foo `bar`**

not__bold__ __this__either

Expand Down Expand Up @@ -109,6 +111,14 @@ not__bold__ __this__either
]],
["punctuation", "__"]
]],
["bold", [
["punctuation", "__"],
["content", [
"foo ",
["code-snippet", "`bar`"]
]],
["punctuation", "__"]
]],

["bold", [
["punctuation", "**"],
Expand Down Expand Up @@ -176,6 +186,14 @@ not__bold__ __this__either
]],
["punctuation", "**"]
]],
["bold", [
["punctuation", "**"],
["content", [
"foo ",
["code-snippet", "`bar`"]
]],
["punctuation", "**"]
]],

"\r\n\r\nnot__bold__ __this__either"
]
Expand Down
9 changes: 5 additions & 4 deletions tests/languages/markdown/code_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ var a = 0;
----------------------------------------------------

[
["code", "`foo bar baz`"],
["code", "``foo `bar` baz``"],
["code-snippet", "`foo bar baz`"],
["code-snippet", "``foo `bar` baz``"],

["code", " foobar"],

["code", "\tfoobar\r\n\tcontinuous"],

["code", [
["punctuation", "```"],
["code-language", " js"],
["punctuation", "```"], ["code-language", " js"],
["code-block", "var a = 0;"],
["punctuation", "```"]
]]
Expand Down
17 changes: 17 additions & 0 deletions tests/languages/markdown/issue2966.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
* foo
* `asd` afsdfsdfsdf
* foo
* foo
* `REM`
* foo

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

[
["list", "*"], " foo\r\n",
["list", "*"], ["code-snippet", "`asd`"], " afsdfsdfsdf\r\n",
["list", "*"], " foo\r\n ",
["list", "*"], " foo\r\n ",
["list", "*"], ["code-snippet", "`REM`"],
["list", "*"], " foo"
]
18 changes: 18 additions & 0 deletions tests/languages/markdown/italic_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ _foo **bar** baz_
_foo ~bar~ baz_
_foo ~~bar~~ baz_
_foo[bar](baz)_
_foo `bar`_

*foo __bar__ baz*
*foo **bar** baz*
*foo ~bar~ baz*
*foo ~~bar~~ baz*
*foo[bar](baz)*
*foo `bar`*

not_italic_ _this_either

Expand Down Expand Up @@ -109,6 +111,14 @@ not_italic_ _this_either
]],
["punctuation", "_"]
]],
["italic", [
["punctuation", "_"],
["content", [
"foo ",
["code-snippet", "`bar`"]
]],
["punctuation", "_"]
]],

["italic", [
["punctuation", "*"],
Expand Down Expand Up @@ -176,6 +186,14 @@ not_italic_ _this_either
]],
["punctuation", "*"]
]],
["italic", [
["punctuation", "*"],
["content", [
"foo ",
["code-snippet", "`bar`"]
]],
["punctuation", "*"]
]],

"\r\n\r\nnot_italic_ _this_either"
]
Expand Down
18 changes: 18 additions & 0 deletions tests/languages/markdown/strike_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ bar~
~foo **bar** baz~
~foo __bar__ baz~
~foo[bar](baz)~
~foo `bar`~

~~foo *bar* baz~~
~~foo _bar_ baz~~
~~foo **bar** baz~~
~~foo __bar__ baz~~
~~foo[bar](baz)~~
~~foo `bar`~~

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

Expand Down Expand Up @@ -107,6 +109,14 @@ bar~
]],
["punctuation", "~"]
]],
["strike", [
["punctuation", "~"],
["content", [
"foo ",
["code-snippet", "`bar`"]
]],
["punctuation", "~"]
]],

["strike", [
["punctuation", "~~"],
Expand Down Expand Up @@ -173,6 +183,14 @@ bar~
]]
]],
["punctuation", "~~"]
]],
["strike", [
["punctuation", "~~"],
["content", [
"foo ",
["code-snippet", "`bar`"]
]],
["punctuation", "~~"]
]]
]

Expand Down
99 changes: 28 additions & 71 deletions tests/languages/markdown/table_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ Markdown | Less | Pretty
["table", [
["table-header-row", [
["punctuation", "|"],
["table-header", [
" Tables "
]],
["table-header", [" Tables "]],
["punctuation", "|"],
["table-header", [
" Are "
]],
["table-header", [" Are "]],
["punctuation", "|"],
["table-header", [
" Cool "
]],
["table-header", [" Cool "]],
["punctuation", "|"]
]],
["table-line", [
Expand All @@ -43,60 +37,38 @@ Markdown | Less | Pretty
]],
["table-data-rows", [
["punctuation", "|"],
["table-data", [
" col 3 is "
]],
["table-data", [" col 3 is "]],
["punctuation", "|"],
["table-data", [
" right-aligned "
]],
["table-data", [" right-aligned "]],
["punctuation", "|"],
["table-data", [
" $1600 "
]],
["table-data", [" $1600 "]],
["punctuation", "|"],

["punctuation", "|"],
["table-data", [
" col 2 is "
]],
["table-data", [" col 2 is "]],
["punctuation", "|"],
["table-data", [
" centered "
]],
["table-data", [" centered "]],
["punctuation", "|"],
["table-data", [
" $12 "
]],
["table-data", [" $12 "]],
["punctuation", "|"],

["punctuation", "|"],
["table-data", [
" zebra stripes "
]],
["table-data", [" zebra stripes "]],
["punctuation", "|"],
["table-data", [
" are neat "
]],
["table-data", [" are neat "]],
["punctuation", "|"],
["table-data", [
" $1 "
]],
["table-data", [" $1 "]],
["punctuation", "|"]
]]
]],

["table", [
["table-header-row", [
["table-header", [
"Markdown "
]],
["table-header", ["Markdown "]],
["punctuation", "|"],
["table-header", [
" Less "
]],
["table-header", [" Less "]],
["punctuation", "|"],
["table-header", [
" Pretty"
]]
["table-header", [" Pretty"]]
]],
["table-line", [
["punctuation", "---"],
Expand All @@ -109,50 +81,37 @@ Markdown | Less | Pretty
["table-data", [
["italic", [
["punctuation", "*"],
["content", [
"Still"
]],
["content", ["Still"]],
["punctuation", "*"]
]]
]],
["punctuation", "|"],
["table-data", [
["code", "`renders`"]
["code-snippet", "`renders`"]
]],
["punctuation", "|"],
["table-data", [
["bold", [
["punctuation", "**"],
["content", [
"nicely"
]],
["content", ["nicely"]],
["punctuation", "**"]
]]
]],
["table-data", [
"1 "
]],

["table-data", ["1 "]],
["punctuation", "|"],
["table-data", [
" 2 "
]],
["table-data", [" 2 "]],
["punctuation", "|"],
["table-data", [
" 3"
]]
["table-data", [" 3"]]
]]
]],

["table", [
["table-header-row", [
["punctuation", "|"],
["table-header", [
"Abc "
]],
["table-header", ["Abc "]],
["punctuation", "|"],
["table-header", [
" Def "
]],
["table-header", [" Def "]],
["punctuation", "|"]
]],
["table-line", [
Expand All @@ -163,12 +122,10 @@ Markdown | Less | Pretty
["table-data-rows", [
["punctuation", "|"],
["table-data", [
["code", "`` `. ``"]
["code-snippet", "`` `. ``"]
]],
["punctuation", "|"],
["table-data", [
"2"
]],
["table-data", ["2"]],
["punctuation", "|"]
]]
]]
Expand Down
Loading

0 comments on commit e9477d8

Please sign in to comment.