Skip to content

Instantly share code, notes, and snippets.

@sgon00
Last active December 24, 2018 11:22
Show Gist options
  • Save sgon00/836386ba8b16e95297e4e5aa0a7089ae to your computer and use it in GitHub Desktop.
Save sgon00/836386ba8b16e95297e4e5aa0a7089ae to your computer and use it in GitHub Desktop.
Note: `prism-shell.min.js` is actually not minimized JS file. Because I am using [MacDown](https://github.com/MacDownApp/macdown) locally, I am lazy to minimize the JS file. Just put it at `/Applications/MacDown.app/Contents/Resources/Prism/components/` and modifies your `components.js` and corresponding CSS file `my_macdown_style.css`.
...... other js codes ......
"shell": {
"title": "Shell",
"owner": "sgon00" // based on zeitgeist87's bash
},
...... other js codes ......
...... other css codes ......
.token.promptString {
color: #880E4F;
}
.token.promptOthers {
color: #222;
}
.token.cmdOut {
font-style: italic;
color: #999;
}
...... other css codes ......
(function(Prism) {
var insideString = {
variable: [
// Arithmetic Environment
{
pattern: /\$?\(\([\w\W]+?\)\)/,
inside: {
// If there is a $ sign at the beginning highlight $(( and )) as variable
variable: [{
pattern: /(^\$\(\([\w\W]+)\)\)/,
lookbehind: true
},
/^\$\(\(/,
],
number: /\b-?(?:0x[\dA-Fa-f]+|\d*\.?\d+(?:[Ee]-?\d+)?)\b/,
// Operators according to https://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic
operator: /--?|-=|\+\+?|\+=|!=?|~|\*\*?|\*=|\/=?|%=?|<<=?|>>=?|<=?|>=?|==?|&&?|&=|\^=?|\|\|?|\|=|\?|:/,
// If there is no $ sign at the beginning highlight (( and )) as punctuation
punctuation: /\(\(?|\)\)?|,|;/
}
},
// Command Substitution
{
pattern: /\$\([^)]+\)|`[^`]+`/,
inside: {
variable: /^\$\(|^`|\)$|`$/
}
},
/\$(?:[a-z0-9_#\?\*!@]+|\{[^}]+\})/i
],
};
Prism.languages.shell = {
'prompt': {
pattern: /^[\w\d\[(@-]*((?!^)\ )?[\])~\/\w\d-]*[>%$#].*/m,
inside: {
'promptString': {
pattern: /^[\w\d\[(@-]*((?!^)\ )?[\])~\/\w\d-]*[>%$#]/,
},
'comment': {
pattern: /(^|[^"{\\])#.*/,
lookbehind: true
},
'string': [
//Support for Here-Documents https://en.wikipedia.org/wiki/Here_document
{
pattern: /((?:^|[^<])<<\s*)(?:"|')?(\w+?)(?:"|')?\s*\r?\n(?:[\s\S])*?\r?\n\2/g,
lookbehind: true,
inside: insideString
},
{
pattern: /(["'])(?:\\\\|\\?[^\\])*?\1/g,
inside: insideString
}
],
'variable': insideString.variable,
// Originally based on http://ss64.com/bash/
'function': {
pattern: /(^|\s|;|\||&)(?:brew|flutter|alias|apropos|apt-get|aptitude|aspell|awk|basename|bash|bc|bg|builtin|bzip2|cal|cat|cd|cfdisk|chgrp|chmod|chown|chroot|chkconfig|cksum|clear|cmp|comm|command|cp|cron|crontab|csplit|cut|date|dc|dd|ddrescue|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|du|egrep|eject|enable|env|ethtool|eval|exec|expand|expect|export|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|getopts|git|grep|groupadd|groupdel|groupmod|groups|gzip|hash|head|help|hg|history|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|jobs|join|kill|killall|less|link|ln|locate|logname|logout|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|make|man|mkdir|mkfifo|mkisofs|mknod|more|most|mount|mtools|mtr|mv|mmv|nano|netstat|nice|nl|nohup|notify-send|nslookup|open|op|passwd|paste|pathchk|ping|pkill|popd|pr|printcap|printenv|printf|ps|pushd|pv|pwd|quota|quotacheck|quotactl|ram|rar|rcp|read|readarray|readonly|reboot|rename|renice|remsync|rev|rm|rmdir|rsync|screen|scp|sdiff|sed|seq|service|sftp|shift|shopt|shutdown|sleep|slocate|sort|source|split|ssh|stat|strace|su|sudo|sum|suspend|sync|tail|tar|tee|test|time|timeout|times|touch|top|traceroute|trap|tr|tsort|tty|type|ulimit|umask|umount|unalias|uname|unexpand|uniq|units|unrar|unshar|uptime|useradd|userdel|usermod|users|uuencode|uudecode|v|vdir|vi|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yes|zip)(?=$|\s|;|\||&)/,
lookbehind: true
},
'keyword': {
pattern: /(^|\s|;|\||&)(?:let|:|\.|if|then|else|elif|fi|for|break|continue|while|in|case|function|select|do|done|until|echo|exit|return|set|declare)(?=$|\s|;|\||&)/,
lookbehind: true
},
'boolean': {
pattern: /(^|\s|;|\||&)(?:true|false)(?=$|\s|;|\||&)/,
lookbehind: true
},
'operator': /&&?|\|\|?|==?|!=?|<<<?|>>|<=?|>=?|=~/,
'punctuation': /\$?\(\(?|\)\)?|\.\.|[{}[\];]/,
'localFunction': {
pattern: /\.\/\w+\ /,
alias: 'function',
},
'promptOthers': /.*/,
}
},
'comment': {
pattern: /(^|[^"{\\])#.*/,
lookbehind: true
},
'cmdOut': /\n.*/,
};
var inside = insideString.variable[1].inside;
inside['function'] = Prism.languages.bash['function'];
inside.keyword = Prism.languages.bash.keyword;
inside.boolean = Prism.languages.bash.boolean;
inside.operator = Prism.languages.bash.operator;
inside.punctuation = Prism.languages.bash.punctuation;
})(Prism);
@sgon00
Copy link
Author

sgon00 commented Dec 24, 2018

Note: prism-shell.min.js is actually not minimized JS file. Because I am using MacDown locally, I am lazy to minimize the JS file. For MacDown user, just put it at /Applications/MacDown.app/Contents/Resources/Prism/components/ and modifies your /Applications/MacDown.app/Contents/Resources/Prism/components.js and corresponding CSS file my_macdown_style.css at "/Users/<YOUR-USER-NAME>/Library/Application Support/MacDown/Styles".

@RunDevelopment
Copy link

RunDevelopment commented Dec 24, 2018

@sgon00 You don't have to minify the files yourself. gulp will do that for you using the npx gulp command. It will also create components.js, so please make your changes in the .json.

On that note: The entry for bash in components.json has a shell alias which you should remove. Same for the line Prism.languages.shell = Prism.languages.bash in prism-bash.js.

As for the language itself:

  1. Line 86 to 91 only work when prism-bash.js is loaded. Replacing Prism.languages.bash with Prism.languages.shell['prompt'].inside should fix that.
  2. What does comment match (the one before cmdOut)? Can comments appear outside of prompt?
  3. Use 'cmdOut': /.+/. That should work.

Also, when you make patterns for Prism, please be careful that they always match at least one character. Otherwise, they might cause an infinite number of zero-length matches, which causes Prism to abort at some point to prevent looping forever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment