Skip to content

Commit

Permalink
Merge pull request #7 from SnowFireWolf/dev
Browse files Browse the repository at this point in the history
fix some types and build
  • Loading branch information
SnowFireWolf authored Jul 30, 2022
2 parents 340981b + c987afe commit ed6f23e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 40 deletions.
2 changes: 1 addition & 1 deletion dist/motdParser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
interface extraLibraryType {
[key: string]: string;
}

interface motdJsonType {
text: string | Number;
extra?: {
color?: string;
text?: string | Number;
bold?: boolean;
strikethrough?: boolean;
underlined?: boolean;
obfuscated?: boolean;
italic?: boolean;
extra?: object[];
}[];
[key: string]: string | boolean | object | Array<object> | undefined;
font?: string;
color?: string;
text?: string;
extra?: object[];
}

export type { extraLibraryType, motdJsonType };
1 change: 0 additions & 1 deletion dist/types.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sfirew/mc-motd-parser",
"description": "Minecraft Server MOTD Parser, can convert to html, json, text.",
"version": "1.0.9",
"version": "1.0.9-1",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "MIT",
Expand Down
43 changes: 22 additions & 21 deletions src/motdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { isMotdJSONType, htmlStringFormatting } from './utils';



// color code to font styles
const extras: extraLibraryType = {
'§k': 'obfuscated;',
'§l': 'font-weight: bold;',
Expand All @@ -21,6 +21,7 @@ const extras: extraLibraryType = {
'§r': 'color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;',
};

// json extra font styles
const extraFontStyles: extraLibraryType = {
'bold': 'font-weight: bold;',
'italic': 'font-style: italic;',
Expand All @@ -30,6 +31,7 @@ const extraFontStyles: extraLibraryType = {
'reset': 'color: inherit;text-decoration: none !important;font-weight:normal!important;font-style: normal!important;',
};

// text to json extra name
const textToJsonExtras: extraLibraryType = {
'§k': 'obfuscated',
'§l': 'bold',
Expand All @@ -46,6 +48,7 @@ const textToJsonExtras: extraLibraryType = {
'§P': ''
};

// base color hex
const colorCodeToHex: extraLibraryType = {
'§0': '#000000',
'§1': '#0000AA',
Expand All @@ -65,6 +68,7 @@ const colorCodeToHex: extraLibraryType = {
'§f': '#FFFFFF',
};

// json extra to hex color
const extraColorsToHex: extraLibraryType = {
'black': '#000000',
'dark_blue': '#0000AA',
Expand All @@ -86,9 +90,13 @@ const extraColorsToHex: extraLibraryType = {







// clean tags
/**
* ### `cleanTags(string)`
* #### `cleanTags(string)`
* Clean all tags from motd source string.
*/
function cleanTags(text: string) {
Expand All @@ -100,12 +108,9 @@ function cleanTags(text: string) {
return textResult
}




// text to html
/**
* ### `textToHTML(string)`
* #### `textToHTML(string)`
* Convert motd text to html.
*/
function textToHTML(motdString: string) {
Expand Down Expand Up @@ -149,6 +154,7 @@ function textToHTML(motdString: string) {
//console.log('color: ' + colorHex)
//console.log('text: ' + item)
//console.log('---------------------------------')
// replace html tags
textContent = htmlStringFormatting(textContent)

if (resultColor.length !== 0 || fontStyle.length !== 0) {
Expand All @@ -163,11 +169,9 @@ function textToHTML(motdString: string) {
return resultHTML
}



// text to json
/**
* ### `textToJSON(string)`
* #### `textToJSON(string)`
* Convert motd text to JSON.
*/
function parseTextToJSON(text: string) {
Expand Down Expand Up @@ -227,19 +231,21 @@ function parseTextToJSON(text: string) {
})

// console.log('resultObject', resultObject);

let newExtra: Array<motdJsonType> = [];
// if text is '', remote it and merge to next array
// if text is '', remove it and merge to next array
resultObject.extra && resultObject.extra.forEach((item, index) => {
// console.log('item', item);
if (item.text === '') {
if (resultObject.extra && typeof resultObject.extra[index + 1] === 'object') {
newExtra.push({
...item as any,
...item as motdJsonType,
...resultObject.extra[index + 1],
})
}
}
})
});
// remove blank content
newExtra = newExtra.filter(item => item.text !== '');
// console.log('newExtra', newExtra);

Expand All @@ -249,11 +255,9 @@ function parseTextToJSON(text: string) {
};
}



// json convert to html
/**
* ### `JSONToString(string)`
* #### `JSONToString(string)`
* Convert JSON to HTML.
*/
function parseJSONToHTML(sourceJson: motdJsonType) {
Expand Down Expand Up @@ -295,9 +299,7 @@ function parseJSONToHTML(sourceJson: motdJsonType) {
// text
if (key === "text") {
if (typeof sourceJson.text === 'string' || typeof sourceJson.text === 'number') {
//console.log(textToHtml(sourceJson.text))

// replace space to &nbsp; code
// convert all type to string
htmlElement += textToHTML(String(sourceJson.text));
continue;
}
Expand Down Expand Up @@ -353,10 +355,9 @@ function parseJSONToHTML(sourceJson: motdJsonType) {



// JSON 完整轉換 包含 換行等
// JSON full convert include newline
function jsonEnterRender(json: motdJsonType | object) {
// console.log('json', json);
// JSON.stringify(json).split('\\n').join("<br/>")
const resultMotdHtml = parseJSONToHTML(JSON.parse(JSON.stringify(json)));

//console.log('motd: ' + resultMotd)
Expand All @@ -365,7 +366,7 @@ function jsonEnterRender(json: motdJsonType | object) {



// TEXT 完整轉換 包含 換行等
// TEXT full convert include newline
function textEnterRender(text: string) {
const resultMotdHtml = textToHTML(text);

Expand Down
17 changes: 13 additions & 4 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
interface extraLibraryType {
[key: string]: string;
}

interface motdJsonType {
text: string | Number;
extra?: {
color?: string;
text?: string | Number;
bold?: boolean;
strikethrough?: boolean;
underlined?: boolean;
obfuscated?: boolean;
italic?: boolean;
extra?: object[];
}[];
[key: string]: string | boolean | object | Array<object> | undefined;
font?: string;
color?: string;
text?: string;
extra?: object[];
}

export type { extraLibraryType, motdJsonType };
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function isMotdJSONType(object: any): object is motdJsonType {
}


/**
*
* #### replace all html tags to &...
*/
export const htmlStringFormatting = (text: string): string => {
return text
// space
Expand All @@ -24,7 +28,7 @@ export const htmlStringFormatting = (text: string): string => {
.replace(/>/g, '&gt;')
.replace(/\"/g, '&quot;')
.replace(/\'/g, '&#39;')
// return
// return
.replace(/\n/g, '<br/>');
// .replace(/\//g, '&#x2F;');
}
14 changes: 7 additions & 7 deletions test/bugs.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { motdParser } from '../src';
import type { motdJsonType } from '../src/types';
const testFromCode = `&r&f &r&9&m&l &r&8&m&l[ &r&f &r&6&lMineplex&r&f &r&f&lGames&r&f &r&8&m&l ]&r&9&m&l &r&f
&r&e&lSTATS REVAMP&r &a&l&n testesest
`;
const replacedString = testFromCode.replace(/&/g, '§');

// console.log(motdParser.autoToHtml(replacedString));


// const testFromCode = `&r&f &r&9&m&l &r&8&m&l[ &r&f &r&6&lMineplex&r&f &r&f&lGames&r&f &r&8&m&l ]&r&9&m&l &r&f
// &r&e&lSTATS REVAMP&r &a&l&n testesest
// `;
// const replacedString = testFromCode.replace(/&/g, '§');

// console.log(motdParser.autoToHtml(replacedString));


const testFromJson: motdJsonType = {
const testNumberContentFromJson: motdJsonType = {
"extra":
[{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"green","text":"life boost: "},
{"italic":false,"color":"white","text":5}],
"text":""
}


console.log(motdParser.autoToHtml(testFromJson));
console.log(motdParser.autoToHtml(testNumberContentFromJson));

0 comments on commit ed6f23e

Please sign in to comment.