@sfirew/minecraft-motd-parser 1.1.2-dev.1 → 1.1.2-dev.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +0 -7
- package/types/motdParser.d.ts +5 -5
- package/types/parser/index.d.ts +3 -3
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/minecraft-motd-parser.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
var T=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var E=(t,e)=>{for(var r in e)T(t,r,{get:e[r],enumerable:!0})},C=(t,e,r,p)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of j(e))!k.call(t,l)&&l!==r&&T(t,l,{get:()=>e[l],enumerable:!(p=H(e,l))||p.enumerable});return t};var M=t=>C(T({},"__esModule",{value:!0}),t);var R={};E(R,{default:()=>N});module.exports=M(R);function b(t){return!!t}function g(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/\n/g,"<br/>")}function O(t){return t.replace(/<[^>]*>?/gm,"")}function L(t){let e=/(?:§)([0-9a-fA-FklmnorFKLMNOR])/g,r="";return r=t.replace(e,""),r}var y={"\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;"},u={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;"},A={"\xA7k":"obfuscated","\xA7l":"bold","\xA7m":"strikethrough","\xA7n":"underlined","\xA7o":"italic","\xA7r":"reset"},x={"\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"},h={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 m(t){let e=t,r=/([§][0-9a-fA-FklmnorFKLMNOR])/g,p=new RegExp(r.source),l=e.split(p),o="",n="",a="";return l.forEach(s=>{let i=s.toLowerCase();if(Object.hasOwn(x,i))n=x[i];else if(Object.hasOwn(y,i))o=y[i];else{let f="",c=s;n!==""&&(f=`color:${n};`),c!==""&&(c=g(c),f.length!==0||o.length!==0?a+=`<span style="${o}${f}">${c}</span>`:a+=c)}}),a}function d(t){let e="",r="",p="";for(let o of Object.keys(t)){if(o=o.toLowerCase(),Object.hasOwn(u,o)&&t[o]&&(p+=`${u[o]}`),o==="color"){let n=t[o];if(typeof n=="string")if(Object.hasOwn(h,n)){r=`color:${h[n]};`;continue}else if(Object.hasOwn(x,n)){r=`color:${x[n]};`;continue}else{r=`color:${n};`;continue}}if(o==="extra"&&typeof t.extra=="object"){t.text!==void 0&&(typeof t.text=="string"||typeof t.text=="number")&&(e+=m(String(t.text)));for(let n of t.extra)b(n)&&(e+=d(n))}}if(t.extra===void 0&&t.text!==void 0){let o=t.text;(typeof t.text=="string"||typeof t.text=="number")&&(e+=m(String(o)))}let l="";return p.length!==0||r.length!==0?l=`<span style="${p+r}">${e}</span>`:l=e,l}function F(t){let e=t,r=/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g,p=new RegExp(r.source),l=e.split(p),o="",n="",a={text:"",extra:[]};l.forEach(i=>{let f=i.toLowerCase();if(Object.hasOwn(x,f))n=x[f];else if(Object.hasOwn(A,f))o=A[f];else{let c={text:"",extra:[]};o!==""&&(c[o]=!0),c.text=i,n!==""&&(c.color=n),typeof a.extra=="object"&&a.extra.push(c)}});let s=[];return a.extra&&(a.extra.length>1?a.extra.forEach((i,f)=>{i.text===""?a.extra&&typeof a.extra[f+1]=="object"&&s.push({...i,...a.extra[f+1]}):i.text!==(s[s.length-1]&&s[s.length-1].text)&&s.push(i)}):s.push(a.extra[0])),s=s.filter(i=>i.text!==""),{text:a.text,extra:s}}function S(t){return d(JSON.parse(JSON.stringify(t)))}function J(t){return typeof t=="object"?S(t):typeof t=="string"?m(t):"unknown motd data type"}var w={textToHTML:m,textToJSON:F,JSONToHTML:d,jsonRender:S,autoToHTML:J,htmlStringFormatting:g,cleanTags:L,cleanHtmlTags:O};var N=w;0&&(module.exports={});
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/styleLibrary.ts","../src/parser/textToHtml.ts","../src/parser/jsonToHtml.ts","../src/parser/textToJSON.ts","../src/motdParser.ts"],"sourcesContent":["import motdParser from \"./motdParser\";\n\nexport { motdParser };\nexport const cleanTags = motdParser.cleanTags;\nexport const textToHTML = motdParser.textToHTML;\nexport const textToJSON = motdParser.textToJSON;\nexport const JSONToHTML = motdParser.JSONToHTML;\nexport const jsonRender = motdParser.jsonRender;\nexport const autoToHTML = motdParser.autoToHTML;\n\nexport default motdParser;\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { motdJsonType } from \"./types\";\n\n\n\n\n// 類型檢查\nexport function isMotdJSONType(object: object): object is motdJsonType {\n return !!object;\n};\n\n\n\n/**\n *\n * replace all html tags to &...\n */\nexport function htmlStringFormatting(text: string): string {\n return (\n text\n // space\n // .replace(/ /g, \"\\u00a0\")\n //\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\")\n // return\n .replace(/\\n/g, \"<br/>\")\n );\n // .replace(/\\//g, '/');\n};\n\n\n\n/**\n * clean html tags\n * \n * @param text\n * example: `<span>hello world</span>`\n * \n * result: `hello world`\n */\nexport function cleanHtmlTags(text: string): string {\n return text.replace(/<[^>]*>?/gm, '');\n};\n\n\n\n/**\n * clean motd tags\n * \n * Clean all tags from motd source string.\n * \n * @param {string} text - motd string include tag § will remove\n */\nexport function cleanTags(text: string) {\n const REGEX = /(?:§)([0-9a-fA-FklmnorFKLMNOR])/g;\n let textResult = \"\";\n\n textResult = text.replace(REGEX, \"\");\n\n return textResult;\n};\n","import { extraLibraryType } from \"./types\";\n\n\n\n// color code to font styles\nconst extras: extraLibraryType = {\n \"§k\": \"obfuscated;\",\n \"§l\": \"font-weight: bold;\",\n \"§m\": \"text-decoration: line-through;\",\n \"§n\": \"text-decoration: underline;\",\n \"§o\": \"font-style: italic;\",\n \"§r\": \"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;\",\n};\n\n// json extra font styles\nconst extraFontStyles: extraLibraryType = {\n bold: \"font-weight: bold;\",\n italic: \"font-style: italic;\",\n underlined: \"text-decoration:underline;\",\n strikethrough: \"text-decoration: line-through;\",\n obfuscated: \"mc_obfuscated;\",\n reset: \"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;\",\n};\n\n// text to json extra name\nconst textToJsonExtras: extraLibraryType = {\n \"§k\": \"obfuscated\",\n \"§l\": \"bold\",\n \"§m\": \"strikethrough\",\n \"§n\": \"underlined\",\n \"§o\": \"italic\",\n \"§r\": \"reset\",\n};\n\n// base color hex\nconst colorCodeToHex: extraLibraryType = {\n \"§0\": \"#000000\",\n \"§1\": \"#0000AA\",\n \"§2\": \"#00AA00\",\n \"§3\": \"#00AAAA\",\n \"§4\": \"#AA0000\",\n \"§5\": \"#AA00AA\",\n \"§6\": \"#FFAA00\",\n \"§7\": \"#AAAAAA\",\n \"§8\": \"#555555\",\n \"§9\": \"#5555FF\",\n \"§a\": \"#55FF55\",\n \"§b\": \"#55FFFF\",\n \"§c\": \"#FF5555\",\n \"§d\": \"#FF55FF\",\n \"§e\": \"#FFFF55\",\n \"§f\": \"#FFFFFF\",\n};\n\n// json extra to hex color\nconst extraColorsToHex: extraLibraryType = {\n black: \"#000000\",\n dark_blue: \"#0000AA\",\n dark_green: \"#00AA00\",\n dark_aqua: \"#00AAAA\",\n dark_red: \"#AA0000\",\n dark_purple: \"#AA00AA\",\n gold: \"#FFAA00\",\n gray: \"#AAAAAA\",\n dark_gray: \"#555555\",\n blue: \"#5555FF\",\n green: \"#55FF55\",\n aqua: \"#55FFFF\",\n red: \"#FF5555\",\n light_purple: \"#FF55FF\",\n yellow: \"#FFFF55\",\n white: \"#FFFFFF\",\n};\n\n\n\nexport {\n extras,\n extraFontStyles,\n textToJsonExtras,\n colorCodeToHex,\n extraColorsToHex,\n};\n","import {\n extras,\n colorCodeToHex,\n} from '../styleLibrary';\nimport {\n htmlStringFormatting,\n} from \"../utils\";\n\n\n\n\n\n\n// text to html\n/**\n * Convert motd text to html.\n * @param motdString\n */\nexport default function textToHTML(motdString: string) {\n const motdText = motdString;\n\n const colorCodeReg = /([§][0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const codeSplit = motdText.split(codeREGEX);\n\n let fontStyle = \"\";\n let colorHex = \"\";\n let resultHTML = \"\";\n\n codeSplit.forEach((item) => {\n const motdStringToLowerCase = item.toLowerCase();\n\n // 過濾 hex\n if (Object.hasOwn(colorCodeToHex, motdStringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[motdStringToLowerCase];\n\n // 過濾文字 style\n } else if (Object.hasOwn(extras, motdStringToLowerCase)) {\n // font style code 轉換\n //console.log(`偵測出 style ${ extras[item] }`)\n fontStyle = extras[motdStringToLowerCase];\n //console.log('textFont: ' + fontStyle)\n // 正常文字\n } else {\n let resultColor = \"\";\n let textContent = item;\n //console.log(fontStyle)\n\n // 檢查 Hex color\n if (colorHex !== \"\") {\n resultColor = `color:${colorHex};`;\n }\n\n if (textContent !== \"\") {\n //console.log('font: ' + fontStyle)\n //console.log('color: ' + colorHex)\n //console.log('text: ' + item)\n //console.log('---------------------------------')\n // replace html tags\n textContent = htmlStringFormatting(textContent);\n\n if (resultColor.length !== 0 || fontStyle.length !== 0) {\n resultHTML += `<span style=\"${fontStyle}${resultColor}\">${textContent}</span>`;\n } else {\n resultHTML += textContent;\n }\n }\n }\n });\n\n return resultHTML;\n}\n","import { motdJsonType } from \"../types\";\nimport {\n extraFontStyles,\n colorCodeToHex,\n extraColorsToHex,\n} from '../styleLibrary';\nimport {\n isMotdJSONType,\n} from \"../utils\";\nimport textToHTML from \"./textToHtml\";\n\n\n\n\n\n\n// json convert to html\n/**\n * Convert JSON to HTML.\n * \n * @param sourceJson \n */\nexport default function parseJSONToHTML(\n sourceJson: motdJsonType,\n) {\n let htmlElement = \"\";\n let colorHex = \"\";\n let fontStyle = \"\";\n\n // console.log('sourceJson', sourceJson);\n // console.log('---------');\n\n for (let key of Object.keys(sourceJson)) {\n // console.log('sourceJson key', key);\n key = key.toLowerCase();\n\n // text styles\n if (Object.hasOwn(extraFontStyles, key)) {\n if (sourceJson[key]) {\n fontStyle += `${extraFontStyles[key]}`;\n }\n }\n\n // ---------- old text process ----------\n // if (key === \"text\") {\n // if (\n // typeof sourceJson.text === \"string\" ||\n // typeof sourceJson.text === \"number\"\n // ) {\n // // convert all type to string\n // htmlElement += textToHTML(String(sourceJson.text));\n // }\n // }\n\n // color\n if (key === \"color\") {\n const colorKey = sourceJson[key];\n\n if (typeof colorKey === \"string\") {\n // Hex color\n if (Object.hasOwn(extraColorsToHex, colorKey)) {\n colorHex = `color:${extraColorsToHex[colorKey]};`;\n continue;\n // color code\n } else if (Object.hasOwn(colorCodeToHex, colorKey)) {\n colorHex = `color:${colorCodeToHex[colorKey]};`;\n continue;\n // custom color\n } else {\n // custom hex color code mode\n colorHex = `color:${colorKey};`;\n continue;\n }\n }\n }\n\n // extra\n if (key === \"extra\" && typeof sourceJson.extra === \"object\") {\n // ---------- with extra text ----------\n if (sourceJson.text !== undefined\n && (typeof sourceJson.text === \"string\" || typeof sourceJson.text === \"number\")\n ) {\n // content to html\n htmlElement += textToHTML(String(sourceJson.text));\n }\n\n // ---------- foreach extra data and parse ----------\n for (const sourceJsonExtra of sourceJson.extra) {\n // console.log('sourceJsonExtra', sourceJsonExtra);\n if (isMotdJSONType(sourceJsonExtra)) {\n htmlElement += parseJSONToHTML(sourceJsonExtra);\n }\n }\n }\n }\n\n // ---------- without extra text content ----------\n if (sourceJson.extra === undefined && sourceJson.text !== undefined) {\n const currentText = sourceJson.text;\n if (\n typeof sourceJson.text === \"string\" ||\n typeof sourceJson.text === \"number\"\n ) {\n // convert all type to string\n htmlElement += textToHTML(String(currentText));\n }\n }\n\n\n\n let returnHTML = \"\";\n if (fontStyle.length !== 0 || colorHex.length !== 0) {\n returnHTML = `<span style=\"${fontStyle + colorHex}\">${htmlElement}</span>`;\n } else {\n returnHTML = htmlElement;\n }\n\n return returnHTML;\n}\n","import { motdJsonType } from \"../types\";\nimport {\n textToJsonExtras,\n colorCodeToHex,\n} from '../styleLibrary';\n\n\n\n\n\n\n// text to json\n/**\n * Convert motd text to JSON.\n * \n * @param text\n */\nexport default function parseTextToJSON(text: string) {\n const motdText = text;\n\n // color code regex: /([§][0-9a-fklmnor])/g\n // color hex regex: /^#(?:[0-9a-f]{3}){1,2}$/g\n const colorCodeReg = /([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const textSplit = motdText.split(codeREGEX);\n let fontStyle = \"\";\n let colorHex = \"\";\n\n const resultObject: motdJsonType = {\n text: \"\",\n extra: [],\n };\n\n // console.log('textSplit', textSplit);\n textSplit.forEach((item) => {\n const stringToLowerCase = item.toLowerCase();\n\n // color code 轉換成 hex\n if (Object.hasOwn(colorCodeToHex, stringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[stringToLowerCase];\n } else if (Object.hasOwn(textToJsonExtras, stringToLowerCase)) {\n // font style code 轉換\n //console.log(`偵測出 style ${ textToJsonExtras[item] }`)\n fontStyle = textToJsonExtras[stringToLowerCase];\n } else {\n const innerObject: motdJsonType = {\n text: \"\",\n extra: [],\n };\n\n // 其餘字串\n if (fontStyle !== \"\") {\n innerObject[fontStyle] = true;\n }\n\n innerObject.text = item;\n\n if (colorHex !== \"\") {\n innerObject.color = colorHex;\n }\n\n if (typeof resultObject.extra === \"object\") {\n resultObject.extra.push(innerObject);\n }\n }\n });\n\n // code styles merge\n let newExtra: Array<motdJsonType> = [];\n // console.log('resultObject', resultObject);\n if (resultObject.extra) {\n if (resultObject.extra.length > 1) {\n // if text is '', remove it and merge to next array\n resultObject.extra.forEach((item, index) => {\n // console.log('item', item);\n if (item.text === \"\") {\n if (\n resultObject.extra\n && typeof resultObject.extra[index + 1] === \"object\"\n ) {\n newExtra.push({\n ...(item as motdJsonType),\n ...resultObject.extra[index + 1],\n });\n }\n } else {\n if (\n item.text !== (newExtra[newExtra.length - 1]\n && newExtra[newExtra.length - 1].text)\n ) {\n newExtra.push(item as motdJsonType);\n }\n }\n });\n } else {\n newExtra.push(resultObject.extra[0] as motdJsonType);\n }\n }\n\n // console.log('newExtra', newExtra);\n // remove blank content\n newExtra = newExtra.filter((item) => item.text !== \"\");\n // console.log('newExtra', newExtra);\n\n return {\n text: resultObject.text,\n extra: newExtra,\n };\n}\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { motdJsonType } from \"./types\";\nimport {\n htmlStringFormatting,\n cleanTags,\n cleanHtmlTags,\n} from \"./utils\";\nimport {\n jsonToHtml,\n textToHtml,\n textToJSON,\n} from \"./parser\";\n\n\n\n\n\n// JSON full convert include newline\nfunction jsonRender(json: motdJsonType | object) {\n return jsonToHtml(JSON.parse(JSON.stringify(json)));\n}\n\n\n\n/**\n * ### `autoToHTML(object | string)`\n * auto check data type then convert to html.\n */\nfunction autoToHTML(motd: motdJsonType | string | object): string {\n // type check\n if (typeof motd === \"object\") {\n // console.log('process mode: Object mode');\n return jsonRender(motd);\n } else if (typeof motd === \"string\") {\n // console.log('process mode: String mode');\n return textToHtml(motd);\n } else {\n return \"unknown motd data type\";\n }\n}\n\n\n\n/*\n * #### minecraft motd parser\n * * [github](https://github.com/SnowFireWolf/minecraft-motd-parser/tree/main#minecraft-server-motd-parser)\n * * [npm](https://www.npmjs.com/package/@sfirew/minecraft-motd-parser)\n *\n * (c) 2023 Kevin Zheng\n *\n * Released under the MIT license\n */\nconst defaultModules = {\n // text convert to HTML\n textToHTML: textToHtml,\n // text convert to JSON\n textToJSON: textToJSON,\n // JSON convert to HTML\n JSONToHTML: jsonToHtml,\n // JSON full convert HTML (include enter)\n jsonRender,\n // auto check type to convert\n autoToHTML,\n\n // utils\n htmlStringFormatting,\n // clean all motd tags\n cleanTags,\n // clean all html tags\n cleanHtmlTags,\n};\n\nexport default defaultModules;\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,EAAA,eAAAC,EAAA,cAAAC,EAAA,YAAAC,EAAA,eAAAC,EAAA,eAAAC,EAAA,eAAAC,EAAA,eAAAC,IAAA,eAAAC,EAAAV,GCYO,SAASW,EAAeC,EAAwC,CACrE,MAAO,CAAC,CAACA,CACX,CAQO,SA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,OAEnCA,EAAS,KAAKL,CAAoB,CAGxC,CAAC,EAEDK,EAAS,KAAKN,EAAa,MAAM,EAAkB,GAMvDM,EAAWA,EAAS,OAAQL,GAASA,EAAK,OAAS,EAAE,EAG9C,CACL,KAAMD,EAAa,KACnB,MAAOM,CACT,CACF,CCtFA,SAASE,EAAWC,EAA6B,CAC/C,OAAOC,EAAW,KAAK,MAAM,KAAK,UAAUD,CAAI,CAAC,CAAC,CACpD,CAQA,SAASE,EAAWC,EAA8C,CAEhE,OAAI,OAAOA,GAAS,SAEXJ,EAAWI,CAAI,EACb,OAAOA,GAAS,SAElBC,EAAWD,CAAI,EAEf,wBAEX,CAaA,IAAME,EAAiB,CAErB,WAAYD,EAEZ,WAAYE,EAEZ,WAAYL,EAEZ,WAAAF,EAEA,WAAAG,EAGA,qBAAAK,EAEA,UAAAC,EAEA,cAAAC,CACF,EAEOC,EAAQL,EN1ER,IAAMM,EAAYC,EAAW,UACvBC,EAAaD,EAAW,WACxBE,EAAaF,EAAW,WACxBG,EAAaH,EAAW,WACxBI,EAAaJ,EAAW,WACxBK,EAAaL,EAAW,WAE9BM,EAAQN","names":["src_exports","__export","JSONToHTML","autoToHTML","cleanTags","src_default","jsonRender","motdParser_default","textToHTML","textToJSON","__toCommonJS","isMotdJSONType","object","htmlStringFormatting","text","cleanHtmlTags","cleanTags","REGEX","textResult","extras","extraFontStyles","textToJsonExtras","colorCodeToHex","extraColorsToHex","textToHTML","motdString","motdText","colorCodeReg","codeREGEX","codeSplit","fontStyle","colorHex","resultHTML","item","motdStringToLowerCase","colorCodeToHex","extras","resultColor","textContent","htmlStringFormatting","parseJSONToHTML","sourceJson","htmlElement","colorHex","fontStyle","key","extraFontStyles","colorKey","extraColorsToHex","colorCodeToHex","textToHTML","sourceJsonExtra","isMotdJSONType","currentText","returnHTML","parseTextToJSON","text","motdText","colorCodeReg","codeREGEX","textSplit","fontStyle","colorHex","resultObject","item","stringToLowerCase","colorCodeToHex","textToJsonExtras","innerObject","newExtra","index","jsonRender","json","parseJSONToHTML","autoToHTML","motd","textToHTML","defaultModules","parseTextToJSON","htmlStringFormatting","cleanTags","cleanHtmlTags","motdParser_default","cleanTags","motdParser_default","textToHTML","textToJSON","JSONToHTML","jsonRender","autoToHTML","src_default"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/styleLibrary.ts","../src/parser/textToHTML.ts","../src/parser/JSONToHTML.ts","../src/parser/textToJSON.ts","../src/motdParser.ts"],"sourcesContent":["import motdParser from \"./motdParser\";\n\nexport default motdParser;\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { motdJsonType } from \"./types\";\n\n\n\n\n// 類型檢查\nexport function isMotdJSONType(object: object): object is motdJsonType {\n return !!object;\n};\n\n\n\n/**\n *\n * replace all html tags to &...\n */\nexport function htmlStringFormatting(text: string): string {\n return (\n text\n // space\n // .replace(/ /g, \"\\u00a0\")\n //\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\")\n // return\n .replace(/\\n/g, \"<br/>\")\n );\n // .replace(/\\//g, '/');\n};\n\n\n\n/**\n * clean html tags\n * \n * @param text\n * example: `<span>hello world</span>`\n * \n * result: `hello world`\n */\nexport function cleanHtmlTags(text: string): string {\n return text.replace(/<[^>]*>?/gm, '');\n};\n\n\n\n/**\n * clean motd tags\n * \n * Clean all tags from motd source string.\n * \n * @param {string} text - motd string include tag § will remove\n */\nexport function cleanTags(text: string) {\n const REGEX = /(?:§)([0-9a-fA-FklmnorFKLMNOR])/g;\n let textResult = \"\";\n\n textResult = text.replace(REGEX, \"\");\n\n return textResult;\n};\n","import { extraLibraryType } from \"./types\";\n\n\n\n// color code to font styles\nconst extras: extraLibraryType = {\n \"§k\": \"obfuscated;\",\n \"§l\": \"font-weight: bold;\",\n \"§m\": \"text-decoration: line-through;\",\n \"§n\": \"text-decoration: underline;\",\n \"§o\": \"font-style: italic;\",\n \"§r\": \"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;\",\n};\n\n// json extra font styles\nconst extraFontStyles: extraLibraryType = {\n bold: \"font-weight: bold;\",\n italic: \"font-style: italic;\",\n underlined: \"text-decoration:underline;\",\n strikethrough: \"text-decoration: line-through;\",\n obfuscated: \"mc_obfuscated;\",\n reset: \"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;\",\n};\n\n// text to json extra name\nconst textToJsonExtras: extraLibraryType = {\n \"§k\": \"obfuscated\",\n \"§l\": \"bold\",\n \"§m\": \"strikethrough\",\n \"§n\": \"underlined\",\n \"§o\": \"italic\",\n \"§r\": \"reset\",\n};\n\n// base color hex\nconst colorCodeToHex: extraLibraryType = {\n \"§0\": \"#000000\",\n \"§1\": \"#0000AA\",\n \"§2\": \"#00AA00\",\n \"§3\": \"#00AAAA\",\n \"§4\": \"#AA0000\",\n \"§5\": \"#AA00AA\",\n \"§6\": \"#FFAA00\",\n \"§7\": \"#AAAAAA\",\n \"§8\": \"#555555\",\n \"§9\": \"#5555FF\",\n \"§a\": \"#55FF55\",\n \"§b\": \"#55FFFF\",\n \"§c\": \"#FF5555\",\n \"§d\": \"#FF55FF\",\n \"§e\": \"#FFFF55\",\n \"§f\": \"#FFFFFF\",\n};\n\n// json extra to hex color\nconst extraColorsToHex: extraLibraryType = {\n black: \"#000000\",\n dark_blue: \"#0000AA\",\n dark_green: \"#00AA00\",\n dark_aqua: \"#00AAAA\",\n dark_red: \"#AA0000\",\n dark_purple: \"#AA00AA\",\n gold: \"#FFAA00\",\n gray: \"#AAAAAA\",\n dark_gray: \"#555555\",\n blue: \"#5555FF\",\n green: \"#55FF55\",\n aqua: \"#55FFFF\",\n red: \"#FF5555\",\n light_purple: \"#FF55FF\",\n yellow: \"#FFFF55\",\n white: \"#FFFFFF\",\n};\n\n\n\nexport {\n extras,\n extraFontStyles,\n textToJsonExtras,\n colorCodeToHex,\n extraColorsToHex,\n};\n","import {\n extras,\n colorCodeToHex,\n} from '../styleLibrary';\nimport {\n htmlStringFormatting,\n} from \"../utils\";\n\n\n\n\n\n\n// text to html\n/**\n * Convert motd text to html.\n * @param motdString\n */\nexport default function textToHTML(motdString: string) {\n const motdText = motdString;\n\n const colorCodeReg = /([§][0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const codeSplit = motdText.split(codeREGEX);\n\n let fontStyle = \"\";\n let colorHex = \"\";\n let resultHTML = \"\";\n\n codeSplit.forEach((item) => {\n const motdStringToLowerCase = item.toLowerCase();\n\n // 過濾 hex\n if (Object.hasOwn(colorCodeToHex, motdStringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[motdStringToLowerCase];\n\n // 過濾文字 style\n } else if (Object.hasOwn(extras, motdStringToLowerCase)) {\n // font style code 轉換\n //console.log(`偵測出 style ${ extras[item] }`)\n fontStyle = extras[motdStringToLowerCase];\n //console.log('textFont: ' + fontStyle)\n // 正常文字\n } else {\n let resultColor = \"\";\n let textContent = item;\n //console.log(fontStyle)\n\n // 檢查 Hex color\n if (colorHex !== \"\") {\n resultColor = `color:${colorHex};`;\n }\n\n if (textContent !== \"\") {\n //console.log('font: ' + fontStyle)\n //console.log('color: ' + colorHex)\n //console.log('text: ' + item)\n //console.log('---------------------------------')\n // replace html tags\n textContent = htmlStringFormatting(textContent);\n\n if (resultColor.length !== 0 || fontStyle.length !== 0) {\n resultHTML += `<span style=\"${fontStyle}${resultColor}\">${textContent}</span>`;\n } else {\n resultHTML += textContent;\n }\n }\n }\n });\n\n return resultHTML;\n}\n","import { motdJsonType } from \"../types\";\nimport {\n extraFontStyles,\n colorCodeToHex,\n extraColorsToHex,\n} from '../styleLibrary';\nimport {\n isMotdJSONType,\n} from \"../utils\";\nimport textToHTML from \"./textToHTML\";\n\n\n\n\n\n\n// json convert to html\n/**\n * Convert JSON to HTML.\n * \n * @param sourceJson \n */\nexport default function parseJSONToHTML(\n sourceJson: motdJsonType,\n) {\n let htmlElement = \"\";\n let colorHex = \"\";\n let fontStyle = \"\";\n\n // console.log('sourceJson', sourceJson);\n // console.log('---------');\n\n for (let key of Object.keys(sourceJson)) {\n // console.log('sourceJson key', key);\n key = key.toLowerCase();\n\n // text styles\n if (Object.hasOwn(extraFontStyles, key)) {\n if (sourceJson[key]) {\n fontStyle += `${extraFontStyles[key]}`;\n }\n }\n\n // ---------- old text process ----------\n // if (key === \"text\") {\n // if (\n // typeof sourceJson.text === \"string\" ||\n // typeof sourceJson.text === \"number\"\n // ) {\n // // convert all type to string\n // htmlElement += textToHTML(String(sourceJson.text));\n // }\n // }\n\n // color\n if (key === \"color\") {\n const colorKey = sourceJson[key];\n\n if (typeof colorKey === \"string\") {\n // Hex color\n if (Object.hasOwn(extraColorsToHex, colorKey)) {\n colorHex = `color:${extraColorsToHex[colorKey]};`;\n continue;\n // color code\n } else if (Object.hasOwn(colorCodeToHex, colorKey)) {\n colorHex = `color:${colorCodeToHex[colorKey]};`;\n continue;\n // custom color\n } else {\n // custom hex color code mode\n colorHex = `color:${colorKey};`;\n continue;\n }\n }\n }\n\n // extra\n if (key === \"extra\" && typeof sourceJson.extra === \"object\") {\n // ---------- with extra text ----------\n if (sourceJson.text !== undefined\n && (typeof sourceJson.text === \"string\" || typeof sourceJson.text === \"number\")\n ) {\n // content to html\n htmlElement += textToHTML(String(sourceJson.text));\n }\n\n // ---------- foreach extra data and parse ----------\n for (const sourceJsonExtra of sourceJson.extra) {\n // console.log('sourceJsonExtra', sourceJsonExtra);\n if (isMotdJSONType(sourceJsonExtra)) {\n htmlElement += parseJSONToHTML(sourceJsonExtra);\n }\n }\n }\n }\n\n // ---------- without extra text content ----------\n if (sourceJson.extra === undefined && sourceJson.text !== undefined) {\n const currentText = sourceJson.text;\n if (\n typeof sourceJson.text === \"string\" ||\n typeof sourceJson.text === \"number\"\n ) {\n // convert all type to string\n htmlElement += textToHTML(String(currentText));\n }\n }\n\n\n\n let returnHTML = \"\";\n if (fontStyle.length !== 0 || colorHex.length !== 0) {\n returnHTML = `<span style=\"${fontStyle + colorHex}\">${htmlElement}</span>`;\n } else {\n returnHTML = htmlElement;\n }\n\n return returnHTML;\n}\n","import { motdJsonType } from \"../types\";\nimport {\n textToJsonExtras,\n colorCodeToHex,\n} from '../styleLibrary';\n\n\n\n\n\n\n// text to json\n/**\n * Convert motd text to JSON.\n * \n * @param text\n */\nexport default function parseTextToJSON(text: string) {\n const motdText = text;\n\n // color code regex: /([§][0-9a-fklmnor])/g\n // color hex regex: /^#(?:[0-9a-f]{3}){1,2}$/g\n const colorCodeReg = /([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const textSplit = motdText.split(codeREGEX);\n let fontStyle = \"\";\n let colorHex = \"\";\n\n const resultObject: motdJsonType = {\n text: \"\",\n extra: [],\n };\n\n // console.log('textSplit', textSplit);\n textSplit.forEach((item) => {\n const stringToLowerCase = item.toLowerCase();\n\n // color code 轉換成 hex\n if (Object.hasOwn(colorCodeToHex, stringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[stringToLowerCase];\n } else if (Object.hasOwn(textToJsonExtras, stringToLowerCase)) {\n // font style code 轉換\n //console.log(`偵測出 style ${ textToJsonExtras[item] }`)\n fontStyle = textToJsonExtras[stringToLowerCase];\n } else {\n const innerObject: motdJsonType = {\n text: \"\",\n extra: [],\n };\n\n // 其餘字串\n if (fontStyle !== \"\") {\n innerObject[fontStyle] = true;\n }\n\n innerObject.text = item;\n\n if (colorHex !== \"\") {\n innerObject.color = colorHex;\n }\n\n if (typeof resultObject.extra === \"object\") {\n resultObject.extra.push(innerObject);\n }\n }\n });\n\n // code styles merge\n let newExtra: Array<motdJsonType> = [];\n // console.log('resultObject', resultObject);\n if (resultObject.extra) {\n if (resultObject.extra.length > 1) {\n // if text is '', remove it and merge to next array\n resultObject.extra.forEach((item, index) => {\n // console.log('item', item);\n if (item.text === \"\") {\n if (\n resultObject.extra\n && typeof resultObject.extra[index + 1] === \"object\"\n ) {\n newExtra.push({\n ...(item as motdJsonType),\n ...resultObject.extra[index + 1],\n });\n }\n } else {\n if (\n item.text !== (newExtra[newExtra.length - 1]\n && newExtra[newExtra.length - 1].text)\n ) {\n newExtra.push(item as motdJsonType);\n }\n }\n });\n } else {\n newExtra.push(resultObject.extra[0] as motdJsonType);\n }\n }\n\n // console.log('newExtra', newExtra);\n // remove blank content\n newExtra = newExtra.filter((item) => item.text !== \"\");\n // console.log('newExtra', newExtra);\n\n return {\n text: resultObject.text,\n extra: newExtra,\n };\n}\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { motdJsonType } from \"./types\";\nimport {\n htmlStringFormatting,\n cleanTags,\n cleanHtmlTags,\n} from \"./utils\";\nimport {\n JSONToHTML,\n textToHTML,\n textToJSON,\n} from \"./parser\";\n\n\n\n\n\n// JSON full convert include newline\nfunction jsonRender(json: motdJsonType | object) {\n return JSONToHTML(JSON.parse(JSON.stringify(json)));\n}\n\n\n\n/**\n * ### `autoToHTML(object | string)`\n * auto check data type then convert to html.\n */\nfunction autoToHTML(motd: motdJsonType | string | object): string {\n // type check\n if (typeof motd === \"object\") {\n // console.log('process mode: Object mode');\n return jsonRender(motd);\n } else if (typeof motd === \"string\") {\n // console.log('process mode: String mode');\n return textToHTML(motd);\n } else {\n return \"unknown motd data type\";\n }\n}\n\n\n\n/*\n * #### minecraft motd parser\n * * [github](https://github.com/SnowFireWolf/minecraft-motd-parser/tree/main#minecraft-server-motd-parser)\n * * [npm](https://www.npmjs.com/package/@sfirew/minecraft-motd-parser)\n *\n * (c) 2023 Kevin Zheng\n *\n * Released under the MIT license\n */\nexport default {\n // text convert to HTML\n textToHTML,\n // text convert to JSON\n textToJSON,\n // JSON convert to HTML\n JSONToHTML,\n // JSON full convert HTML (include enter)\n jsonRender,\n // auto check type to convert\n autoToHTML,\n\n // utils\n htmlStringFormatting,\n // clean all motd tags\n cleanTags,\n // clean all html tags\n cleanHtmlTags,\n};\n"],"mappings":"4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GCYO,SAASI,EAAeC,EAAwC,CACrE,MAAO,CAAC,CAACA,CACX,CAQO,SAASC,EAAqBC,EAAsB,CACzD,OACEA,EAIG,QAAQ,KAAM,OAAO,EACrB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,QAAQ,EACtB,QAAQ,KAAM,OAAO,EAErB,QAAQ,MAAO,OAAO,CAG7B,CAYO,SAASC,EAAcD,EAAsB,CAClD,OAAOA,EAAK,QAAQ,aAAc,EAAE,CACtC,CAWO,SAASE,EAAUF,EAAc,CACtC,IAAMG,EAAQ,mCACVC,EAAa,GAEjB,OAAAA,EAAaJ,EAAK,QAAQG,EAAO,EAAE,EAE5BC,CACT,CChEA,IAAMC,EAA2B,CAC/B,QAAM,cACN,QAAM,qBACN,QAAM,iCACN,QAAM,8BACN,QAAM,sBACN,QAAM,4GACR,EAGMC,EAAoC,CACxC,KAAM,qBACN,OAAQ,sBACR,WAAY,6BACZ,cAAe,iCACf,WAAY,iBACZ,MAAO,4GACT,EAGMC,EAAqC,CACzC,QAAM,aACN,QAAM,OACN,QAAM,gBACN,QAAM,aACN,QAAM,SACN,QAAM,OACR,EAGMC,EAAmC,CACvC,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,SACR,EAGMC,EAAqC,CACzC,MAAO,UACP,UAAW,UACX,WAAY,UACZ,UAAW,UACX,SAAU,UACV,YAAa,UACb,KAAM,UACN,KAAM,UACN,UAAW,UACX,KAAM,UACN,MAAO,UACP,KAAM,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,IAAU,CAEtCN,EAAK,OAAS,GAEdD,EAAa,OACV,OAAOA,EAAa,MAAMO,EAAQ,IAAO,UAE5CD,EAAS,KAAK,CACZ,GAAIL,EACJ,GAAGD,EAAa,MAAMO,EAAQ,EAChC,CAAC,EAIDN,EAAK,QAAUK,EAASA,EAAS,OAAS,IACrCA,EAASA,EAAS,OAAS,GAAG,OAEnCA,EAAS,KAAKL,CAAoB,CAGxC,CAAC,EAEDK,EAAS,KAAKN,EAAa,MAAM,EAAkB,GAMvDM,EAAWA,EAAS,OAAQL,GAASA,EAAK,OAAS,EAAE,EAG9C,CACL,KAAMD,EAAa,KACnB,MAAOM,CACT,CACF,CCtFA,SAASE,EAAWC,EAA6B,CAC/C,OAAOC,EAAW,KAAK,MAAM,KAAK,UAAUD,CAAI,CAAC,CAAC,CACpD,CAQA,SAASE,EAAWC,EAA8C,CAEhE,OAAI,OAAOA,GAAS,SAEXJ,EAAWI,CAAI,EACb,OAAOA,GAAS,SAElBC,EAAWD,CAAI,EAEf,wBAEX,CAaA,IAAOE,EAAQ,CAEb,WAAAD,EAEA,WAAAE,EAEA,WAAAL,EAEA,WAAAF,EAEA,WAAAG,EAGA,qBAAAK,EAEA,UAAAC,EAEA,cAAAC,CACF,ENzEA,IAAOC,EAAQC","names":["src_exports","__export","src_default","__toCommonJS","isMotdJSONType","object","htmlStringFormatting","text","cleanHtmlTags","cleanTags","REGEX","textResult","extras","extraFontStyles","textToJsonExtras","colorCodeToHex","extraColorsToHex","textToHTML","motdString","motdText","colorCodeReg","codeREGEX","codeSplit","fontStyle","colorHex","resultHTML","item","motdStringToLowerCase","colorCodeToHex","extras","resultColor","textContent","htmlStringFormatting","parseJSONToHTML","sourceJson","htmlElement","colorHex","fontStyle","key","extraFontStyles","colorKey","extraColorsToHex","colorCodeToHex","textToHTML","sourceJsonExtra","isMotdJSONType","currentText","returnHTML","parseTextToJSON","text","motdText","colorCodeReg","codeREGEX","textSplit","fontStyle","colorHex","resultObject","item","stringToLowerCase","colorCodeToHex","textToJsonExtras","innerObject","newExtra","index","jsonRender","json","parseJSONToHTML","autoToHTML","motd","textToHTML","motdParser_default","parseTextToJSON","htmlStringFormatting","cleanTags","cleanHtmlTags","src_default","motdParser_default"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function
|
|
1
|
+
function h(t){return!!t}function g(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/\n/g,"<br/>")}function b(t){return t.replace(/<[^>]*>?/gm,"")}function O(t){let l=/(?:§)([0-9a-fA-FklmnorFKLMNOR])/g,i="";return i=t.replace(l,""),i}var T={"\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;"},y={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;"},u={"\xA7k":"obfuscated","\xA7l":"bold","\xA7m":"strikethrough","\xA7n":"underlined","\xA7o":"italic","\xA7r":"reset"},p={"\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"},A={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 c(t){let l=t,i=/([§][0-9a-fA-FklmnorFKLMNOR])/g,x=new RegExp(i.source),m=l.split(x),e="",o="",r="";return m.forEach(a=>{let n=a.toLowerCase();if(Object.hasOwn(p,n))o=p[n];else if(Object.hasOwn(T,n))e=T[n];else{let s="",f=a;o!==""&&(s=`color:${o};`),f!==""&&(f=g(f),s.length!==0||e.length!==0?r+=`<span style="${e}${s}">${f}</span>`:r+=f)}}),r}function d(t){let l="",i="",x="";for(let e of Object.keys(t)){if(e=e.toLowerCase(),Object.hasOwn(y,e)&&t[e]&&(x+=`${y[e]}`),e==="color"){let o=t[e];if(typeof o=="string")if(Object.hasOwn(A,o)){i=`color:${A[o]};`;continue}else if(Object.hasOwn(p,o)){i=`color:${p[o]};`;continue}else{i=`color:${o};`;continue}}if(e==="extra"&&typeof t.extra=="object"){t.text!==void 0&&(typeof t.text=="string"||typeof t.text=="number")&&(l+=c(String(t.text)));for(let o of t.extra)h(o)&&(l+=d(o))}}if(t.extra===void 0&&t.text!==void 0){let e=t.text;(typeof t.text=="string"||typeof t.text=="number")&&(l+=c(String(e)))}let m="";return x.length!==0||i.length!==0?m=`<span style="${x+i}">${l}</span>`:m=l,m}function F(t){let l=t,i=/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g,x=new RegExp(i.source),m=l.split(x),e="",o="",r={text:"",extra:[]};m.forEach(n=>{let s=n.toLowerCase();if(Object.hasOwn(p,s))o=p[s];else if(Object.hasOwn(u,s))e=u[s];else{let f={text:"",extra:[]};e!==""&&(f[e]=!0),f.text=n,o!==""&&(f.color=o),typeof r.extra=="object"&&r.extra.push(f)}});let a=[];return r.extra&&(r.extra.length>1?r.extra.forEach((n,s)=>{n.text===""?r.extra&&typeof r.extra[s+1]=="object"&&a.push({...n,...r.extra[s+1]}):n.text!==(a[a.length-1]&&a[a.length-1].text)&&a.push(n)}):a.push(r.extra[0])),a=a.filter(n=>n.text!==""),{text:r.text,extra:a}}function L(t){return d(JSON.parse(JSON.stringify(t)))}function w(t){return typeof t=="object"?L(t):typeof t=="string"?c(t):"unknown motd data type"}var S={textToHTML:c,textToJSON:F,JSONToHTML:d,jsonRender:L,autoToHTML:w,htmlStringFormatting:g,cleanTags:O,cleanHtmlTags:b};var D=S;export{D as default};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts","../src/styleLibrary.ts","../src/parser/textToHtml.ts","../src/parser/jsonToHtml.ts","../src/parser/textToJSON.ts","../src/motdParser.ts","../src/index.ts"],"sourcesContent":["/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { motdJsonType } from \"./types\";\n\n\n\n\n// 類型檢查\nexport function isMotdJSONType(object: object): object is motdJsonType {\n return !!object;\n};\n\n\n\n/**\n *\n * replace all html tags to &...\n */\nexport function htmlStringFormatting(text: string): string {\n return (\n text\n // space\n // .replace(/ /g, \"\\u00a0\")\n //\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\")\n // return\n .replace(/\\n/g, \"<br/>\")\n );\n // .replace(/\\//g, '/');\n};\n\n\n\n/**\n * clean html tags\n * \n * @param text\n * example: `<span>hello world</span>`\n * \n * result: `hello world`\n */\nexport function cleanHtmlTags(text: string): string {\n return text.replace(/<[^>]*>?/gm, '');\n};\n\n\n\n/**\n * clean motd tags\n * \n * Clean all tags from motd source string.\n * \n * @param {string} text - motd string include tag § will remove\n */\nexport function cleanTags(text: string) {\n const REGEX = /(?:§)([0-9a-fA-FklmnorFKLMNOR])/g;\n let textResult = \"\";\n\n textResult = text.replace(REGEX, \"\");\n\n return textResult;\n};\n","import { extraLibraryType } from \"./types\";\n\n\n\n// color code to font styles\nconst extras: extraLibraryType = {\n \"§k\": \"obfuscated;\",\n \"§l\": \"font-weight: bold;\",\n \"§m\": \"text-decoration: line-through;\",\n \"§n\": \"text-decoration: underline;\",\n \"§o\": \"font-style: italic;\",\n \"§r\": \"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;\",\n};\n\n// json extra font styles\nconst extraFontStyles: extraLibraryType = {\n bold: \"font-weight: bold;\",\n italic: \"font-style: italic;\",\n underlined: \"text-decoration:underline;\",\n strikethrough: \"text-decoration: line-through;\",\n obfuscated: \"mc_obfuscated;\",\n reset: \"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;\",\n};\n\n// text to json extra name\nconst textToJsonExtras: extraLibraryType = {\n \"§k\": \"obfuscated\",\n \"§l\": \"bold\",\n \"§m\": \"strikethrough\",\n \"§n\": \"underlined\",\n \"§o\": \"italic\",\n \"§r\": \"reset\",\n};\n\n// base color hex\nconst colorCodeToHex: extraLibraryType = {\n \"§0\": \"#000000\",\n \"§1\": \"#0000AA\",\n \"§2\": \"#00AA00\",\n \"§3\": \"#00AAAA\",\n \"§4\": \"#AA0000\",\n \"§5\": \"#AA00AA\",\n \"§6\": \"#FFAA00\",\n \"§7\": \"#AAAAAA\",\n \"§8\": \"#555555\",\n \"§9\": \"#5555FF\",\n \"§a\": \"#55FF55\",\n \"§b\": \"#55FFFF\",\n \"§c\": \"#FF5555\",\n \"§d\": \"#FF55FF\",\n \"§e\": \"#FFFF55\",\n \"§f\": \"#FFFFFF\",\n};\n\n// json extra to hex color\nconst extraColorsToHex: extraLibraryType = {\n black: \"#000000\",\n dark_blue: \"#0000AA\",\n dark_green: \"#00AA00\",\n dark_aqua: \"#00AAAA\",\n dark_red: \"#AA0000\",\n dark_purple: \"#AA00AA\",\n gold: \"#FFAA00\",\n gray: \"#AAAAAA\",\n dark_gray: \"#555555\",\n blue: \"#5555FF\",\n green: \"#55FF55\",\n aqua: \"#55FFFF\",\n red: \"#FF5555\",\n light_purple: \"#FF55FF\",\n yellow: \"#FFFF55\",\n white: \"#FFFFFF\",\n};\n\n\n\nexport {\n extras,\n extraFontStyles,\n textToJsonExtras,\n colorCodeToHex,\n extraColorsToHex,\n};\n","import {\n extras,\n colorCodeToHex,\n} from '../styleLibrary';\nimport {\n htmlStringFormatting,\n} from \"../utils\";\n\n\n\n\n\n\n// text to html\n/**\n * Convert motd text to html.\n * @param motdString\n */\nexport default function textToHTML(motdString: string) {\n const motdText = motdString;\n\n const colorCodeReg = /([§][0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const codeSplit = motdText.split(codeREGEX);\n\n let fontStyle = \"\";\n let colorHex = \"\";\n let resultHTML = \"\";\n\n codeSplit.forEach((item) => {\n const motdStringToLowerCase = item.toLowerCase();\n\n // 過濾 hex\n if (Object.hasOwn(colorCodeToHex, motdStringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[motdStringToLowerCase];\n\n // 過濾文字 style\n } else if (Object.hasOwn(extras, motdStringToLowerCase)) {\n // font style code 轉換\n //console.log(`偵測出 style ${ extras[item] }`)\n fontStyle = extras[motdStringToLowerCase];\n //console.log('textFont: ' + fontStyle)\n // 正常文字\n } else {\n let resultColor = \"\";\n let textContent = item;\n //console.log(fontStyle)\n\n // 檢查 Hex color\n if (colorHex !== \"\") {\n resultColor = `color:${colorHex};`;\n }\n\n if (textContent !== \"\") {\n //console.log('font: ' + fontStyle)\n //console.log('color: ' + colorHex)\n //console.log('text: ' + item)\n //console.log('---------------------------------')\n // replace html tags\n textContent = htmlStringFormatting(textContent);\n\n if (resultColor.length !== 0 || fontStyle.length !== 0) {\n resultHTML += `<span style=\"${fontStyle}${resultColor}\">${textContent}</span>`;\n } else {\n resultHTML += textContent;\n }\n }\n }\n });\n\n return resultHTML;\n}\n","import { motdJsonType } from \"../types\";\nimport {\n extraFontStyles,\n colorCodeToHex,\n extraColorsToHex,\n} from '../styleLibrary';\nimport {\n isMotdJSONType,\n} from \"../utils\";\nimport textToHTML from \"./textToHtml\";\n\n\n\n\n\n\n// json convert to html\n/**\n * Convert JSON to HTML.\n * \n * @param sourceJson \n */\nexport default function parseJSONToHTML(\n sourceJson: motdJsonType,\n) {\n let htmlElement = \"\";\n let colorHex = \"\";\n let fontStyle = \"\";\n\n // console.log('sourceJson', sourceJson);\n // console.log('---------');\n\n for (let key of Object.keys(sourceJson)) {\n // console.log('sourceJson key', key);\n key = key.toLowerCase();\n\n // text styles\n if (Object.hasOwn(extraFontStyles, key)) {\n if (sourceJson[key]) {\n fontStyle += `${extraFontStyles[key]}`;\n }\n }\n\n // ---------- old text process ----------\n // if (key === \"text\") {\n // if (\n // typeof sourceJson.text === \"string\" ||\n // typeof sourceJson.text === \"number\"\n // ) {\n // // convert all type to string\n // htmlElement += textToHTML(String(sourceJson.text));\n // }\n // }\n\n // color\n if (key === \"color\") {\n const colorKey = sourceJson[key];\n\n if (typeof colorKey === \"string\") {\n // Hex color\n if (Object.hasOwn(extraColorsToHex, colorKey)) {\n colorHex = `color:${extraColorsToHex[colorKey]};`;\n continue;\n // color code\n } else if (Object.hasOwn(colorCodeToHex, colorKey)) {\n colorHex = `color:${colorCodeToHex[colorKey]};`;\n continue;\n // custom color\n } else {\n // custom hex color code mode\n colorHex = `color:${colorKey};`;\n continue;\n }\n }\n }\n\n // extra\n if (key === \"extra\" && typeof sourceJson.extra === \"object\") {\n // ---------- with extra text ----------\n if (sourceJson.text !== undefined\n && (typeof sourceJson.text === \"string\" || typeof sourceJson.text === \"number\")\n ) {\n // content to html\n htmlElement += textToHTML(String(sourceJson.text));\n }\n\n // ---------- foreach extra data and parse ----------\n for (const sourceJsonExtra of sourceJson.extra) {\n // console.log('sourceJsonExtra', sourceJsonExtra);\n if (isMotdJSONType(sourceJsonExtra)) {\n htmlElement += parseJSONToHTML(sourceJsonExtra);\n }\n }\n }\n }\n\n // ---------- without extra text content ----------\n if (sourceJson.extra === undefined && sourceJson.text !== undefined) {\n const currentText = sourceJson.text;\n if (\n typeof sourceJson.text === \"string\" ||\n typeof sourceJson.text === \"number\"\n ) {\n // convert all type to string\n htmlElement += textToHTML(String(currentText));\n }\n }\n\n\n\n let returnHTML = \"\";\n if (fontStyle.length !== 0 || colorHex.length !== 0) {\n returnHTML = `<span style=\"${fontStyle + colorHex}\">${htmlElement}</span>`;\n } else {\n returnHTML = htmlElement;\n }\n\n return returnHTML;\n}\n","import { motdJsonType } from \"../types\";\nimport {\n textToJsonExtras,\n colorCodeToHex,\n} from '../styleLibrary';\n\n\n\n\n\n\n// text to json\n/**\n * Convert motd text to JSON.\n * \n * @param text\n */\nexport default function parseTextToJSON(text: string) {\n const motdText = text;\n\n // color code regex: /([§][0-9a-fklmnor])/g\n // color hex regex: /^#(?:[0-9a-f]{3}){1,2}$/g\n const colorCodeReg = /([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const textSplit = motdText.split(codeREGEX);\n let fontStyle = \"\";\n let colorHex = \"\";\n\n const resultObject: motdJsonType = {\n text: \"\",\n extra: [],\n };\n\n // console.log('textSplit', textSplit);\n textSplit.forEach((item) => {\n const stringToLowerCase = item.toLowerCase();\n\n // color code 轉換成 hex\n if (Object.hasOwn(colorCodeToHex, stringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[stringToLowerCase];\n } else if (Object.hasOwn(textToJsonExtras, stringToLowerCase)) {\n // font style code 轉換\n //console.log(`偵測出 style ${ textToJsonExtras[item] }`)\n fontStyle = textToJsonExtras[stringToLowerCase];\n } else {\n const innerObject: motdJsonType = {\n text: \"\",\n extra: [],\n };\n\n // 其餘字串\n if (fontStyle !== \"\") {\n innerObject[fontStyle] = true;\n }\n\n innerObject.text = item;\n\n if (colorHex !== \"\") {\n innerObject.color = colorHex;\n }\n\n if (typeof resultObject.extra === \"object\") {\n resultObject.extra.push(innerObject);\n }\n }\n });\n\n // code styles merge\n let newExtra: Array<motdJsonType> = [];\n // console.log('resultObject', resultObject);\n if (resultObject.extra) {\n if (resultObject.extra.length > 1) {\n // if text is '', remove it and merge to next array\n resultObject.extra.forEach((item, index) => {\n // console.log('item', item);\n if (item.text === \"\") {\n if (\n resultObject.extra\n && typeof resultObject.extra[index + 1] === \"object\"\n ) {\n newExtra.push({\n ...(item as motdJsonType),\n ...resultObject.extra[index + 1],\n });\n }\n } else {\n if (\n item.text !== (newExtra[newExtra.length - 1]\n && newExtra[newExtra.length - 1].text)\n ) {\n newExtra.push(item as motdJsonType);\n }\n }\n });\n } else {\n newExtra.push(resultObject.extra[0] as motdJsonType);\n }\n }\n\n // console.log('newExtra', newExtra);\n // remove blank content\n newExtra = newExtra.filter((item) => item.text !== \"\");\n // console.log('newExtra', newExtra);\n\n return {\n text: resultObject.text,\n extra: newExtra,\n };\n}\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { motdJsonType } from \"./types\";\nimport {\n htmlStringFormatting,\n cleanTags,\n cleanHtmlTags,\n} from \"./utils\";\nimport {\n jsonToHtml,\n textToHtml,\n textToJSON,\n} from \"./parser\";\n\n\n\n\n\n// JSON full convert include newline\nfunction jsonRender(json: motdJsonType | object) {\n return jsonToHtml(JSON.parse(JSON.stringify(json)));\n}\n\n\n\n/**\n * ### `autoToHTML(object | string)`\n * auto check data type then convert to html.\n */\nfunction autoToHTML(motd: motdJsonType | string | object): string {\n // type check\n if (typeof motd === \"object\") {\n // console.log('process mode: Object mode');\n return jsonRender(motd);\n } else if (typeof motd === \"string\") {\n // console.log('process mode: String mode');\n return textToHtml(motd);\n } else {\n return \"unknown motd data type\";\n }\n}\n\n\n\n/*\n * #### minecraft motd parser\n * * [github](https://github.com/SnowFireWolf/minecraft-motd-parser/tree/main#minecraft-server-motd-parser)\n * * [npm](https://www.npmjs.com/package/@sfirew/minecraft-motd-parser)\n *\n * (c) 2023 Kevin Zheng\n *\n * Released under the MIT license\n */\nconst defaultModules = {\n // text convert to HTML\n textToHTML: textToHtml,\n // text convert to JSON\n textToJSON: textToJSON,\n // JSON convert to HTML\n JSONToHTML: jsonToHtml,\n // JSON full convert HTML (include enter)\n jsonRender,\n // auto check type to convert\n autoToHTML,\n\n // utils\n htmlStringFormatting,\n // clean all motd tags\n cleanTags,\n // clean all html tags\n cleanHtmlTags,\n};\n\nexport default defaultModules;\n","import motdParser from \"./motdParser\";\n\nexport { motdParser };\nexport const cleanTags = motdParser.cleanTags;\nexport const textToHTML = motdParser.textToHTML;\nexport const textToJSON = motdParser.textToJSON;\nexport const JSONToHTML = motdParser.JSONToHTML;\nexport const jsonRender = motdParser.jsonRender;\nexport const autoToHTML = motdParser.autoToHTML;\n\nexport default motdParser;\n"],"mappings":"AAYO,SAASA,EAAeC,EAAwC,CACrE,MAAO,CAAC,CAACA,CACX,CAQO,SAASC,EAAqBC,EAAsB,CACzD,OACEA,EAIG,QAAQ,KAAM,OAAO,EACrB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,QAAQ,EACtB,QAAQ,KAAM,OAAO,EAErB,QAAQ,MAAO,OAAO,CAG7B,CAYO,SAASC,EAAcD,EAAsB,CAClD,OAAOA,EAAK,QAAQ,aAAc,EAAE,CACtC,CAWO,SAASE,EAAUF,EAAc,CACtC,IAAMG,EAAQ,mCACVC,EAAa,GAEjB,OAAAA,EAAaJ,EAAK,QAAQG,EAAO,EAAE,EAE5BC,CACT,CChEA,IAAMC,EAA2B,CAC/B,QAAM,cACN,QAAM,qBACN,QAAM,iCACN,QAAM,8BACN,QAAM,sBACN,QAAM,4GACR,EAGMC,EAAoC,CACxC,KAAM,qBACN,OAAQ,sBACR,WAAY,6BACZ,cAAe,iCACf,WAAY,iBACZ,MAAO,4GACT,EAGMC,EAAqC,CACzC,QAAM,aACN,QAAM,OACN,QAAM,gBACN,QAAM,aACN,QAAM,SACN,QAAM,OACR,EAGMC,EAAmC,CACvC,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UACN,QAAM,UAC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leanHtmlTags","cleanTags","REGEX","textResult","extras","extraFontStyles","textToJsonExtras","colorCodeToHex","extraColorsToHex","textToHTML","motdString","motdText","colorCodeReg","codeREGEX","codeSplit","fontStyle","colorHex","resultHTML","item","motdStringToLowerCase","colorCodeToHex","extras","resultColor","textContent","htmlStringFormatting","parseJSONToHTML","sourceJson","htmlElement","colorHex","fontStyle","key","extraFontStyles","colorKey","extraColorsToHex","colorCodeToHex","textToHTML","sourceJsonExtra","isMotdJSONType","currentText","returnHTML","parseTextToJSON","text","motdText","colorCodeReg","codeREGEX","textSplit","fontStyle","colorHex","resultObject","item","stringToLowerCase","colorCodeToHex","textToJsonExtras","innerObject","newExtra","index","jsonRender","json","parseJSONToHTML","autoToHTML","motd","textToHTML","defaultModules","parseTextToJSON","htmlStringFormatting","cleanTags","cleanHtmlTags","motdParser_default","cleanTags","motdParser_default","textToHTML","textToJSON","JSONToHTML","jsonRender","autoToHTML","src_default"]}
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/styleLibrary.ts","../src/parser/textToHTML.ts","../src/parser/JSONToHTML.ts","../src/parser/textToJSON.ts","../src/motdParser.ts","../src/index.ts"],"sourcesContent":["/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { motdJsonType } from \"./types\";\n\n\n\n\n// 類型檢查\nexport function isMotdJSONType(object: object): object is motdJsonType {\n return !!object;\n};\n\n\n\n/**\n *\n * replace all html tags to &...\n */\nexport function htmlStringFormatting(text: string): string {\n return (\n text\n // space\n // .replace(/ /g, \"\\u00a0\")\n //\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\")\n // return\n .replace(/\\n/g, \"<br/>\")\n );\n // .replace(/\\//g, '/');\n};\n\n\n\n/**\n * clean html tags\n * \n * @param text\n * example: `<span>hello world</span>`\n * \n * result: `hello world`\n */\nexport function cleanHtmlTags(text: string): string {\n return text.replace(/<[^>]*>?/gm, '');\n};\n\n\n\n/**\n * clean motd tags\n * \n * Clean all tags from motd source string.\n * \n * @param {string} text - motd string include tag § will remove\n */\nexport function cleanTags(text: string) {\n const REGEX = /(?:§)([0-9a-fA-FklmnorFKLMNOR])/g;\n let textResult = \"\";\n\n textResult = text.replace(REGEX, \"\");\n\n return textResult;\n};\n","import { extraLibraryType } from \"./types\";\n\n\n\n// color code to font styles\nconst extras: extraLibraryType = {\n \"§k\": \"obfuscated;\",\n \"§l\": \"font-weight: bold;\",\n \"§m\": \"text-decoration: line-through;\",\n \"§n\": \"text-decoration: underline;\",\n \"§o\": \"font-style: italic;\",\n \"§r\": \"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;\",\n};\n\n// json extra font styles\nconst extraFontStyles: extraLibraryType = {\n bold: \"font-weight: bold;\",\n italic: \"font-style: italic;\",\n underlined: \"text-decoration:underline;\",\n strikethrough: \"text-decoration: line-through;\",\n obfuscated: \"mc_obfuscated;\",\n reset: \"color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;\",\n};\n\n// text to json extra name\nconst textToJsonExtras: extraLibraryType = {\n \"§k\": \"obfuscated\",\n \"§l\": \"bold\",\n \"§m\": \"strikethrough\",\n \"§n\": \"underlined\",\n \"§o\": \"italic\",\n \"§r\": \"reset\",\n};\n\n// base color hex\nconst colorCodeToHex: extraLibraryType = {\n \"§0\": \"#000000\",\n \"§1\": \"#0000AA\",\n \"§2\": \"#00AA00\",\n \"§3\": \"#00AAAA\",\n \"§4\": \"#AA0000\",\n \"§5\": \"#AA00AA\",\n \"§6\": \"#FFAA00\",\n \"§7\": \"#AAAAAA\",\n \"§8\": \"#555555\",\n \"§9\": \"#5555FF\",\n \"§a\": \"#55FF55\",\n \"§b\": \"#55FFFF\",\n \"§c\": \"#FF5555\",\n \"§d\": \"#FF55FF\",\n \"§e\": \"#FFFF55\",\n \"§f\": \"#FFFFFF\",\n};\n\n// json extra to hex color\nconst extraColorsToHex: extraLibraryType = {\n black: \"#000000\",\n dark_blue: \"#0000AA\",\n dark_green: \"#00AA00\",\n dark_aqua: \"#00AAAA\",\n dark_red: \"#AA0000\",\n dark_purple: \"#AA00AA\",\n gold: \"#FFAA00\",\n gray: \"#AAAAAA\",\n dark_gray: \"#555555\",\n blue: \"#5555FF\",\n green: \"#55FF55\",\n aqua: \"#55FFFF\",\n red: \"#FF5555\",\n light_purple: \"#FF55FF\",\n yellow: \"#FFFF55\",\n white: \"#FFFFFF\",\n};\n\n\n\nexport {\n extras,\n extraFontStyles,\n textToJsonExtras,\n colorCodeToHex,\n extraColorsToHex,\n};\n","import {\n extras,\n colorCodeToHex,\n} from '../styleLibrary';\nimport {\n htmlStringFormatting,\n} from \"../utils\";\n\n\n\n\n\n\n// text to html\n/**\n * Convert motd text to html.\n * @param motdString\n */\nexport default function textToHTML(motdString: string) {\n const motdText = motdString;\n\n const colorCodeReg = /([§][0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const codeSplit = motdText.split(codeREGEX);\n\n let fontStyle = \"\";\n let colorHex = \"\";\n let resultHTML = \"\";\n\n codeSplit.forEach((item) => {\n const motdStringToLowerCase = item.toLowerCase();\n\n // 過濾 hex\n if (Object.hasOwn(colorCodeToHex, motdStringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[motdStringToLowerCase];\n\n // 過濾文字 style\n } else if (Object.hasOwn(extras, motdStringToLowerCase)) {\n // font style code 轉換\n //console.log(`偵測出 style ${ extras[item] }`)\n fontStyle = extras[motdStringToLowerCase];\n //console.log('textFont: ' + fontStyle)\n // 正常文字\n } else {\n let resultColor = \"\";\n let textContent = item;\n //console.log(fontStyle)\n\n // 檢查 Hex color\n if (colorHex !== \"\") {\n resultColor = `color:${colorHex};`;\n }\n\n if (textContent !== \"\") {\n //console.log('font: ' + fontStyle)\n //console.log('color: ' + colorHex)\n //console.log('text: ' + item)\n //console.log('---------------------------------')\n // replace html tags\n textContent = htmlStringFormatting(textContent);\n\n if (resultColor.length !== 0 || fontStyle.length !== 0) {\n resultHTML += `<span style=\"${fontStyle}${resultColor}\">${textContent}</span>`;\n } else {\n resultHTML += textContent;\n }\n }\n }\n });\n\n return resultHTML;\n}\n","import { motdJsonType } from \"../types\";\nimport {\n extraFontStyles,\n colorCodeToHex,\n extraColorsToHex,\n} from '../styleLibrary';\nimport {\n isMotdJSONType,\n} from \"../utils\";\nimport textToHTML from \"./textToHTML\";\n\n\n\n\n\n\n// json convert to html\n/**\n * Convert JSON to HTML.\n * \n * @param sourceJson \n */\nexport default function parseJSONToHTML(\n sourceJson: motdJsonType,\n) {\n let htmlElement = \"\";\n let colorHex = \"\";\n let fontStyle = \"\";\n\n // console.log('sourceJson', sourceJson);\n // console.log('---------');\n\n for (let key of Object.keys(sourceJson)) {\n // console.log('sourceJson key', key);\n key = key.toLowerCase();\n\n // text styles\n if (Object.hasOwn(extraFontStyles, key)) {\n if (sourceJson[key]) {\n fontStyle += `${extraFontStyles[key]}`;\n }\n }\n\n // ---------- old text process ----------\n // if (key === \"text\") {\n // if (\n // typeof sourceJson.text === \"string\" ||\n // typeof sourceJson.text === \"number\"\n // ) {\n // // convert all type to string\n // htmlElement += textToHTML(String(sourceJson.text));\n // }\n // }\n\n // color\n if (key === \"color\") {\n const colorKey = sourceJson[key];\n\n if (typeof colorKey === \"string\") {\n // Hex color\n if (Object.hasOwn(extraColorsToHex, colorKey)) {\n colorHex = `color:${extraColorsToHex[colorKey]};`;\n continue;\n // color code\n } else if (Object.hasOwn(colorCodeToHex, colorKey)) {\n colorHex = `color:${colorCodeToHex[colorKey]};`;\n continue;\n // custom color\n } else {\n // custom hex color code mode\n colorHex = `color:${colorKey};`;\n continue;\n }\n }\n }\n\n // extra\n if (key === \"extra\" && typeof sourceJson.extra === \"object\") {\n // ---------- with extra text ----------\n if (sourceJson.text !== undefined\n && (typeof sourceJson.text === \"string\" || typeof sourceJson.text === \"number\")\n ) {\n // content to html\n htmlElement += textToHTML(String(sourceJson.text));\n }\n\n // ---------- foreach extra data and parse ----------\n for (const sourceJsonExtra of sourceJson.extra) {\n // console.log('sourceJsonExtra', sourceJsonExtra);\n if (isMotdJSONType(sourceJsonExtra)) {\n htmlElement += parseJSONToHTML(sourceJsonExtra);\n }\n }\n }\n }\n\n // ---------- without extra text content ----------\n if (sourceJson.extra === undefined && sourceJson.text !== undefined) {\n const currentText = sourceJson.text;\n if (\n typeof sourceJson.text === \"string\" ||\n typeof sourceJson.text === \"number\"\n ) {\n // convert all type to string\n htmlElement += textToHTML(String(currentText));\n }\n }\n\n\n\n let returnHTML = \"\";\n if (fontStyle.length !== 0 || colorHex.length !== 0) {\n returnHTML = `<span style=\"${fontStyle + colorHex}\">${htmlElement}</span>`;\n } else {\n returnHTML = htmlElement;\n }\n\n return returnHTML;\n}\n","import { motdJsonType } from \"../types\";\nimport {\n textToJsonExtras,\n colorCodeToHex,\n} from '../styleLibrary';\n\n\n\n\n\n\n// text to json\n/**\n * Convert motd text to JSON.\n * \n * @param text\n */\nexport default function parseTextToJSON(text: string) {\n const motdText = text;\n\n // color code regex: /([§][0-9a-fklmnor])/g\n // color hex regex: /^#(?:[0-9a-f]{3}){1,2}$/g\n const colorCodeReg = /([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const textSplit = motdText.split(codeREGEX);\n let fontStyle = \"\";\n let colorHex = \"\";\n\n const resultObject: motdJsonType = {\n text: \"\",\n extra: [],\n };\n\n // console.log('textSplit', textSplit);\n textSplit.forEach((item) => {\n const stringToLowerCase = item.toLowerCase();\n\n // color code 轉換成 hex\n if (Object.hasOwn(colorCodeToHex, stringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[stringToLowerCase];\n } else if (Object.hasOwn(textToJsonExtras, stringToLowerCase)) {\n // font style code 轉換\n //console.log(`偵測出 style ${ textToJsonExtras[item] }`)\n fontStyle = textToJsonExtras[stringToLowerCase];\n } else {\n const innerObject: motdJsonType = {\n text: \"\",\n extra: [],\n };\n\n // 其餘字串\n if (fontStyle !== \"\") {\n innerObject[fontStyle] = true;\n }\n\n innerObject.text = item;\n\n if (colorHex !== \"\") {\n innerObject.color = colorHex;\n }\n\n if (typeof resultObject.extra === \"object\") {\n resultObject.extra.push(innerObject);\n }\n }\n });\n\n // code styles merge\n let newExtra: Array<motdJsonType> = [];\n // console.log('resultObject', resultObject);\n if (resultObject.extra) {\n if (resultObject.extra.length > 1) {\n // if text is '', remove it and merge to next array\n resultObject.extra.forEach((item, index) => {\n // console.log('item', item);\n if (item.text === \"\") {\n if (\n resultObject.extra\n && typeof resultObject.extra[index + 1] === \"object\"\n ) {\n newExtra.push({\n ...(item as motdJsonType),\n ...resultObject.extra[index + 1],\n });\n }\n } else {\n if (\n item.text !== (newExtra[newExtra.length - 1]\n && newExtra[newExtra.length - 1].text)\n ) {\n newExtra.push(item as motdJsonType);\n }\n }\n });\n } else {\n newExtra.push(resultObject.extra[0] as motdJsonType);\n }\n }\n\n // console.log('newExtra', newExtra);\n // remove blank content\n newExtra = newExtra.filter((item) => item.text !== \"\");\n // console.log('newExtra', newExtra);\n\n return {\n text: resultObject.text,\n extra: newExtra,\n };\n}\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { motdJsonType } from \"./types\";\nimport {\n htmlStringFormatting,\n cleanTags,\n cleanHtmlTags,\n} from \"./utils\";\nimport {\n JSONToHTML,\n textToHTML,\n textToJSON,\n} from \"./parser\";\n\n\n\n\n\n// JSON full convert include newline\nfunction jsonRender(json: motdJsonType | object) {\n return JSONToHTML(JSON.parse(JSON.stringify(json)));\n}\n\n\n\n/**\n * ### `autoToHTML(object | string)`\n * auto check data type then convert to html.\n */\nfunction autoToHTML(motd: motdJsonType | string | object): string {\n // type check\n if (typeof motd === \"object\") {\n // console.log('process mode: Object mode');\n return jsonRender(motd);\n } else if (typeof motd === \"string\") {\n // console.log('process mode: String mode');\n return textToHTML(motd);\n } else {\n return \"unknown motd data type\";\n }\n}\n\n\n\n/*\n * #### minecraft motd parser\n * * [github](https://github.com/SnowFireWolf/minecraft-motd-parser/tree/main#minecraft-server-motd-parser)\n * * [npm](https://www.npmjs.com/package/@sfirew/minecraft-motd-parser)\n *\n * (c) 2023 Kevin Zheng\n *\n * Released under the MIT license\n */\nexport default {\n // text convert to HTML\n textToHTML,\n // text convert to JSON\n textToJSON,\n // JSON convert to HTML\n JSONToHTML,\n // JSON full convert HTML (include enter)\n jsonRender,\n // auto check type to convert\n autoToHTML,\n\n // utils\n htmlStringFormatting,\n // clean all motd tags\n cleanTags,\n // clean all html tags\n cleanHtmlTags,\n};\n","import motdParser from \"./motdParser\";\n\nexport default motdParser;\n"],"mappings":"AAYO,SAASA,EAAeC,EAAwC,CACrE,MAAO,CAAC,CAACA,CACX,CAQO,SAASC,EAAqBC,EAAsB,CACzD,OACEA,EAIG,QAAQ,KAAM,OAAO,EACrB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,QAAQ,EACtB,QAAQ,KAAM,OAAO,EAErB,QAAQ,MAAO,OAAO,CAG7B,CAYO,SAASC,EAAcD,EAAsB,CAClD,OAAOA,EAAK,QAAQ,aAAc,EAAE,CACtC,CAWO,SAASE,EAAUF,EAAc,CACtC,IAAMG,EAAQ,mCACVC,EAAa,GAEjB,OAAAA,EAAaJ,EAAK,QAAQG,EAAO,EAAE,EAE5BC,CACT,CChEA,IAAMC,EAA2B,CAC/B,QAAM,cACN,QAAM,qBACN,QAAM,iCACN,QAAM,8BACN,QAAM,sBACN,QAAM,4GACR,EAGMC,EAAoC,CACxC,KAAM,qBACN,OAAQ,sBACR,WAAY,6BACZ,cAAe,iCACf,WAAY,iBACZ,MAAO,4GACT,EAGMC,EAAqC,CACzC,QAAM,aACN,QAAM,OACN,QAAM,gBACN,QAAM,aACN,QAAM,SACN,QAAM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YM,EAAiBF,OACxB,CACL,IAAMG,EAA4B,CAChC,KAAM,GACN,MAAO,CAAC,CACV,EAGIP,IAAc,KAChBO,EAAYP,GAAa,IAG3BO,EAAY,KAAOJ,EAEfF,IAAa,KACfM,EAAY,MAAQN,GAGlB,OAAOC,EAAa,OAAU,UAChCA,EAAa,MAAM,KAAKK,CAAW,CAEvC,CACF,CAAC,EAGD,IAAIC,EAAgC,CAAC,EAErC,OAAIN,EAAa,QACXA,EAAa,MAAM,OAAS,EAE9BA,EAAa,MAAM,QAAQ,CAACC,EAAMM,IAAU,CAEtCN,EAAK,OAAS,GAEdD,EAAa,OACV,OAAOA,EAAa,MAAMO,EAAQ,IAAO,UAE5CD,EAAS,KAAK,CACZ,GAAIL,EACJ,GAAGD,EAAa,MAAMO,EAAQ,EAChC,CAAC,EAIDN,EAAK,QAAUK,EAASA,EAAS,OAAS,IACrCA,EAASA,EAAS,OAAS,GAAG,OAEnCA,EAAS,KAAKL,CAAoB,CAGxC,CAAC,EAEDK,EAAS,KAAKN,EAAa,MAAM,EAAkB,GAMvDM,EAAWA,EAAS,OAAQL,GAASA,EAAK,OAAS,EAAE,EAG9C,CACL,KAAMD,EAAa,KACnB,MAAOM,CACT,CACF,CCtFA,SAASE,EAAWC,EAA6B,CAC/C,OAAOC,EAAW,KAAK,MAAM,KAAK,UAAUD,CAAI,CAAC,CAAC,CACpD,CAQA,SAASE,EAAWC,EAA8C,CAEhE,OAAI,OAAOA,GAAS,SAEXJ,EAAWI,CAAI,EACb,OAAOA,GAAS,SAElBC,EAAWD,CAAI,EAEf,wBAEX,CAaA,IAAOE,EAAQ,CAEb,WAAAD,EAEA,WAAAE,EAEA,WAAAL,EAEA,WAAAF,EAEA,WAAAG,EAGA,qBAAAK,EAEA,UAAAC,EAEA,cAAAC,CACF,ECzEA,IAAOC,EAAQC","names":["isMotdJSONType","object","htmlStringFormatting","text","cleanHtmlTags","cleanTags","REGEX","textResult","extras","extraFontStyles","textToJsonExtras","colorCodeToHex","extraColorsToHex","textToHTML","motdString","motdText","colorCodeReg","codeREGEX","codeSplit","fontStyle","colorHex","resultHTML","item","motdStringToLowerCase","colorCodeToHex","extras","resultColor","textContent","htmlStringFormatting","parseJSONToHTML","sourceJson","htmlElement","colorHex","fontStyle","key","extraFontStyles","colorKey","extraColorsToHex","colorCodeToHex","textToHTML","sourceJsonExtra","isMotdJSONType","currentText","returnHTML","parseTextToJSON","text","motdText","colorCodeReg","codeREGEX","textSplit","fontStyle","colorHex","resultObject","item","stringToLowerCase","colorCodeToHex","textToJsonExtras","innerObject","newExtra","index","jsonRender","json","parseJSONToHTML","autoToHTML","motd","textToHTML","motdParser_default","parseTextToJSON","htmlStringFormatting","cleanTags","cleanHtmlTags","src_default","motdParser_default"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sfirew/minecraft-motd-parser",
|
|
3
3
|
"description": "Minecraft Server MOTD Parser, can convert to html, json, text.",
|
|
4
|
-
"version": "1.1.2-dev.
|
|
4
|
+
"version": "1.1.2-dev.3",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "types/index.d.ts",
|
package/types/index.d.ts
CHANGED
|
@@ -1,9 +1,2 @@
|
|
|
1
1
|
import motdParser from "./motdParser";
|
|
2
|
-
export { motdParser };
|
|
3
|
-
export declare const cleanTags: typeof import("./utils").cleanTags;
|
|
4
|
-
export declare const textToHTML: typeof import("./parser").textToHtml;
|
|
5
|
-
export declare const textToJSON: typeof import("./parser").textToJSON;
|
|
6
|
-
export declare const JSONToHTML: typeof import("./parser").jsonToHtml;
|
|
7
|
-
export declare const jsonRender: (json: object | import("./types").motdJsonType) => string;
|
|
8
|
-
export declare const autoToHTML: (motd: string | object | import("./types").motdJsonType) => string;
|
|
9
2
|
export default motdParser;
|
package/types/motdParser.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { motdJsonType } from "./types";
|
|
2
2
|
import { htmlStringFormatting, cleanTags, cleanHtmlTags } from "./utils";
|
|
3
|
-
import {
|
|
3
|
+
import { JSONToHTML, textToHTML, textToJSON } from "./parser";
|
|
4
4
|
declare function jsonRender(json: motdJsonType | object): string;
|
|
5
5
|
/**
|
|
6
6
|
* ### `autoToHTML(object | string)`
|
|
7
7
|
* auto check data type then convert to html.
|
|
8
8
|
*/
|
|
9
9
|
declare function autoToHTML(motd: motdJsonType | string | object): string;
|
|
10
|
-
declare const
|
|
11
|
-
textToHTML: typeof
|
|
10
|
+
declare const _default: {
|
|
11
|
+
textToHTML: typeof textToHTML;
|
|
12
12
|
textToJSON: typeof textToJSON;
|
|
13
|
-
JSONToHTML: typeof
|
|
13
|
+
JSONToHTML: typeof JSONToHTML;
|
|
14
14
|
jsonRender: typeof jsonRender;
|
|
15
15
|
autoToHTML: typeof autoToHTML;
|
|
16
16
|
htmlStringFormatting: typeof htmlStringFormatting;
|
|
17
17
|
cleanTags: typeof cleanTags;
|
|
18
18
|
cleanHtmlTags: typeof cleanHtmlTags;
|
|
19
19
|
};
|
|
20
|
-
export default
|
|
20
|
+
export default _default;
|
package/types/parser/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import JSONToHTML from "./JSONToHTML";
|
|
2
|
+
import textToHTML from "./textToHTML";
|
|
3
3
|
import textToJSON from "./textToJSON";
|
|
4
|
-
export {
|
|
4
|
+
export { JSONToHTML, textToHTML, textToJSON, };
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<module type="WEB_MODULE" version="4">
|
|
3
|
-
<component name="NewModuleRootManager">
|
|
4
|
-
<content url="file://$MODULE_DIR$">
|
|
5
|
-
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
|
6
|
-
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
|
7
|
-
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
|
8
|
-
</content>
|
|
9
|
-
<orderEntry type="inheritedJdk" />
|
|
10
|
-
<orderEntry type="sourceFolder" forTests="false" />
|
|
11
|
-
</component>
|
|
12
|
-
</module>
|
package/.idea/modules.xml
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<project version="4">
|
|
3
|
-
<component name="ProjectModuleManager">
|
|
4
|
-
<modules>
|
|
5
|
-
<module fileurl="file://$PROJECT_DIR$/.idea/minecraft-motd-parser.iml" filepath="$PROJECT_DIR$/.idea/minecraft-motd-parser.iml" />
|
|
6
|
-
</modules>
|
|
7
|
-
</component>
|
|
8
|
-
</project>
|