1.3.1 • Published 4 years ago

minecraft-color-parser v1.3.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

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 parse
  • options:
    • 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 text
  • style - Element's style
  • useClasses - Use HTML classes instead of inline CSS styles (default: false)
  • tag - Use different HTML tag (default: 'span')

clean(string, onlyValidCodes)

  • string - String to clean
  • onlyValidCodes - 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
  },
  ...
}

Examples

Parse string and translate it to HTML

Demo: https://codepen.io/rqbik/pen/wvBgPNK

Parse string with multiple color codes and translate it to HTML

Demo: https://codepen.io/rqbik/pen/YzPNEgg

Parse string with 'obfuscated' style and animate it

Demo: https://codepen.io/rqbik/pen/dyPNZxr

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago