reserve-cmd
Version:
command handler for REserve
37 lines (31 loc) • 764 B
JavaScript
/* eslint-disable no-control-regex */
const colors = {
1: '<b>',
22: '</b>',
3: '<i>',
23: '</i>',
4: '<u>',
24: '</u>',
39: '</span>',
0: '</span>'
}
const span = className => `<span class="${className}">`
'black,red,green,yellow,blue,magenta,cyan,white'
.split(',')
.forEach((color, index) => {
colors[`3${index}`] = span(color)
colors[`3${index};1`] = span(color)
colors[`9${index}`] = span(`${color} bright`)
})
module.exports = string => {
const html = string
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/█/g, '▮')
if (html.includes('\x1B[')) {
return html.replace(/\x1B\[([0-9;]+)m/g, (_, code) => colors[code] || '')
}
return html
}