UNPKG

@trap_stevo/loml

Version:

Unleash the power of a language crafted for dynamic text styling. Loml transforms your words into code—bold, italic, colorful, and linkable. Turn plain text into expressive, richly formatted messages with simple, elegant commands. Loml empowers you with a

97 lines (96 loc) 3.1 kB
"use strict"; class Loml { constructor() { this.message = ""; } static styleToString(styles = {}) { const camelCaseToKebabCase = str => str.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`); const styleString = Object.entries(styles).map(([key, value]) => { const kebabKey = camelCaseToKebabCase(key); if (kebabKey.startsWith("webkit")) { return `-webkit-${kebabKey.slice(6)} : ${value}`; } return `${kebabKey} : ${value}`; }).join("; "); return styleString ? styleString + ";" : ""; } text(text, styles = {}) { const styleString = Loml.styleToString(styles); this.message += `<span style="${styleString}">${text}</span>`; return this; } bold(text, styles = {}) { const styleString = Loml.styleToString(styles); this.message += `<strong style="${styleString}">${text}</strong>`; return this; } italic(text, styles = {}) { const styleString = Loml.styleToString(styles); this.message += `<em style="${styleString}">${text}</em>`; return this; } br() { this.message += "<br>"; return this; } link(text, href, styles = {}) { const styleString = Loml.styleToString(styles); this.message += `<a href="${href}" style="${styleString}" target="_blank">${text}</a>`; return this; } color(text, color, styles = {}) { styles.color = color; const styleString = Loml.styleToString(styles); this.message += `<span style="${styleString}">${text}</span>`; return this; } gradientText(text, colors = [], direction = "to right", styles = {}) { if (colors.length === 0) { return this; } const gradient = `linear-gradient(${direction}, ${colors.join(", ")})`; styles.backgroundImage = gradient; styles.WebkitBackgroundClip = "text"; styles.color = "transparent"; const styleString = Loml.styleToString(styles); this.message += `<span style="${styleString}">${text}</span>`; return this; } element(tag, text, styles = {}) { const styleString = Loml.styleToString(styles); this.message += `<${tag} style="${styleString}">${text}</${tag}>`; return this; } build() { return this.message; } clear() { this.message = ""; return this; } static quickText(text, styles = {}) { return new Loml().text(text, styles).build(); } static quickBold(text, styles = {}) { return new Loml().bold(text, styles).build(); } static quickItalic(text, styles = {}) { return new Loml().italic(text, styles).build(); } static quickLink(text, href, styles = {}) { return new Loml().link(text, href, styles).build(); } static quickColor(text, color, styles = {}) { return new Loml().color(text, color, styles).build(); } static quickGradientText(text, colors = [], direction = "to right", styles = {}) { return new Loml().gradientText(text, colors, direction, styles).build(); } static quickElement(tag, text, styles = {}) { return new Loml().element(tag, text, styles).build(); } static quickBr() { return "<br>"; } } module.exports = Loml;