Commit cda976b1b76f6adb0e0b5e692db3b30404093e15

Michael Schmidt 2021-05-04T16:46:50

Shell sessions: Fixed multi-line commands (#2872)

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"]
+]