diff --git a/dist/motdParser.js b/dist/motdParser.js index 90c67a1..8ebe58b 100644 --- a/dist/motdParser.js +++ b/dist/motdParser.js @@ -1 +1 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.motdParser=void 0;const utils_1=require("./utils"),extras={"\xa7k":"obfuscated;","\xa7l":"font-weight: bold;","\xa7m":"text-decoration: line-through;","\xa7n":"text-decoration: underline;","\xa7o":"font-style: italic;","\xa7r":"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;"},extraFontStyles={bold:"font-weight: bold;",italic:"font-style: italic;",underline:"text-decoration:underline;",strikethrough:"text-decoration: line-through;",obfuscated:"mc_obfuscated;",reset:"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;"},textToJsonExtras={"\xa7k":"obfuscated","\xa7l":"bold","\xa7m":"strikethrough","\xa7n":"underline","\xa7o":"italic","\xa7r":"","\xa7K":"obfuscated","\xa7L":"bold","\xa7M":"strikethrough","\xa7N":"underline","\xa7O":"italic","\xa7P":""},colorCodeToHex={"\xa70":"#000000","\xa71":"#0000AA","\xa72":"#00AA00","\xa73":"#00AAAA","\xa74":"#AA0000","\xa75":"#AA00AA","\xa76":"#FFAA00","\xa77":"#AAAAAA","\xa78":"#555555","\xa79":"#5555FF","\xa7a":"#55FF55","\xa7b":"#55FFFF","\xa7c":"#FF5555","\xa7d":"#FF55FF","\xa7e":"#FFFF55","\xa7f":"#FFFFFF"},extraColorsToHex={black:"#000000",dark_blue:"#0000AA",dark_green:"#00AA00",dark_aqua:"#00AAAA",dark_red:"#AA0000",dark_purple:"#AA00AA",gold:"#FFAA00",gray:"#AAAAAA",dark_gray:"#555555",blue:"#5555FF",green:"#55FF55",aqua:"#55FFFF",red:"#FF5555",light_purple:"#FF55FF",yellow:"#FFFF55",white:"#FFFFFF"};function cleanTags(a){return a.replace(/(?:§)([0-9a-fA-FklmnorFKLMNOR])/g,"")}function textToHTML(a){let b=new RegExp(/([§][0-9a-fA-FklmnorFKLMNOR])/g.source),c=a.split(b),e="",f="",d="";return c.forEach((g,h)=>{let b=g.toLowerCase();if(colorCodeToHex.hasOwnProperty(b))f=colorCodeToHex[b];else if(extras.hasOwnProperty(b))e=extras[b];else{let c="",a=g;""!==f&&(c=`color:${f};`),""!==a&&(a=(0,utils_1.htmlStringFormatting)(a),0!==c.length||0!==e.length?d+=`${a}`:d+=a)}}),d}function parseTextToJSON(c){let d=new RegExp(/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g.source),e=c.split(d),f="",g="",a={text:"",extra:[]};e.forEach(d=>{let b=d.toLowerCase();if(colorCodeToHex.hasOwnProperty(b))g=colorCodeToHex[b];else if(textToJsonExtras.hasOwnProperty(b))f=textToJsonExtras[b];else{let c={text:"",extra:[]};""!==f&&(c[f]=!0),c.text=d,""!==g&&(c.color=g),"object"==typeof a.extra&&a.extra.push(c)}});let b=[];return a.extra&&a.extra.forEach((c,d)=>{""===c.text&&a.extra&&"object"==typeof a.extra[d+1]&&b.push(Object.assign(Object.assign({},c),a.extra[d+1]))}),b=b.filter(a=>""!==a.text),{text:a.text,extra:b}}function parseJSONToHTML(c){let f="",e="",b="";for(let a of Object.keys(c)){if(a=a.toLowerCase(),extraFontStyles.hasOwnProperty(a)){c[a]?b+=`${extraFontStyles[a]}`:"bold"===a?b+="font-weight:normal !important;":"italic"===a?b+="font-style: normal !important;":"underline"===a?b+="text-decoration: none !important;":"strikethrough"===a?b+="text-decoration: line-through !important;":"obfuscated"===a?b+="":b="";continue}if("text"===a&&"string"==typeof c.text){f+=textToHTML(c.text);continue}if("color"===a){let d=c[a];if("string"==typeof d){if(extraColorsToHex.hasOwnProperty(d)){e=`color: ${extraColorsToHex[d]};`;continue}if(colorCodeToHex.hasOwnProperty(d)){e=`color: ${colorCodeToHex[d]};`;continue}e=`color: ${d};`;continue}}if("extra"===a&&"object"==typeof c.extra)for(let g of c.extra)(0,utils_1.isMotdJSONType)(g)&&(f+=parseJSONToHTML(g))}return 0!==b.length||0!==e.length?`${f}`:f}function jsonEnterRender(a){let b=parseJSONToHTML(JSON.parse(JSON.stringify(a)));return b}function textEnterRender(a){let b=textToHTML(a);return b}function autoToHtml(a){return"object"==typeof a?jsonEnterRender(a):"string"==typeof a?jsonEnterRender(parseTextToJSON(a)):"unknown motd data type"}exports.motdParser={cleanTags,textToHTML,textToJSON:parseTextToJSON,JSONToHtml:parseJSONToHTML,jsonEnterRender,textEnterRender,autoToHtml} \ No newline at end of file +"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.motdParser=void 0;const utils_1=require("./utils"),extras={"\xa7k":"obfuscated;","\xa7l":"font-weight: bold;","\xa7m":"text-decoration: line-through;","\xa7n":"text-decoration: underline;","\xa7o":"font-style: italic;","\xa7r":"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;"},extraFontStyles={bold:"font-weight: bold;",italic:"font-style: italic;",underlined:"text-decoration:underline;",strikethrough:"text-decoration: line-through;",obfuscated:"mc_obfuscated;",reset:"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;"},textToJsonExtras={"\xa7k":"obfuscated","\xa7l":"bold","\xa7m":"strikethrough","\xa7n":"underlined","\xa7o":"italic","\xa7r":"","\xa7K":"obfuscated","\xa7L":"bold","\xa7M":"strikethrough","\xa7N":"underlined","\xa7O":"italic","\xa7P":""},colorCodeToHex={"\xa70":"#000000","\xa71":"#0000AA","\xa72":"#00AA00","\xa73":"#00AAAA","\xa74":"#AA0000","\xa75":"#AA00AA","\xa76":"#FFAA00","\xa77":"#AAAAAA","\xa78":"#555555","\xa79":"#5555FF","\xa7a":"#55FF55","\xa7b":"#55FFFF","\xa7c":"#FF5555","\xa7d":"#FF55FF","\xa7e":"#FFFF55","\xa7f":"#FFFFFF"},extraColorsToHex={black:"#000000",dark_blue:"#0000AA",dark_green:"#00AA00",dark_aqua:"#00AAAA",dark_red:"#AA0000",dark_purple:"#AA00AA",gold:"#FFAA00",gray:"#AAAAAA",dark_gray:"#555555",blue:"#5555FF",green:"#55FF55",aqua:"#55FFFF",red:"#FF5555",light_purple:"#FF55FF",yellow:"#FFFF55",white:"#FFFFFF"};function cleanTags(a){return a.replace(/(?:§)([0-9a-fA-FklmnorFKLMNOR])/g,"")}function textToHTML(a){let b=new RegExp(/([§][0-9a-fA-FklmnorFKLMNOR])/g.source),c=a.split(b),e="",f="",d="";return c.forEach((g,h)=>{let b=g.toLowerCase();if(colorCodeToHex.hasOwnProperty(b))f=colorCodeToHex[b];else if(extras.hasOwnProperty(b))e=extras[b];else{let c="",a=g;""!==f&&(c=`color:${f};`),""!==a&&(a=(0,utils_1.htmlStringFormatting)(a),0!==c.length||0!==e.length?d+=`${a}`:d+=a)}}),d}function parseTextToJSON(c){let d=new RegExp(/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g.source),e=c.split(d),f="",g="",a={text:"",extra:[]};e.forEach(d=>{let b=d.toLowerCase();if(colorCodeToHex.hasOwnProperty(b))g=colorCodeToHex[b];else if(textToJsonExtras.hasOwnProperty(b))f=textToJsonExtras[b];else{let c={text:"",extra:[]};""!==f&&(c[f]=!0),c.text=d,""!==g&&(c.color=g),"object"==typeof a.extra&&a.extra.push(c)}});let b=[];return a.extra&&a.extra.forEach((c,d)=>{""===c.text&&a.extra&&"object"==typeof a.extra[d+1]&&b.push(Object.assign(Object.assign({},c),a.extra[d+1]))}),b=b.filter(a=>""!==a.text),{text:a.text,extra:b}}function parseJSONToHTML(c){let f="",e="",b="";for(let a of Object.keys(c)){if(a=a.toLowerCase(),extraFontStyles.hasOwnProperty(a)){c[a]?b+=`${extraFontStyles[a]}`:"bold"===a?b+="font-weight:normal !important;":"italic"===a?b+="font-style: normal !important;":"underline"===a?b+="text-decoration: none !important;":"strikethrough"===a?b+="text-decoration: line-through !important;":"obfuscated"===a?b+="":b="";continue}if("text"===a&&("string"==typeof c.text||"number"==typeof c.text)){f+=textToHTML(String(c.text));continue}if("color"===a){let d=c[a];if("string"==typeof d){if(extraColorsToHex.hasOwnProperty(d)){e=`color: ${extraColorsToHex[d]};`;continue}if(colorCodeToHex.hasOwnProperty(d)){e=`color: ${colorCodeToHex[d]};`;continue}e=`color: ${d};`;continue}}if("extra"===a&&"object"==typeof c.extra)for(let g of c.extra)(0,utils_1.isMotdJSONType)(g)&&(f+=parseJSONToHTML(g))}return 0!==b.length||0!==e.length?`${f}`:f}function jsonEnterRender(a){let b=parseJSONToHTML(JSON.parse(JSON.stringify(a)));return b}function textEnterRender(a){let b=textToHTML(a);return b}function autoToHtml(a){return"object"==typeof a?jsonEnterRender(a):"string"==typeof a?jsonEnterRender(parseTextToJSON(a)):"unknown motd data type"}exports.motdParser={cleanTags,textToHTML,textToJSON:parseTextToJSON,JSONToHtml:parseJSONToHTML,jsonEnterRender,textEnterRender,autoToHtml} \ No newline at end of file diff --git a/dist/types.d.ts b/dist/types.d.ts index 6ce410d..bf4068f 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -1,11 +1,20 @@ interface extraLibraryType { [key: string]: string; } + interface motdJsonType { + text: string | Number; + extra?: { + color?: string; + text?: string | Number; + bold?: boolean; + strikethrough?: boolean; + underlined?: boolean; + obfuscated?: boolean; + italic?: boolean; + extra?: object[]; + }[]; [key: string]: string | boolean | object | Array | undefined; - font?: string; - color?: string; - text?: string; - extra?: object[]; } + export type { extraLibraryType, motdJsonType }; diff --git a/dist/types.js b/dist/types.js deleted file mode 100644 index cce858d..0000000 --- a/dist/types.js +++ /dev/null @@ -1 +0,0 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0}) \ No newline at end of file diff --git a/package.json b/package.json index e7b827b..9299641 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@sfirew/mc-motd-parser", "description": "Minecraft Server MOTD Parser, can convert to html, json, text.", - "version": "1.0.9", + "version": "1.0.9-1", "main": "./dist/index.js", "types": "./dist/index.d.ts", "license": "MIT", diff --git a/src/motdParser.ts b/src/motdParser.ts index 7bd736b..a91438b 100644 --- a/src/motdParser.ts +++ b/src/motdParser.ts @@ -11,7 +11,7 @@ import { import { isMotdJSONType, htmlStringFormatting } from './utils'; - +// color code to font styles const extras: extraLibraryType = { '§k': 'obfuscated;', '§l': 'font-weight: bold;', @@ -21,6 +21,7 @@ const extras: extraLibraryType = { '§r': 'color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;', }; +// json extra font styles const extraFontStyles: extraLibraryType = { 'bold': 'font-weight: bold;', 'italic': 'font-style: italic;', @@ -30,6 +31,7 @@ const extraFontStyles: extraLibraryType = { 'reset': 'color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;', }; +// text to json extra name const textToJsonExtras: extraLibraryType = { '§k': 'obfuscated', '§l': 'bold', @@ -46,6 +48,7 @@ const textToJsonExtras: extraLibraryType = { '§P': '' }; +// base color hex const colorCodeToHex: extraLibraryType = { '§0': '#000000', '§1': '#0000AA', @@ -65,6 +68,7 @@ const colorCodeToHex: extraLibraryType = { '§f': '#FFFFFF', }; +// json extra to hex color const extraColorsToHex: extraLibraryType = { 'black': '#000000', 'dark_blue': '#0000AA', @@ -86,9 +90,13 @@ const extraColorsToHex: extraLibraryType = { + + + + // clean tags /** - * ### `cleanTags(string)` + * #### `cleanTags(string)` * Clean all tags from motd source string. */ function cleanTags(text: string) { @@ -100,12 +108,9 @@ function cleanTags(text: string) { return textResult } - - - // text to html /** - * ### `textToHTML(string)` + * #### `textToHTML(string)` * Convert motd text to html. */ function textToHTML(motdString: string) { @@ -149,6 +154,7 @@ function textToHTML(motdString: string) { //console.log('color: ' + colorHex) //console.log('text: ' + item) //console.log('---------------------------------') + // replace html tags textContent = htmlStringFormatting(textContent) if (resultColor.length !== 0 || fontStyle.length !== 0) { @@ -163,11 +169,9 @@ function textToHTML(motdString: string) { return resultHTML } - - // text to json /** - * ### `textToJSON(string)` + * #### `textToJSON(string)` * Convert motd text to JSON. */ function parseTextToJSON(text: string) { @@ -227,19 +231,21 @@ function parseTextToJSON(text: string) { }) // console.log('resultObject', resultObject); + let newExtra: Array = []; - // if text is '', remote it and merge to next array + // if text is '', remove it and merge to next array resultObject.extra && resultObject.extra.forEach((item, index) => { // console.log('item', item); if (item.text === '') { if (resultObject.extra && typeof resultObject.extra[index + 1] === 'object') { newExtra.push({ - ...item as any, + ...item as motdJsonType, ...resultObject.extra[index + 1], }) } } - }) + }); + // remove blank content newExtra = newExtra.filter(item => item.text !== ''); // console.log('newExtra', newExtra); @@ -249,11 +255,9 @@ function parseTextToJSON(text: string) { }; } - - // json convert to html /** - * ### `JSONToString(string)` + * #### `JSONToString(string)` * Convert JSON to HTML. */ function parseJSONToHTML(sourceJson: motdJsonType) { @@ -295,9 +299,7 @@ function parseJSONToHTML(sourceJson: motdJsonType) { // text if (key === "text") { if (typeof sourceJson.text === 'string' || typeof sourceJson.text === 'number') { - //console.log(textToHtml(sourceJson.text)) - - // replace space to   code + // convert all type to string htmlElement += textToHTML(String(sourceJson.text)); continue; } @@ -353,10 +355,9 @@ function parseJSONToHTML(sourceJson: motdJsonType) { -// JSON 完整轉換 包含 換行等 +// JSON full convert include newline function jsonEnterRender(json: motdJsonType | object) { // console.log('json', json); - // JSON.stringify(json).split('\\n').join("
") const resultMotdHtml = parseJSONToHTML(JSON.parse(JSON.stringify(json))); //console.log('motd: ' + resultMotd) @@ -365,7 +366,7 @@ function jsonEnterRender(json: motdJsonType | object) { -// TEXT 完整轉換 包含 換行等 +// TEXT full convert include newline function textEnterRender(text: string) { const resultMotdHtml = textToHTML(text); diff --git a/src/types.d.ts b/src/types.d.ts index 6ce410d..bf4068f 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,11 +1,20 @@ interface extraLibraryType { [key: string]: string; } + interface motdJsonType { + text: string | Number; + extra?: { + color?: string; + text?: string | Number; + bold?: boolean; + strikethrough?: boolean; + underlined?: boolean; + obfuscated?: boolean; + italic?: boolean; + extra?: object[]; + }[]; [key: string]: string | boolean | object | Array | undefined; - font?: string; - color?: string; - text?: string; - extra?: object[]; } + export type { extraLibraryType, motdJsonType }; diff --git a/src/utils.ts b/src/utils.ts index 1b3e654..abc46c9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -14,6 +14,10 @@ export function isMotdJSONType(object: any): object is motdJsonType { } +/** + * + * #### replace all html tags to &... + */ export const htmlStringFormatting = (text: string): string => { return text // space @@ -24,7 +28,7 @@ export const htmlStringFormatting = (text: string): string => { .replace(/>/g, '>') .replace(/\"/g, '"') .replace(/\'/g, ''') - // return + // return .replace(/\n/g, '
'); // .replace(/\//g, '/'); } \ No newline at end of file diff --git a/test/bugs.ts b/test/bugs.ts index 349660a..fd1e3e1 100644 --- a/test/bugs.ts +++ b/test/bugs.ts @@ -1,17 +1,17 @@ import { motdParser } from '../src'; import type { motdJsonType } from '../src/types'; -const testFromCode = `&r&f &r&9&m&l &r&8&m&l[ &r&f &r&6&lMineplex&r&f &r&f&lGames&r&f &r&8&m&l ]&r&9&m&l &r&f - &r&e&lSTATS REVAMP&r &a&l&n testesest -`; -const replacedString = testFromCode.replace(/&/g, '§'); -// console.log(motdParser.autoToHtml(replacedString)); +// const testFromCode = `&r&f &r&9&m&l &r&8&m&l[ &r&f &r&6&lMineplex&r&f &r&f&lGames&r&f &r&8&m&l ]&r&9&m&l &r&f +// &r&e&lSTATS REVAMP&r &a&l&n testesest +// `; +// const replacedString = testFromCode.replace(/&/g, '§'); +// console.log(motdParser.autoToHtml(replacedString)); -const testFromJson: motdJsonType = { +const testNumberContentFromJson: motdJsonType = { "extra": [{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"green","text":"life boost: "}, {"italic":false,"color":"white","text":5}], @@ -19,4 +19,4 @@ const testFromJson: motdJsonType = { } -console.log(motdParser.autoToHtml(testFromJson)); \ No newline at end of file +console.log(motdParser.autoToHtml(testNumberContentFromJson)); \ No newline at end of file