Shell sessions: Fixed multi-line commands (#2872)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
diff --git a/components/prism-shell-session.js b/components/prism-shell-session.js
index 91ef9ea..9748755 100644
--- a/components/prism-shell-session.js
+++ b/components/prism-shell-session.js
@@ -16,7 +16,15 @@
Prism.languages['shell-session'] = {
'command': {
- pattern: RegExp(/^(?:[^\s@:$#*!/\\]+@[^\s@:$#*!/\\]+(?::[^\0-\x1F$#*?"<>:;|]+)?|[^\0-\x1F$#*?"<>:;|]+)?[$#](?:[^\\\r\n'"<$]|\\.|\$(?!')|<<str>>)+/.source.replace(/<<str>>/g, function () { return strings; }), 'm'),
+ pattern: RegExp(
+ // user info
+ /^(?:[^\s@:$#*!/\\]+@[^\s@:$#*!/\\]+(?::[^\0-\x1F$#*?"<>:;|]+)?|[^\0-\x1F$#*?"<>:;|]+)?/.source +
+ // shell symbol
+ /[$#]/.source +
+ // bash command
+ /(?:[^\\\r\n'"<$]|\\(?:[^\r]|\r\n?)|\$(?!')|<<str>>)+/.source.replace(/<<str>>/g, function () { return strings; }),
+ 'm'
+ ),
greedy: true,
inside: {
'info': {
diff --git a/components/prism-shell-session.min.js b/components/prism-shell-session.min.js
index d0ca817..4967676 100644
--- a/components/prism-shell-session.min.js
+++ b/components/prism-shell-session.min.js
@@ -1 +1 @@
-!function(s){var n=['"(?:\\\\[^]|\\$\\([^)]+\\)|\\$(?!\\()|`[^`]+`|[^"\\\\`$])*"',"'[^']*'","\\$'(?:[^'\\\\]|\\\\[^])*'","<<-?\\s*([\"']?)(\\w+)\\1\\s[^]*?[\r\n]\\2"].join("|");s.languages["shell-session"]={command:{pattern:RegExp('^(?:[^\\s@:$#*!/\\\\]+@[^\\s@:$#*!/\\\\]+(?::[^\0-\\x1F$#*?"<>:;|]+)?|[^\0-\\x1F$#*?"<>:;|]+)?[$#](?:[^\\\\\r\n\'"<$]|\\\\.|\\$(?!\')|<<str>>)+'.replace(/<<str>>/g,function(){return n}),"m"),greedy:!0,inside:{info:{pattern:/^[^#$]+/,alias:"punctuation",inside:{user:/^[^\s@:$#*!/\\]+@[^\s@:$#*!/\\]+/,punctuation:/:/,path:/[\s\S]+/}},bash:{pattern:/(^[$#]\s*)\S[\s\S]*/,lookbehind:!0,alias:"language-bash",inside:s.languages.bash},"shell-symbol":{pattern:/^[$#]/,alias:"important"}}},output:/.(?:.*(?:[\r\n]|.$))*/},s.languages["sh-session"]=s.languages.shellsession=s.languages["shell-session"]}(Prism);
\ No newline at end of file
+!function(s){var n=['"(?:\\\\[^]|\\$\\([^)]+\\)|\\$(?!\\()|`[^`]+`|[^"\\\\`$])*"',"'[^']*'","\\$'(?:[^'\\\\]|\\\\[^])*'","<<-?\\s*([\"']?)(\\w+)\\1\\s[^]*?[\r\n]\\2"].join("|");s.languages["shell-session"]={command:{pattern:RegExp('^(?:[^\\s@:$#*!/\\\\]+@[^\\s@:$#*!/\\\\]+(?::[^\0-\\x1F$#*?"<>:;|]+)?|[^\0-\\x1F$#*?"<>:;|]+)?[$#]'+"(?:[^\\\\\r\n'\"<$]|\\\\(?:[^\r]|\r\n?)|\\$(?!')|<<str>>)+".replace(/<<str>>/g,function(){return n}),"m"),greedy:!0,inside:{info:{pattern:/^[^#$]+/,alias:"punctuation",inside:{user:/^[^\s@:$#*!/\\]+@[^\s@:$#*!/\\]+/,punctuation:/:/,path:/[\s\S]+/}},bash:{pattern:/(^[$#]\s*)\S[\s\S]*/,lookbehind:!0,alias:"language-bash",inside:s.languages.bash},"shell-symbol":{pattern:/^[$#]/,alias:"important"}}},output:/.(?:.*(?:[\r\n]|.$))*/},s.languages["sh-session"]=s.languages.shellsession=s.languages["shell-session"]}(Prism);
\ No newline at end of file
diff --git a/tests/languages/shell-session/issue2871.test b/tests/languages/shell-session/issue2871.test
new file mode 100644
index 0000000..01af277
--- /dev/null
+++ b/tests/languages/shell-session/issue2871.test
@@ -0,0 +1,30 @@
+fyu@home $ sudo ls -l ~/.config 's' && \
+ echo 'hello'
+drwxr-xr-x - franklinyu 26 Sep 2020 asciinema
+drwxr-xr-x - franklinyu 26 Jan 2020 'bash'
+hello
+
+----------------------------------------------------
+
+[
+ ["command", [
+ ["info", [
+ ["user", "fyu@home"],
+ ["path", " "]
+ ]],
+ ["shell-symbol", "$"],
+ ["bash", [
+ ["function", "sudo"],
+ ["function", "ls"],
+ " -l ~/.config ",
+ ["string", "'s'"],
+ ["operator", ["&&"]],
+ ["punctuation", "\\"],
+
+ ["builtin", "echo"],
+ ["string", "'hello'"]
+ ]]
+ ]],
+
+ ["output", "drwxr-xr-x - franklinyu 26 Sep 2020 asciinema\r\ndrwxr-xr-x - franklinyu 26 Jan 2020 'bash'\r\nhello"]
+]