diff options
author | LawnCable <lawncable.tmw2@simonlaux.de> | 2018-05-24 16:17:50 -0400 |
---|---|---|
committer | LawnCable <lawncable.tmw2@simonlaux.de> | 2018-05-24 16:17:50 -0400 |
commit | d81bc993b8e4acc63af8cf0030d391c4befd77fb (patch) | |
tree | e9409a520759f55dd7373481b16df34a44a17505 /src/renderer/gameserver/news.ts | |
parent | e48328aac858537871c29953d9f4f2316e0c39dd (diff) | |
download | electron-d81bc993b8e4acc63af8cf0030d391c4befd77fb.tar.gz electron-d81bc993b8e4acc63af8cf0030d391c4befd77fb.tar.bz2 electron-d81bc993b8e4acc63af8cf0030d391c4befd77fb.tar.xz electron-d81bc993b8e4acc63af8cf0030d391c4befd77fb.zip |
added basic support to have the latest news entry on the info page
Diffstat (limited to 'src/renderer/gameserver/news.ts')
-rw-r--r-- | src/renderer/gameserver/news.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/renderer/gameserver/news.ts b/src/renderer/gameserver/news.ts new file mode 100644 index 0000000..b8c7b14 --- /dev/null +++ b/src/renderer/gameserver/news.ts @@ -0,0 +1,54 @@ +export enum NewsType { + Markdown, // Without html + yaml + ManaPlus //update news txt +} + +export namespace News { // Fetches only the most recent entry + export async function get(url:string,parser:NewsType):Promise<string>{ + try { + //1. load + const unsafe_content = await request(url); + //2. sanitize + const content = killHTML( unsafe_content ); + //3. parse + if(parser == NewsType.ManaPlus){ + return manaTextParser(content); + } else if (parser == NewsType.Markdown){ + return "Parsing Failed: Markdown Parser is not implemented yet"; + } + } catch (e){ + console.log(e); + return `Failed to get the news, please select the news category on the right to view all news`; + } + } +} + +function request(url:string):Promise<string>{ + return new Promise((res, rej)=>{ + var xhr = new XMLHttpRequest(); + xhr.open('GET', url); + xhr.onload = function() { + if (xhr.status === 200) { + res(xhr.responseText); + } + else { + rej(new Error(`xhr.status: ${xhr.status} != 200`)); + } + }; + xhr.send(); + }); +} + +function killHTML(raw:string):string{ + return raw.replace(/<|>/,"⚠️"); +} + +function manaTextParser(input:string){ + return input + .replace(/\[@@(.+?)\|(.+?)@@\]/g,'<a href="$1">$2</a>') + .replace(/##B(.+?)##b/g,'<b>$1</b>') + .replace(/##0 Actual Release: ##1 *(.+)/,'<h2>$1</h2>') + .replace(/\n/g,"<br>") + .replace(/br>([^]+?)<br></g,'br><p>$1</p><br><').replace(/<br>/g,"") + .replace(/##\d/g,""); +} |