From 9d2fddc26c05a9fd0a8b91ae9cb8769bde5a22b6 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Wed, 28 Nov 2018 14:21:20 +0100 Subject: [PATCH] Added strike support to markdown (#1563) This PR adds support for the strike operator in [both forms][1]. It does not include actual styling for the strike operator as it makes the ~ character really hard to read. [1]: https://webapps.stackexchange.com/questions/14986/strikethrough-with-github-markdown/103691#103691 --- components/prism-markdown.js | 21 +++++- components/prism-markdown.min.js | 2 +- tests/languages/markdown/bold_feature.test | 14 +++- tests/languages/markdown/strike_feature.test | 70 ++++++++++++++++++++ 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/languages/markdown/strike_feature.test diff --git a/components/prism-markdown.js b/components/prism-markdown.js index 8b63780406..6ec38a0e91 100644 --- a/components/prism-markdown.js +++ b/components/prism-markdown.js @@ -83,6 +83,7 @@ Prism.languages.insertBefore('markdown', 'prolog', { // Allow only one line break pattern: /(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/, lookbehind: true, + greedy: true, inside: { 'punctuation': /^\*\*|^__|\*\*$|__$/ } @@ -94,10 +95,23 @@ Prism.languages.insertBefore('markdown', 'prolog', { // Allow only one line break pattern: /(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/, lookbehind: true, + greedy: true, inside: { 'punctuation': /^[*_]|[*_]$/ } }, + 'strike': { + // ~~strike through~~ + // ~strike~ + + // Allow only one line break + pattern: /(^|[^\\])(~~?)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/, + lookbehind: true, + greedy: true, + inside: { + 'punctuation': /^~~?|~~?$/ + } + }, 'url': { // [example](http://example.com "Optional title") // [example] [id] @@ -116,5 +130,10 @@ Prism.languages.insertBefore('markdown', 'prolog', { Prism.languages.markdown['bold'].inside['url'] = Prism.languages.markdown['url']; Prism.languages.markdown['italic'].inside['url'] = Prism.languages.markdown['url']; +Prism.languages.markdown['strike'].inside['url'] = Prism.languages.markdown['url']; Prism.languages.markdown['bold'].inside['italic'] = Prism.languages.markdown['italic']; -Prism.languages.markdown['italic'].inside['bold'] = Prism.languages.markdown['bold']; \ No newline at end of file +Prism.languages.markdown['bold'].inside['strike'] = Prism.languages.markdown['strike']; +Prism.languages.markdown['italic'].inside['bold'] = Prism.languages.markdown['bold']; +Prism.languages.markdown['italic'].inside['strike'] = Prism.languages.markdown['strike']; +Prism.languages.markdown['strike'].inside['bold'] = Prism.languages.markdown['bold']; +Prism.languages.markdown['strike'].inside['italic'] = Prism.languages.markdown['italic']; diff --git a/components/prism-markdown.min.js b/components/prism-markdown.min.js index 012565b4a6..a13d883ebb 100644 --- a/components/prism-markdown.min.js +++ b/components/prism-markdown.min.js @@ -1 +1 @@ -Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/^>(?:[\t ]*>)*/m,alias:"punctuation"},code:[{pattern:/^(?: {4}|\t).+/m,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*(?:\r?\n|\r)(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/(^\s*)#+.+/m,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/(^\s*)([*-])(?:[\t ]*\2){2,}(?=\s*$)/m,lookbehind:!0,alias:"punctuation"},list:{pattern:/(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,punctuation:/^[\[\]!:]|[<>]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^\*\*|^__|\*\*$|__$/}},italic:{pattern:/(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^[*_]|[*_]$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:\\.|[^"\\])*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.languages.markdown.url,Prism.languages.markdown.italic.inside.url=Prism.languages.markdown.url,Prism.languages.markdown.bold.inside.italic=Prism.languages.markdown.italic,Prism.languages.markdown.italic.inside.bold=Prism.languages.markdown.bold; \ No newline at end of file +Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/^>(?:[\t ]*>)*/m,alias:"punctuation"},code:[{pattern:/^(?: {4}|\t).+/m,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*(?:\r?\n|\r)(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/(^\s*)#+.+/m,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/(^\s*)([*-])(?:[\t ]*\2){2,}(?=\s*$)/m,lookbehind:!0,alias:"punctuation"},list:{pattern:/(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,punctuation:/^[\[\]!:]|[<>]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,greedy:!0,inside:{punctuation:/^\*\*|^__|\*\*$|__$/}},italic:{pattern:/(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,greedy:!0,inside:{punctuation:/^[*_]|[*_]$/}},strike:{pattern:/(^|[^\\])(~~?)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,greedy:!0,inside:{punctuation:/^~~?|~~?$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:\\.|[^"\\])*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.languages.markdown.url,Prism.languages.markdown.italic.inside.url=Prism.languages.markdown.url,Prism.languages.markdown.strike.inside.url=Prism.languages.markdown.url,Prism.languages.markdown.bold.inside.italic=Prism.languages.markdown.italic,Prism.languages.markdown.bold.inside.strike=Prism.languages.markdown.strike,Prism.languages.markdown.italic.inside.bold=Prism.languages.markdown.bold,Prism.languages.markdown.italic.inside.strike=Prism.languages.markdown.strike,Prism.languages.markdown.strike.inside.bold=Prism.languages.markdown.bold,Prism.languages.markdown.strike.inside.italic=Prism.languages.markdown.italic; \ No newline at end of file diff --git a/tests/languages/markdown/bold_feature.test b/tests/languages/markdown/bold_feature.test index 7d8e8ba6cd..486e6ab60e 100644 --- a/tests/languages/markdown/bold_feature.test +++ b/tests/languages/markdown/bold_feature.test @@ -7,6 +7,7 @@ bar__ __foo*bar*baz__ __foo_bar_baz__ +__foo~bar~baz__ __foo[bar](baz)__ ---------------------------------------------------- @@ -55,6 +56,17 @@ __foo[bar](baz)__ "baz", ["punctuation", "__"] ]], + ["bold", [ + ["punctuation", "__"], + "foo", + ["strike", [ + ["punctuation", "~"], + "bar", + ["punctuation", "~"] + ]], + "baz", + ["punctuation", "__"] + ]], ["bold", [ ["punctuation", "__"], "foo", @@ -67,4 +79,4 @@ __foo[bar](baz)__ ---------------------------------------------------- -Checks for bold. Also tests for inclusion of italic and url. \ No newline at end of file +Checks for bold. Also tests for inclusion of italic, strike, and url. diff --git a/tests/languages/markdown/strike_feature.test b/tests/languages/markdown/strike_feature.test new file mode 100644 index 0000000000..49a4a139a4 --- /dev/null +++ b/tests/languages/markdown/strike_feature.test @@ -0,0 +1,70 @@ +~~foobar~~ +~~foo +bar~~ +~foobar~ +~foo +bar~ + +~foo*bar*baz~ +~foo__bar__baz~ +~foo[bar](baz)~ + +---------------------------------------------------- + +[ + ["strike", [ + ["punctuation", "~~"], + "foobar", + ["punctuation", "~~"] + ]], + ["strike", [ + ["punctuation", "~~"], + "foo\r\nbar", + ["punctuation", "~~"] + ]], + ["strike", [ + ["punctuation", "~"], + "foobar", + ["punctuation", "~"] + ]], + ["strike", [ + ["punctuation", "~"], + "foo\r\nbar", + ["punctuation", "~"] + ]], + + ["strike", [ + ["punctuation", "~"], + "foo", + ["italic", [ + ["punctuation", "*"], + "bar", + ["punctuation", "*"] + ]], + "baz", + ["punctuation", "~"] + ]], + ["strike", [ + ["punctuation", "~"], + "foo", + ["bold", [ + ["punctuation", "__"], + "bar", + ["punctuation", "__"] + ]], + "baz", + ["punctuation", "~"] + ]], + ["strike", [ + ["punctuation", "~"], + "foo", + ["url", [ + "[bar](baz)" + ]], + ["punctuation", "~"] + ]] +] + +---------------------------------------------------- + +Checks for strike. Also tests for inclusion of bold, italic, and url.