@sfirew/minecraft-motd-parser 1.1.1 → 1.1.2-1

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/README.md CHANGED
@@ -1,15 +1,15 @@
1
1
  # Minecraft Server MOTD Parser
2
2
  ![Version](https://img.shields.io/github/languages/top/SnowFireWolf/minecraft-motd-parser?style=for-the-badge)
3
- [![npm version](https://img.shields.io/npm/v/@sfirew/mc-motd-parser?label=version&style=for-the-badge)](https://www.npmjs.com/package/@sfirew/mc-motd-parser?style=for-the-badge)
3
+ [![npm version](https://img.shields.io/npm/v/@sfirew/minecraft-motd-parser?label=version&style=for-the-badge)](https://www.npmjs.com/package/@sfirew/minecraft-motd-parser?style=for-the-badge)
4
4
  [![License](https://img.shields.io/npm/l/minecraft-server-util?style=for-the-badge)](https://github.com/SnowFireWolf/minecraft-motd-parser/blob/master/LICENSE)
5
- ![npm weekly downloads](https://img.shields.io/npm/dw/@sfirew/mc-motd-parser?style=for-the-badge)
5
+ ![npm weekly downloads](https://img.shields.io/npm/dw/@sfirew/minecraft-motd-parser?style=for-the-badge)
6
6
 
7
7
 
8
8
 
9
9
  ## Introduction
10
10
  This package support **CommonJS**, **ES Module**, and **tree shaking**
11
11
 
12
- Can convert minecraft server MOTD data to text, json, html, and clean tags.
12
+ Can convert minecraft server MOTD data to text, json, html, and clean codes.
13
13
 
14
14
  Support **1.16** custom **hex color**, and auto check motd data type.
15
15
 
@@ -35,20 +35,20 @@ $ pnpm add @sfirew/minecraft-motd-parser
35
35
  ## Usage
36
36
  ### CommonJS
37
37
  ```typescript
38
- const { autoToHtml } = require('@sfirew/minecraft-motd-parser');
38
+ const { autoToHTML } = require('@sfirew/minecraft-motd-parser');
39
39
  ```
40
40
 
41
41
  ### ES6 Modules
42
42
  ```typescript
43
- import { autoToHtml } from '@sfirew/minecraft-motd-parser'
44
- // or
45
- import motdParser from '@sfirew/minecraft-motd-parser'
46
- // motdParser.autoToHtml('motdString');
43
+ import { autoToHTML } from '@sfirew/minecraft-motd-parser'
47
44
  ```
48
45
 
49
46
  ### Simple use example
50
47
  ```typescript
51
- import { autoToHtml as motdParser } from '@sfirew/minecraft-motd-parser'
48
+ import { autoToHTML as motdParser } from '@sfirew/minecraft-motd-parser'
49
+ // or
50
+ import motdParser from '@sfirew/minecraft-motd-parser'
51
+ // motdParser.autoToHTML('motdString...');
52
52
 
53
53
  const hypixelMotdString = "§aHypixel Network §7§c1.8/1.9/1.10/1.11/1.12 §e§lNEW PTL GAME:§b§l THE BRIDGE";
54
54
 
@@ -83,14 +83,14 @@ You can try server status view in my created service
83
83
  Some examples here, you can use **TypeScript** or **JavaScript**.
84
84
 
85
85
 
86
- ### `cleanTags(string)`
87
- clean text color tags.
86
+ ### `cleanCodes(string)`
87
+ clean motd text color codes.
88
88
  ```typescript
89
89
  import motdParser from '@sfirew/minecraft-motd-parser'
90
- import { cleanTags } from '@sfirew/minecraft-motd-parser'
90
+ import { cleanCodes } from '@sfirew/minecraft-motd-parser'
91
91
 
92
92
  const motdText = "§aHypixel Network §7§c1.8/1.9/1.10/1.11/1.12 §e§lNEW PTL GAME:§b§l THE BRIDGE";
93
- const result = cleanTags(motdText);
93
+ const result = cleanCodes(motdText);
94
94
  console.log(result);
95
95
 
96
96
  /* result, callback Text:
@@ -99,7 +99,7 @@ console.log(result);
99
99
  ```
100
100
 
101
101
 
102
- ### `autoToHtml(string | object)`
102
+ ### `autoToHTML(string | object)`
103
103
  auto check MOTD data type then return same html result.
104
104
 
105
105
  ```typescript
@@ -159,7 +159,7 @@ let jsonExample = {
159
159
  ],
160
160
  "text": ""
161
161
  };
162
- let autoJsonResult = motdParser.autoToHtml(jsonExample);
162
+ let autoJsonResult = motdParser.autoToHTML(jsonExample);
163
163
  console.log(autoJsonResult);
164
164
 
165
165
  /* auto JSON Result, callback HTML:
@@ -167,7 +167,7 @@ console.log(autoJsonResult);
167
167
  */
168
168
 
169
169
  let textExample = "§aHypixel Network §7§c1.8/1.9/1.10/1.11/1.12 §e§lNEW PTL GAME:§b§l THE BRIDGE";
170
- let autoTextResult = motdParser.autoToHtml(textExample);
170
+ let autoTextResult = motdParser.autoToHTML(textExample);
171
171
  console.log(autoTextResult);
172
172
 
173
173
  /* auto Text Result, callback HTML:
@@ -219,7 +219,7 @@ console.log(result);
219
219
  ```
220
220
 
221
221
 
222
- ### `JSONToHtml(object)`
222
+ ### `JSONToHTML(object)`
223
223
  convert text to motd json.
224
224
  ```typescript
225
225
  let mcfalloutJson = {
@@ -267,7 +267,7 @@ let mcfalloutJson = {
267
267
  ],
268
268
  "text": ""
269
269
  }
270
- let result = motdParser.JSONToHtml(mcfalloutJson);
270
+ let result = motdParser.JSONToHTML(mcfalloutJson);
271
271
  console.log(result)
272
272
 
273
273
  /* result, callback HTML:
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var F=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames,m=Object.getOwnPropertySymbols;var h=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var A=(t,e,o)=>e in t?F(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o,g=(t,e)=>{for(var o in e||(e={}))h.call(e,o)&&A(t,o,e[o]);if(m)for(var o of m(e))N.call(e,o)&&A(t,o,e[o]);return t};var R=(t,e)=>{for(var o in e)F(t,o,{get:e[o],enumerable:!0})},J=(t,e,o,f)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of E(e))!h.call(t,i)&&i!==o&&F(t,i,{get:()=>e[i],enumerable:!(f=H(e,i))||f.enumerable});return t};var M=t=>J(F({},"__esModule",{value:!0}),t);var B={};R(B,{JSONToHtml:()=>X,autoToHtml:()=>v,cleanTags:()=>K,default:()=>z,jsonRender:()=>P,motdParser:()=>p,textToHTML:()=>q,textToJSON:()=>G});module.exports=M(B);function b(t){return!!t}var O=t=>t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/\n/g,"<br/>");var S={"\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;"},w={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;"},j={"\xA7k":"obfuscated","\xA7l":"bold","\xA7m":"strikethrough","\xA7n":"underlined","\xA7o":"italic","\xA7r":""},d={"\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"},L={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 e=/(?:§)([0-9a-fA-FklmnorFKLMNOR])/g,o="";return o=t.replace(e,""),o}function y(t){let e=t,o=/([§][0-9a-fA-FklmnorFKLMNOR])/g,f=new RegExp(o.source),i=e.split(f),r="",n="",a="";return i.forEach(s=>{let l=s.toLowerCase();if(Object.hasOwn(d,l))n=d[l];else if(Object.hasOwn(S,l))r=S[l];else{let c="",x=s;n!==""&&(c=`color:${n};`),x!==""&&(x=O(x),c.length!==0||r.length!==0?a+=`<span style="${c}${r}">${x}</span>`:a+=x)}}),a}function k(t){let e=t,o=/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g,f=new RegExp(o.source),i=e.split(f),r="",n="",a={text:"",extra:[]};i.forEach(l=>{let c=l.toLowerCase();if(Object.hasOwn(d,c))n=d[c];else if(Object.hasOwn(j,c))r=j[c];else{let x={text:"",extra:[]};r!==""&&(x[r]=!0),x.text=l,n!==""&&(x.color=n),typeof a.extra=="object"&&a.extra.push(x)}});let s=[];return a.extra&&(a.extra.length>1?a.extra.forEach((l,c)=>{l.text===""?a.extra&&typeof a.extra[c+1]=="object"&&s.push(g(g({},l),a.extra[c+1])):l.text!==(s[s.length-1]&&s[s.length-1].text)&&s.push(l)}):s.push(a.extra[0])),s=s.filter(l=>l.text!==""),{text:a.text,extra:s}}function T(t){let e="",o="",f="";for(let r of Object.keys(t)){if(r=r.toLowerCase(),Object.hasOwn(w,r)&&t[r]&&(f+=`${w[r]}`),r==="color"){let n=t[r];if(typeof n=="string")if(Object.hasOwn(L,n)){o=`color:${L[n]};`;continue}else if(Object.hasOwn(d,n)){o=`color:${d[n]};`;continue}else{o=`color:${n};`;continue}}if(r==="extra"&&typeof t.extra=="object"){t.text!==void 0&&(typeof t.text=="string"||typeof t.text=="number")&&(e+=y(String(t.text)));for(let n of t.extra)b(n)&&(e+=T(n))}}if(t.extra===void 0&&t.text!==void 0){let r=t.text;(typeof t.text=="string"||typeof t.text=="number")&&(e+=y(String(r)))}let i="";return f.length!==0||o.length!==0?i=`<span style="${o+f}">${e}</span>`:i=e,i}function u(t){return T(JSON.parse(JSON.stringify(t)))}function $(t){return typeof t=="object"?u(t):typeof t=="string"?u(k(t)):"unknown motd data type"}var _={cleanTags:C,textToHTML:y,textToJSON:k,JSONToHtml:T,jsonRender:u,autoToHtml:$},p=_;var K=p.cleanTags,q=p.textToHTML,G=p.textToJSON,X=p.JSONToHtml,P=p.jsonRender,v=p.autoToHtml,z=p;0&&(module.exports={JSONToHtml,autoToHtml,cleanTags,jsonRender,motdParser,textToHTML,textToJSON});
1
+ var u=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var J=(t,o)=>{for(var a in o)u(t,a,{get:o[a],enumerable:!0})},k=(t,o,a,p)=>{if(o&&typeof o=="object"||typeof o=="function")for(let f of M(o))!j.call(t,f)&&f!==a&&u(t,f,{get:()=>o[f],enumerable:!(p=w(o,f))||p.enumerable});return t};var C=t=>k(u({},"__esModule",{value:!0}),t);var R={};J(R,{JSONRender:()=>g,JSONToHTML:()=>d,autoToHTML:()=>y,cleanCodes:()=>b,cleanHtmlTags:()=>h,default:()=>E,htmlStringFormatting:()=>T,isMotdJSONType:()=>A,textToHTML:()=>c,textToJSON:()=>F});module.exports=C(R);function A(t){return!!t}function T(t){return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/\n/g,"<br/>")}function h(t){return t.replace(/<[^>]*>?/gm,"")}function b(t){let o=/(?:§)([0-9a-fA-FklmnorFKLMNOR])/g,a="";return a=t.replace(o,""),a}var O={"\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;"},L={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;"},S={"\xA7k":"obfuscated","\xA7l":"bold","\xA7m":"strikethrough","\xA7n":"underlined","\xA7o":"italic","\xA7r":"reset"},m={"\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 c(t){let o=t,a=/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g,p=new RegExp(a.source),f=o.split(p).filter(l=>l!==""),e="",n="",r="";return f.forEach(l=>{let i=l.toLowerCase();if(Object.hasOwn(m,i))n=m[i],i==="\xA7f"&&(e="");else if(Object.hasOwn(O,i))i==="\xA7r"?(n="",e=""):e+=O[i];else{let s="",x=l;n!==""&&(s=`color:${n};`),x!==""&&(x=T(x),s.length!==0||e.length!==0?r+=`<span style="${s}${e}">${x}</span>`:r+=x)}}),r}function d(t){let o="",a="",p="";for(let e of Object.keys(t)){if(e=e.toLowerCase(),Object.hasOwn(L,e)&&t[e]&&(p+=`${L[e]}`),e==="color"){let n=t[e],r="";typeof n=="string"&&(Object.hasOwn(H,n)?r=H[n]:Object.hasOwn(m,n)?r=m[n]:r=n),r!==""&&(a=`color:${r};`)}if(e==="extra"&&typeof t.extra=="object"){t.text!==void 0&&(typeof t.text=="string"||typeof t.text=="number")&&(o+=c(String(t.text)));for(let n of t.extra)A(n)&&(o+=d(n))}}if(t.extra===void 0&&t.text!==void 0){let e=t.text;(typeof t.text=="string"||typeof t.text=="number")&&(o+=c(String(e)))}let f="";return p.length!==0||a.length!==0?f=`<span style="${a+p}">${o}</span>`:f=o,f}function F(t){let o=t,a=/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g,p=new RegExp(a.source),f=o.split(p),e="",n="",r={text:"",extra:[]};f.forEach(i=>{let s=i.toLowerCase();if(Object.hasOwn(m,s))n=m[s],s==="\xA7f"&&(e="");else if(Object.hasOwn(S,s))s==="\xA7r"?(e="",n=""):e=S[s];else{let x={text:"",extra:[]};e!==""&&(x[e]=!0),x.text=i,n!==""&&(x.color=n),typeof r.extra=="object"&&r.extra.push(x)}});let l=[];return r.extra&&(r.extra.length>1?r.extra.forEach((i,s)=>{i.text===""?r.extra&&typeof r.extra[s+1]=="object"&&l.push({...i,...r.extra[s+1]}):i.text!==(l[l.length-1]&&l[l.length-1].text)&&l.push(i)}):l.push(r.extra[0])),l=l.filter(i=>i.text!==""),{text:r.text,extra:l}}function g(t){return d(JSON.parse(JSON.stringify(t)))}function y(t){return typeof t=="object"?g(t):typeof t=="string"?c(t):"unknown motd data type"}var N={textToHTML:c,textToJSON:F,JSONToHTML:d,JSONRender:g,autoToHTML:y,htmlStringFormatting:T,cleanCodes:b,cleanHtmlTags:h},E=N;0&&(module.exports={JSONRender,JSONToHTML,autoToHTML,cleanCodes,cleanHtmlTags,htmlStringFormatting,isMotdJSONType,textToHTML,textToJSON});
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/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// 類型檢查\nexport function isMotdJSONType(object: object): object is motdJsonType {\n return !!object;\n}\n\n/**\n *\n * #### replace all html tags to &...\n */\nexport const htmlStringFormatting = (text: string): string => {\n return (\n text\n // space\n // .replace(/ /g, \"\\u00a0\")\n //\n .replace(/&/g, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n .replace(/'/g, \"&#39;\")\n // return\n .replace(/\\n/g, \"<br/>\")\n );\n // .replace(/\\//g, '&#x2F;');\n};\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { extraLibraryType, motdJsonType } from \"./types\";\nimport { isMotdJSONType, htmlStringFormatting } from \"./utils\";\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\": \"\",\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\n// clean tags\n/**\n * #### `cleanTags(string)`\n * Clean all tags from motd source string.\n */\nfunction 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\n// text to html\n/**\n * #### `textToHTML(string)`\n * Convert motd text to html.\n */\nfunction 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=\"${resultColor}${fontStyle}\">${textContent}</span>`;\n } else {\n resultHTML += textContent;\n }\n }\n }\n });\n\n return resultHTML;\n}\n\n// text to json\n/**\n * #### `textToJSON(string)`\n * Convert motd text to JSON.\n */\nfunction 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 // console.log('resultObject', resultObject);\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// json convert to html\n/**\n * #### `JSONToString(string)`\n * Convert JSON to HTML.\n */\nfunction 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=\"${colorHex + fontStyle}\">${htmlElement}</span>`;\n } else {\n returnHTML = htmlElement;\n }\n\n return returnHTML;\n}\n\n// JSON full convert include newline\nfunction jsonRender(json: motdJsonType | object) {\n return parseJSONToHTML(JSON.parse(JSON.stringify(json)));\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 jsonRender(parseTextToJSON(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/mc-motd-parser)\n * (c) 2023 Kevin Zheng\n *\n * Released under the MIT license\n */\nconst defaultModules = {\n // delete all tags\n cleanTags,\n // text convert to HTML\n textToHTML,\n // text convert to JSON\n textToJSON: parseTextToJSON,\n // JSON convert to HTML\n JSONToHtml: parseJSONToHTML,\n // JSON full convert HTML (include enter)\n jsonRender,\n // auto check type to convert\n autoToHtml,\n};\n\nexport default defaultModules;\n"],"mappings":"4rBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,EAAA,eAAAC,EAAA,cAAAC,EAAA,YAAAC,EAAA,eAAAC,EAAA,eAAAC,EAAA,eAAAC,EAAA,eAAAC,IAAA,eAAAC,EAAAV,GCSO,SAASW,EAAeC,EAAwC,CACrE,MAAO,CAAC,CAACA,CACX,CAMO,IAAMC,EAAwBC,GAEjCA,EAIG,QAAQ,KAAM,OAAO,EACrB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,QAAQ,EACtB,QAAQ,KAAM,OAAO,EAErB,QAAQ,MAAO,OAAO,ECjB7B,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,EACR,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,UACN,IAAK,UACL,aAAc,UACd,OAAQ,UACR,MAAO,SACT,EASA,SAASC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AW,MAAS,UAAY,OAAOA,EAAW,MAAS,YAGtEC,GAAexB,EAAW,OAAOuB,EAAW,IAAI,CAAC,GAInD,QAAWI,KAAmBJ,EAAW,MAEnCK,EAAeD,CAAe,IAChCH,GAAeF,EAAgBK,CAAe,EAGpD,CACF,CAGA,GAAIJ,EAAW,QAAU,QAAaA,EAAW,OAAS,OAAW,CACnE,IAAMM,EAAcN,EAAW,MAE7B,OAAOA,EAAW,MAAS,UAC3B,OAAOA,EAAW,MAAS,YAG3BC,GAAexB,EAAW,OAAO6B,CAAW,CAAC,EAEjD,CAIA,IAAIC,EAAa,GACjB,OAAIxB,EAAU,SAAW,GAAKC,EAAS,SAAW,EAChDuB,EAAa,gBAAgBvB,EAAWD,MAAckB,WAEtDM,EAAaN,EAGRM,CACT,CAGA,SAASC,EAAWC,EAA6B,CAC/C,OAAOV,EAAgB,KAAK,MAAM,KAAK,UAAUU,CAAI,CAAC,CAAC,CACzD,CAMA,SAASC,EAAWC,EAA8C,CAEhE,OAAI,OAAOA,GAAS,SAEXH,EAAWG,CAAI,EACb,OAAOA,GAAS,SAElBH,EAAWjB,EAAgBoB,CAAI,CAAC,EAEhC,wBAEX,CAYA,IAAMC,EAAiB,CAErB,UAAAvC,EAEA,WAAAI,EAEA,WAAYc,EAEZ,WAAYQ,EAEZ,WAAAS,EAEA,WAAAE,CACF,EAEOG,EAAQD,EFtZR,IAAME,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","extras","extraFontStyles","textToJsonExtras","colorCodeToHex","extraColorsToHex","cleanTags","text","REGEX","textResult","textToHTML","motdString","motdText","colorCodeReg","codeREGEX","codeSplit","fontStyle","colorHex","resultHTML","item","motdStringToLowerCase","resultColor","textContent","htmlStringFormatting","parseTextToJSON","textSplit","resultObject","stringToLowerCase","innerObject","newExtra","index","__spreadValues","parseJSONToHTML","sourceJson","htmlElement","key","colorKey","sourceJsonExtra","isMotdJSONType","currentText","returnHTML","jsonRender","json","autoToHtml","motd","defaultModules","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/parser/autoToHTML.ts"],"sourcesContent":["/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\nimport {\n htmlStringFormatting,\n cleanCodes,\n cleanHtmlTags,\n} from \"./utils\";\nimport {\n JSONToHTML,\n textToHTML,\n textToJSON,\n JSONRender,\n autoToHTML,\n} from \"./parser\";\n\n\n\n\n\nexport * from './utils';\nexport * from \"./parser\";\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 motdParser = {\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 codes\n cleanCodes,\n // clean all html tags\n cleanHtmlTags,\n};\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, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n .replace(/'/g, \"&#39;\")\n // return\n .replace(/\\n/g, \"<br/>\")\n );\n // .replace(/\\//g, '&#x2F;');\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 codes\n * \n * Clean all codes from motd source string.\n * \n * @param {string} text - motd string include tag § will remove\n */\nexport function cleanCodes(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-f0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const codeSplit = motdText.split(codeREGEX).filter(item => item !== '');\n\n let fontStyle = \"\";\n let colorHex = \"\";\n let resultHTML = \"\";\n\n codeSplit.forEach((item) => {\n const motdStringToLowerCase = item.toLowerCase();\n // console.log('motdStringToLowerCase', motdStringToLowerCase);\n\n // 過濾 hex\n if (Object.hasOwn(colorCodeToHex, motdStringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[motdStringToLowerCase];\n\n // §f reset\n if(motdStringToLowerCase === \"§f\") {\n fontStyle = \"\";\n }\n // 過濾文字 style\n } else if (Object.hasOwn(extras, motdStringToLowerCase)) {\n if(motdStringToLowerCase === \"§r\") {\n colorHex = \"\";\n fontStyle = \"\";\n } else {\n // font style code 轉換\n // console.log(`偵測出 style ${ extras[motdStringToLowerCase] }`);\n fontStyle += extras[motdStringToLowerCase];\n }\n // console.log('motdStringToLowerCase', 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=\"${resultColor}${fontStyle}\">${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 colorStyle = \"\";\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 let colorHex = \"\";\n\n if (typeof colorKey === \"string\") {\n // Hex color\n if (Object.hasOwn(extraColorsToHex, colorKey)) {\n colorHex = extraColorsToHex[colorKey];\n // color code\n } else if (Object.hasOwn(colorCodeToHex, colorKey)) {\n colorHex = colorCodeToHex[colorKey];\n // custom color\n } else {\n // custom hex color code mode\n colorHex = colorKey;\n }\n }\n\n if(colorHex !== \"\") {\n colorStyle = `color:${colorHex};`;\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 || colorStyle.length !== 0) {\n returnHTML = `<span style=\"${colorStyle + fontStyle}\">${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 // §f reset\n if(stringToLowerCase === \"§f\") {\n fontStyle = \"\";\n }\n } else if (Object.hasOwn(textToJsonExtras, stringToLowerCase)) {\n if(stringToLowerCase === \"§r\") {\n fontStyle = \"\";\n colorHex = \"\";\n } else {\n // font style code 轉換\n //console.log(`偵測出 style ${ textToJsonExtras[item] }`)\n fontStyle = textToJsonExtras[stringToLowerCase];\n }\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 JSONToHTML,\n textToHTML,\n} from \"./\";\n\n\n\n\n\n\n// JSON full convert include newline\nexport function JSONRender(json: object | motdJsonType) {\n return JSONToHTML(JSON.parse(JSON.stringify(json)));\n}\n\n\n\n/**\n * auto check data type then convert to html.\n */\nexport function autoToHTML(motd: string | object | motdJsonType): 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 // console.log('textToJSON(motd)', textToJSON(motd));\n return textToHTML(motd);\n // return jsonRender(textToJSON(motd));\n } else {\n return \"unknown motd data type\";\n }\n}\n\nexport default autoToHTML;\n"],"mappings":"4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,EAAA,eAAAC,EAAA,eAAAC,EAAA,eAAAC,EAAA,kBAAAC,EAAA,YAAAC,EAAA,yBAAAC,EAAA,mBAAAC,EAAA,eAAAC,EAAA,eAAAC,IAAA,eAAAC,EAAAZ,GCYO,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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,EAAa,MAAM,EAAkB,GAMvDM,EAAWA,EAAS,OAAQL,GAASA,EAAK,OAAS,EAAE,EAG9C,CACL,KAAMD,EAAa,KACnB,MAAOM,CACT,CACF,CCpGO,SAASE,EAAWC,EAA6B,CACtD,OAAOC,EAAW,KAAK,MAAM,KAAK,UAAUD,CAAI,CAAC,CAAC,CACpD,CAOO,SAASE,EAAWC,EAA8C,CAEvE,OAAI,OAAOA,GAAS,SAEXJ,EAAWI,CAAI,EACb,OAAOA,GAAS,SAGlBC,EAAWD,CAAI,EAGf,wBAEX,CNJA,IAAME,EAAa,CAEjB,WAAAC,EAEA,WAAAC,EAEA,WAAAC,EAEA,WAAAC,EAEA,WAAAC,EAGA,qBAAAC,EAEA,WAAAC,EAEA,cAAAC,CACF,EAEOC,EAAQT","names":["src_exports","__export","JSONRender","parseJSONToHTML","autoToHTML","cleanCodes","cleanHtmlTags","src_default","htmlStringFormatting","isMotdJSONType","textToHTML","parseTextToJSON","__toCommonJS","isMotdJSONType","object","htmlStringFormatting","text","cleanHtmlTags","cleanCodes","REGEX","textResult","extras","extraFontStyles","textToJsonExtras","colorCodeToHex","extraColorsToHex","textToHTML","motdString","motdText","colorCodeReg","codeREGEX","codeSplit","item","fontStyle","colorHex","resultHTML","motdStringToLowerCase","colorCodeToHex","extras","resultColor","textContent","htmlStringFormatting","parseJSONToHTML","sourceJson","htmlElement","colorStyle","fontStyle","key","extraFontStyles","colorKey","colorHex","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","textToHTML","parseTextToJSON","parseJSONToHTML","JSONRender","autoToHTML","htmlStringFormatting","cleanCodes","cleanHtmlTags","src_default"]}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var L=Object.defineProperty;var T=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable;var m=(t,o,e)=>o in t?L(t,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[o]=e,F=(t,o)=>{for(var e in o||(o={}))k.call(o,e)&&m(t,e,o[e]);if(T)for(var e of T(o))H.call(o,e)&&m(t,e,o[e]);return t};function A(t){return!!t}var h=t=>t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/\n/g,"<br/>");var b={"\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;"},O={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;"},S={"\xA7k":"obfuscated","\xA7l":"bold","\xA7m":"strikethrough","\xA7n":"underlined","\xA7o":"italic","\xA7r":""},d={"\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"},w={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 E(t){let o=/(?:§)([0-9a-fA-FklmnorFKLMNOR])/g,e="";return e=t.replace(o,""),e}function g(t){let o=t,e=/([§][0-9a-fA-FklmnorFKLMNOR])/g,x=new RegExp(e.source),p=o.split(x),r="",n="",a="";return p.forEach(i=>{let l=i.toLowerCase();if(Object.hasOwn(d,l))n=d[l];else if(Object.hasOwn(b,l))r=b[l];else{let s="",c=i;n!==""&&(s=`color:${n};`),c!==""&&(c=h(c),s.length!==0||r.length!==0?a+=`<span style="${s}${r}">${c}</span>`:a+=c)}}),a}function j(t){let o=t,e=/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g,x=new RegExp(e.source),p=o.split(x),r="",n="",a={text:"",extra:[]};p.forEach(l=>{let s=l.toLowerCase();if(Object.hasOwn(d,s))n=d[s];else if(Object.hasOwn(S,s))r=S[s];else{let c={text:"",extra:[]};r!==""&&(c[r]=!0),c.text=l,n!==""&&(c.color=n),typeof a.extra=="object"&&a.extra.push(c)}});let i=[];return a.extra&&(a.extra.length>1?a.extra.forEach((l,s)=>{l.text===""?a.extra&&typeof a.extra[s+1]=="object"&&i.push(F(F({},l),a.extra[s+1])):l.text!==(i[i.length-1]&&i[i.length-1].text)&&i.push(l)}):i.push(a.extra[0])),i=i.filter(l=>l.text!==""),{text:a.text,extra:i}}function u(t){let o="",e="",x="";for(let r of Object.keys(t)){if(r=r.toLowerCase(),Object.hasOwn(O,r)&&t[r]&&(x+=`${O[r]}`),r==="color"){let n=t[r];if(typeof n=="string")if(Object.hasOwn(w,n)){e=`color:${w[n]};`;continue}else if(Object.hasOwn(d,n)){e=`color:${d[n]};`;continue}else{e=`color:${n};`;continue}}if(r==="extra"&&typeof t.extra=="object"){t.text!==void 0&&(typeof t.text=="string"||typeof t.text=="number")&&(o+=g(String(t.text)));for(let n of t.extra)A(n)&&(o+=u(n))}}if(t.extra===void 0&&t.text!==void 0){let r=t.text;(typeof t.text=="string"||typeof t.text=="number")&&(o+=g(String(r)))}let p="";return x.length!==0||e.length!==0?p=`<span style="${e+x}">${o}</span>`:p=o,p}function y(t){return u(JSON.parse(JSON.stringify(t)))}function N(t){return typeof t=="object"?y(t):typeof t=="string"?y(j(t)):"unknown motd data type"}var R={cleanTags:E,textToHTML:g,textToJSON:j,JSONToHtml:u,jsonRender:y,autoToHtml:N},f=R;var q=f.cleanTags,G=f.textToHTML,X=f.textToJSON,P=f.JSONToHtml,v=f.jsonRender,z=f.autoToHtml,B=f;export{P as JSONToHtml,z as autoToHtml,q as cleanTags,B as default,v as jsonRender,f as motdParser,G as textToHTML,X as textToJSON};
1
+ function O(t){return!!t}function T(t){return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/\n/g,"<br/>")}function L(t){return t.replace(/<[^>]*>?/gm,"")}function S(t){let l=/(?:§)([0-9a-fA-FklmnorFKLMNOR])/g,f="";return f=t.replace(l,""),f}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"},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"},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 x(t){let l=t,f=/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g,c=new RegExp(f.source),m=l.split(c).filter(i=>i!==""),e="",r="",o="";return m.forEach(i=>{let n=i.toLowerCase();if(Object.hasOwn(p,n))r=p[n],n==="\xA7f"&&(e="");else if(Object.hasOwn(y,n))n==="\xA7r"?(r="",e=""):e+=y[n];else{let a="",s=i;r!==""&&(a=`color:${r};`),s!==""&&(s=T(s),a.length!==0||e.length!==0?o+=`<span style="${a}${e}">${s}</span>`:o+=s)}}),o}function d(t){let l="",f="",c="";for(let e of Object.keys(t)){if(e=e.toLowerCase(),Object.hasOwn(u,e)&&t[e]&&(c+=`${u[e]}`),e==="color"){let r=t[e],o="";typeof r=="string"&&(Object.hasOwn(h,r)?o=h[r]:Object.hasOwn(p,r)?o=p[r]:o=r),o!==""&&(f=`color:${o};`)}if(e==="extra"&&typeof t.extra=="object"){t.text!==void 0&&(typeof t.text=="string"||typeof t.text=="number")&&(l+=x(String(t.text)));for(let r of t.extra)O(r)&&(l+=d(r))}}if(t.extra===void 0&&t.text!==void 0){let e=t.text;(typeof t.text=="string"||typeof t.text=="number")&&(l+=x(String(e)))}let m="";return c.length!==0||f.length!==0?m=`<span style="${f+c}">${l}</span>`:m=l,m}function F(t){let l=t,f=/([§][0-9a-f0-9a-fA-FklmnorFKLMNOR])/g,c=new RegExp(f.source),m=l.split(c),e="",r="",o={text:"",extra:[]};m.forEach(n=>{let a=n.toLowerCase();if(Object.hasOwn(p,a))r=p[a],a==="\xA7f"&&(e="");else if(Object.hasOwn(A,a))a==="\xA7r"?(e="",r=""):e=A[a];else{let s={text:"",extra:[]};e!==""&&(s[e]=!0),s.text=n,r!==""&&(s.color=r),typeof o.extra=="object"&&o.extra.push(s)}});let i=[];return o.extra&&(o.extra.length>1?o.extra.forEach((n,a)=>{n.text===""?o.extra&&typeof o.extra[a+1]=="object"&&i.push({...n,...o.extra[a+1]}):n.text!==(i[i.length-1]&&i[i.length-1].text)&&i.push(n)}):i.push(o.extra[0])),i=i.filter(n=>n.text!==""),{text:o.text,extra:i}}function g(t){return d(JSON.parse(JSON.stringify(t)))}function b(t){return typeof t=="object"?g(t):typeof t=="string"?x(t):"unknown motd data type"}var H={textToHTML:x,textToJSON:F,JSONToHTML:d,JSONRender:g,autoToHTML:b,htmlStringFormatting:T,cleanCodes:S,cleanHtmlTags:L},I=H;export{g as JSONRender,d as JSONToHTML,b as autoToHTML,S as cleanCodes,L as cleanHtmlTags,I as default,T as htmlStringFormatting,O as isMotdJSONType,x as textToHTML,F as textToJSON};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/utils.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// 類型檢查\nexport function isMotdJSONType(object: object): object is motdJsonType {\n return !!object;\n}\n\n/**\n *\n * #### replace all html tags to &...\n */\nexport const htmlStringFormatting = (text: string): string => {\n return (\n text\n // space\n // .replace(/ /g, \"\\u00a0\")\n //\n .replace(/&/g, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n .replace(/'/g, \"&#39;\")\n // return\n .replace(/\\n/g, \"<br/>\")\n );\n // .replace(/\\//g, '&#x2F;');\n};\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\n\nimport { extraLibraryType, motdJsonType } from \"./types\";\nimport { isMotdJSONType, htmlStringFormatting } from \"./utils\";\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\": \"\",\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\n// clean tags\n/**\n * #### `cleanTags(string)`\n * Clean all tags from motd source string.\n */\nfunction 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\n// text to html\n/**\n * #### `textToHTML(string)`\n * Convert motd text to html.\n */\nfunction 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=\"${resultColor}${fontStyle}\">${textContent}</span>`;\n } else {\n resultHTML += textContent;\n }\n }\n }\n });\n\n return resultHTML;\n}\n\n// text to json\n/**\n * #### `textToJSON(string)`\n * Convert motd text to JSON.\n */\nfunction 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 // console.log('resultObject', resultObject);\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// json convert to html\n/**\n * #### `JSONToString(string)`\n * Convert JSON to HTML.\n */\nfunction 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=\"${colorHex + fontStyle}\">${htmlElement}</span>`;\n } else {\n returnHTML = htmlElement;\n }\n\n return returnHTML;\n}\n\n// JSON full convert include newline\nfunction jsonRender(json: motdJsonType | object) {\n return parseJSONToHTML(JSON.parse(JSON.stringify(json)));\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 jsonRender(parseTextToJSON(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/mc-motd-parser)\n * (c) 2023 Kevin Zheng\n *\n * Released under the MIT license\n */\nconst defaultModules = {\n // delete all tags\n cleanTags,\n // text convert to HTML\n textToHTML,\n // text convert to JSON\n textToJSON: parseTextToJSON,\n // JSON convert to HTML\n JSONToHtml: parseJSONToHTML,\n // JSON full convert HTML (include enter)\n jsonRender,\n // auto check type to convert\n autoToHtml,\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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AAS,UAC3B,OAAOA,EAAW,MAAS,YAG3BC,GAAexB,EAAW,OAAO6B,CAAW,CAAC,EAEjD,CAIA,IAAIC,EAAa,GACjB,OAAIxB,EAAU,SAAW,GAAKC,EAAS,SAAW,EAChDuB,EAAa,gBAAgBvB,EAAWD,MAAckB,WAEtDM,EAAaN,EAGRM,CACT,CAGA,SAASC,EAAWC,EAA6B,CAC/C,OAAOV,EAAgB,KAAK,MAAM,KAAK,UAAUU,CAAI,CAAC,CAAC,CACzD,CAMA,SAASC,EAAWC,EAA8C,CAEhE,OAAI,OAAOA,GAAS,SAEXH,EAAWG,CAAI,EACb,OAAOA,GAAS,SAElBH,EAAWjB,EAAgBoB,CAAI,CAAC,EAEhC,wBAEX,CAYA,IAAMC,EAAiB,CAErB,UAAAvC,EAEA,WAAAI,EAEA,WAAYc,EAEZ,WAAYQ,EAEZ,WAAAS,EAEA,WAAAE,CACF,EAEOG,EAAQD,ECtZR,IAAME,EAAYC,EAAW,UACvBC,EAAaD,EAAW,WACxBE,EAAaF,EAAW,WACxBG,EAAaH,EAAW,WACxBI,EAAaJ,EAAW,WACxBK,EAAaL,EAAW,WAE9BM,EAAQN","names":["isMotdJSONType","object","htmlStringFormatting","text","extras","extraFontStyles","textToJsonExtras","colorCodeToHex","extraColorsToHex","cleanTags","text","REGEX","textResult","textToHTML","motdString","motdText","colorCodeReg","codeREGEX","codeSplit","fontStyle","colorHex","resultHTML","item","motdStringToLowerCase","resultColor","textContent","htmlStringFormatting","parseTextToJSON","textSplit","resultObject","stringToLowerCase","innerObject","newExtra","index","__spreadValues","parseJSONToHTML","sourceJson","htmlElement","key","colorKey","sourceJsonExtra","isMotdJSONType","currentText","returnHTML","jsonRender","json","autoToHtml","motd","defaultModules","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/parser/autoToHTML.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, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n .replace(/'/g, \"&#39;\")\n // return\n .replace(/\\n/g, \"<br/>\")\n );\n // .replace(/\\//g, '&#x2F;');\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 codes\n * \n * Clean all codes from motd source string.\n * \n * @param {string} text - motd string include tag § will remove\n */\nexport function cleanCodes(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-f0-9a-fA-FklmnorFKLMNOR])/g;\n const codeREGEX = new RegExp(colorCodeReg.source);\n const codeSplit = motdText.split(codeREGEX).filter(item => item !== '');\n\n let fontStyle = \"\";\n let colorHex = \"\";\n let resultHTML = \"\";\n\n codeSplit.forEach((item) => {\n const motdStringToLowerCase = item.toLowerCase();\n // console.log('motdStringToLowerCase', motdStringToLowerCase);\n\n // 過濾 hex\n if (Object.hasOwn(colorCodeToHex, motdStringToLowerCase)) {\n //console.log(`偵測出 ${ colorCodeToHex[item] }`)\n colorHex = colorCodeToHex[motdStringToLowerCase];\n\n // §f reset\n if(motdStringToLowerCase === \"§f\") {\n fontStyle = \"\";\n }\n // 過濾文字 style\n } else if (Object.hasOwn(extras, motdStringToLowerCase)) {\n if(motdStringToLowerCase === \"§r\") {\n colorHex = \"\";\n fontStyle = \"\";\n } else {\n // font style code 轉換\n // console.log(`偵測出 style ${ extras[motdStringToLowerCase] }`);\n fontStyle += extras[motdStringToLowerCase];\n }\n // console.log('motdStringToLowerCase', 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=\"${resultColor}${fontStyle}\">${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 colorStyle = \"\";\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 let colorHex = \"\";\n\n if (typeof colorKey === \"string\") {\n // Hex color\n if (Object.hasOwn(extraColorsToHex, colorKey)) {\n colorHex = extraColorsToHex[colorKey];\n // color code\n } else if (Object.hasOwn(colorCodeToHex, colorKey)) {\n colorHex = colorCodeToHex[colorKey];\n // custom color\n } else {\n // custom hex color code mode\n colorHex = colorKey;\n }\n }\n\n if(colorHex !== \"\") {\n colorStyle = `color:${colorHex};`;\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 || colorStyle.length !== 0) {\n returnHTML = `<span style=\"${colorStyle + fontStyle}\">${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 // §f reset\n if(stringToLowerCase === \"§f\") {\n fontStyle = \"\";\n }\n } else if (Object.hasOwn(textToJsonExtras, stringToLowerCase)) {\n if(stringToLowerCase === \"§r\") {\n fontStyle = \"\";\n colorHex = \"\";\n } else {\n // font style code 轉換\n //console.log(`偵測出 style ${ textToJsonExtras[item] }`)\n fontStyle = textToJsonExtras[stringToLowerCase];\n }\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 JSONToHTML,\n textToHTML,\n} from \"./\";\n\n\n\n\n\n\n// JSON full convert include newline\nexport function JSONRender(json: object | motdJsonType) {\n return JSONToHTML(JSON.parse(JSON.stringify(json)));\n}\n\n\n\n/**\n * auto check data type then convert to html.\n */\nexport function autoToHTML(motd: string | object | motdJsonType): 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 // console.log('textToJSON(motd)', textToJSON(motd));\n return textToHTML(motd);\n // return jsonRender(textToJSON(motd));\n } else {\n return \"unknown motd data type\";\n }\n}\n\nexport default autoToHTML;\n","/*\n * minecraft motd parser\n * (c) 2023 Kevin Zheng\n * Released under the MIT license\n */\nimport {\n htmlStringFormatting,\n cleanCodes,\n cleanHtmlTags,\n} from \"./utils\";\nimport {\n JSONToHTML,\n textToHTML,\n textToJSON,\n JSONRender,\n autoToHTML,\n} from \"./parser\";\n\n\n\n\n\nexport * from './utils';\nexport * from \"./parser\";\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 motdParser = {\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 codes\n cleanCodes,\n // clean all html tags\n cleanHtmlTags,\n};\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,EAAWF,EAAc,CACvC,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,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textResult","extras","extraFontStyles","textToJsonExtras","colorCodeToHex","extraColorsToHex","textToHTML","motdString","motdText","colorCodeReg","codeREGEX","codeSplit","item","fontStyle","colorHex","resultHTML","motdStringToLowerCase","colorCodeToHex","extras","resultColor","textContent","htmlStringFormatting","parseJSONToHTML","sourceJson","htmlElement","colorStyle","fontStyle","key","extraFontStyles","colorKey","colorHex","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","textToHTML","parseTextToJSON","parseJSONToHTML","JSONRender","autoToHTML","htmlStringFormatting","cleanCodes","cleanHtmlTags","src_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.1",
4
+ "version": "1.1.2-1",
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,12 +1,15 @@
1
- import motdParser from "./motdParser";
2
- export { motdParser };
3
- export declare const cleanTags: (text: string) => string;
4
- export declare const textToHTML: (motdString: string) => string;
5
- export declare const textToJSON: (text: string) => {
6
- text: string | number;
7
- extra: import("./types").motdJsonType[];
1
+ import { htmlStringFormatting, cleanCodes, cleanHtmlTags } from "./utils";
2
+ import { JSONToHTML, textToHTML, textToJSON, JSONRender, autoToHTML } from "./parser";
3
+ export * from './utils';
4
+ export * from "./parser";
5
+ declare const motdParser: {
6
+ textToHTML: typeof textToHTML;
7
+ textToJSON: typeof textToJSON;
8
+ JSONToHTML: typeof JSONToHTML;
9
+ JSONRender: typeof JSONRender;
10
+ autoToHTML: typeof autoToHTML;
11
+ htmlStringFormatting: typeof htmlStringFormatting;
12
+ cleanCodes: typeof cleanCodes;
13
+ cleanHtmlTags: typeof cleanHtmlTags;
8
14
  };
9
- export declare const JSONToHtml: (sourceJson: import("./types").motdJsonType) => string;
10
- export declare const jsonRender: (json: object | import("./types").motdJsonType) => string;
11
- export declare const autoToHtml: (motd: string | object | import("./types").motdJsonType) => string;
12
15
  export default motdParser;
@@ -1,39 +1,19 @@
1
1
  import { motdJsonType } from "./types";
2
+ import { htmlStringFormatting, cleanTags, cleanHtmlTags } from "./utils";
3
+ import { JSONToHTML, textToHTML, textToJSON } from "./parser";
4
+ declare function jsonRender(json: object | motdJsonType): string;
2
5
  /**
3
- * #### `cleanTags(string)`
4
- * Clean all tags from motd source string.
5
- */
6
- declare function cleanTags(text: string): string;
7
- /**
8
- * #### `textToHTML(string)`
9
- * Convert motd text to html.
10
- */
11
- declare function textToHTML(motdString: string): string;
12
- /**
13
- * #### `textToJSON(string)`
14
- * Convert motd text to JSON.
15
- */
16
- declare function parseTextToJSON(text: string): {
17
- text: string | number;
18
- extra: motdJsonType[];
19
- };
20
- /**
21
- * #### `JSONToString(string)`
22
- * Convert JSON to HTML.
23
- */
24
- declare function parseJSONToHTML(sourceJson: motdJsonType): string;
25
- declare function jsonRender(json: motdJsonType | object): string;
26
- /**
27
- * ### `autoToHtml(object | string)`
28
6
  * auto check data type then convert to html.
29
7
  */
30
- declare function autoToHtml(motd: motdJsonType | string | object): string;
31
- declare const defaultModules: {
32
- cleanTags: typeof cleanTags;
8
+ declare function autoToHTML(motd: string | object | motdJsonType): string;
9
+ declare const _default: {
33
10
  textToHTML: typeof textToHTML;
34
- textToJSON: typeof parseTextToJSON;
35
- JSONToHtml: typeof parseJSONToHTML;
11
+ textToJSON: typeof textToJSON;
12
+ JSONToHTML: typeof JSONToHTML;
36
13
  jsonRender: typeof jsonRender;
37
- autoToHtml: typeof autoToHtml;
14
+ autoToHTML: typeof autoToHTML;
15
+ htmlStringFormatting: typeof htmlStringFormatting;
16
+ cleanTags: typeof cleanTags;
17
+ cleanHtmlTags: typeof cleanHtmlTags;
38
18
  };
39
- export default defaultModules;
19
+ export default _default;
@@ -0,0 +1,7 @@
1
+ import { motdJsonType } from "../types";
2
+ /**
3
+ * Convert JSON to HTML.
4
+ *
5
+ * @param sourceJson
6
+ */
7
+ export default function parseJSONToHTML(sourceJson: motdJsonType): string;
@@ -0,0 +1,7 @@
1
+ import { motdJsonType } from "../types";
2
+ export declare function JSONRender(json: object | motdJsonType): string;
3
+ /**
4
+ * auto check data type then convert to html.
5
+ */
6
+ export declare function autoToHTML(motd: string | object | motdJsonType): string;
7
+ export default autoToHTML;
@@ -0,0 +1,5 @@
1
+ import JSONToHTML from "./JSONToHTML";
2
+ import textToHTML from "./textToHTML";
3
+ import textToJSON from "./textToJSON";
4
+ import { JSONRender, autoToHTML } from "./autoToHTML";
5
+ export { JSONToHTML, textToHTML, textToJSON, JSONRender, autoToHTML, };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Convert motd text to html.
3
+ * @param motdString
4
+ */
5
+ export default function textToHTML(motdString: string): string;
@@ -0,0 +1,10 @@
1
+ import { motdJsonType } from "../types";
2
+ /**
3
+ * Convert motd text to JSON.
4
+ *
5
+ * @param text
6
+ */
7
+ export default function parseTextToJSON(text: string): {
8
+ text: string | number;
9
+ extra: motdJsonType[];
10
+ };
@@ -0,0 +1,7 @@
1
+ import { extraLibraryType } from "./types";
2
+ declare const extras: extraLibraryType;
3
+ declare const extraFontStyles: extraLibraryType;
4
+ declare const textToJsonExtras: extraLibraryType;
5
+ declare const colorCodeToHex: extraLibraryType;
6
+ declare const extraColorsToHex: extraLibraryType;
7
+ export { extras, extraFontStyles, textToJsonExtras, colorCodeToHex, extraColorsToHex, };
package/types/types.d.ts CHANGED
@@ -11,8 +11,9 @@ interface motdJsonType {
11
11
  underlined?: boolean;
12
12
  obfuscated?: boolean;
13
13
  italic?: boolean;
14
- extra?: object[];
14
+ extra?: object[] | motdJsonType[];
15
15
  }[];
16
+ translate?: string | number;
16
17
  [key: string]: string | boolean | number | object | Array<object> | undefined;
17
18
  }
18
19
  export type { extraLibraryType, motdJsonType };
package/types/utils.d.ts CHANGED
@@ -2,6 +2,23 @@ import { motdJsonType } from "./types";
2
2
  export declare function isMotdJSONType(object: object): object is motdJsonType;
3
3
  /**
4
4
  *
5
- * #### replace all html tags to &...
5
+ * replace all html tags to &...
6
6
  */
7
- export declare const htmlStringFormatting: (text: string) => string;
7
+ export declare function htmlStringFormatting(text: string): string;
8
+ /**
9
+ * clean html tags
10
+ *
11
+ * @param text
12
+ * example: `<span>hello world</span>`
13
+ *
14
+ * result: `hello world`
15
+ */
16
+ export declare function cleanHtmlTags(text: string): string;
17
+ /**
18
+ * clean motd codes
19
+ *
20
+ * Clean all codes from motd source string.
21
+ *
22
+ * @param {string} text - motd string include tag § will remove
23
+ */
24
+ export declare function cleanCodes(text: string): string;