Repository URL to install this package:
|
Version:
1.2.6 ▾
|
import tokenize from './tokenize'
import Input from './input'
const HIGHLIGHT_THEME = {
brackets: [36, 39], // cyan
string: [31, 39], // red
'at-word': [31, 39], // red
comment: [90, 39], // gray
'{': [32, 39], // green
'}': [32, 39], // green
':': [1, 22], // bold
';': [1, 22], // bold
'(': [1, 22], // bold
')': [1, 22], // bold
}
function code(color: string | number) {
return '\u001b[' + color + 'm'
}
function terminalHighlight(css: string | any) {
const tokens = tokenize(new Input(css), { ignoreErrors: true })
const result = []
tokens.forEach(token => {
const color = HIGHLIGHT_THEME[token[0]]
if (color) {
result.push(
token[1]
.split(/\r?\n/)
.map(i => code(color[0]) + i + code(color[1]))
.join('\n')
)
} else {
result.push(token[1])
}
})
return result.join('')
}
export default terminalHighlight