1.3.1 • Published 4 years ago
minecraft-color-parser v1.3.1
minecraft-color-parser
Parse strings that contain minecraft color codes, plus cleaning and translating string with style to HTML
Install:
$ npm i minecraft-color-parser
Usage:
Parse
const { parse, clean } = require('minecraft-color-parser');
const parsed = parse('§a§oItalic green string!');
console.log(parsed);
// [
// {
// text: 'Italic green string!',
// style: {
// color: 'green',
// bold: false,
// italic: true,
// obfuscated: false,
// linethrough: false,
// underline: false
// }
// }
// ]
Clean
const { clean } = require('minecraft-color-parser');
const cleaned = clean('§aString with color code');
console.log(cleaned);
// String with color code
toHTML
const { parse, toHTML } = require('minecraft-color-parser');
const parsed = parse('§9§kObfuscated blue string');
const html = toHTML(parsed[0].text, parsed[0].style);
console.log(html);
// <span class="obfuscated" style="color: 5555FF">Obfuscated blue string</span>
API
parse(input, options)
input
- String or an array of strings to parseoptions
:html
- Automatically parse string into HTML tag (default: false)errorOnInvalidCodes
- Throw an error when encountering an invalid color/style code (for example, '§q') (default: false)useAmpersand
- Parse color/style codes that use ampersand (for example, '&a') (default: false)
toHTML(string, style, useClasses, tag)
string
- Element's textstyle
- Element's styleuseClasses
- Use HTML classes instead of inline CSS styles (default: false)tag
- Use different HTML tag (default: 'span')
clean(string, onlyValidCodes)
string
- String to cleanonlyValidCodes
- Clean only valid color/style codes (for example, '§a' is a valid color code, while '§q' is not) (default: false)
colors
An object with minecraft color codes
Structure
{
color_name: {
color: 'hex_value',
code: 'color_code',
},
...
}
styles
An object with minecraft style codes
Structure
{
style: {
css: 'css_style: property;',
code: 'color_code', // Exception: 'obfuscated' style.
// It uses HTML classes instead of CSS styles
},
...
}