Skip to content

Commit

Permalink
Merge pull request #270 from Jers1k/master
Browse files Browse the repository at this point in the history
Added four new helpers for discord2telegram processing
  • Loading branch information
Lulalaby committed May 4, 2023
2 parents 1af6f7c + 147ea8b commit a9f006b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/discord2telegram/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,52 @@ export const escapeHTMLSpecialChars = R.compose(
R.replace(/</g, "&lt;"),
R.replace(/&/g, "&amp;")
);

/**
* Filters custom emojis from the output
*
* @param input The string that needs to be filtered
*
* @returns Filtered string
*/
export function customEmojiFilter(input: string){
const regex = /\&lt;[^;]*\&gt;/gi;
return input.split(regex).join('');
}

/**
* Replaces @ with # to prevent unneeded references in Telegram
*
* @param input The string that needs to be processed
*
* @returns Processed string
*/
export function replaceAtWithHash(input: string){
const regex = /\@/g;
return input.replace(regex, '#');
}

/**
* Replaces excessive (two or more) whitespaces with a single one
*
* @param input The string that needs to be processed
*
* @returns Processed string
*/
export function replaceExcessiveSpaces(input: string){
const regex = /[^\S\n]{2,}/g;
return input.replace(regex, ' ');
}

/**
* Removes whitespaces if they're placed at the beginning of the newline
*
* @param input The string that needs to be processed
*
* @returns Processed string
*/
export function removeNewlineSpaces(input: string){
const regex = /^[^\S\n]*/gm;
return input.replace(regex, '');
}

17 changes: 15 additions & 2 deletions src/discord2telegram/md2html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import simpleMarkdown, { SingleASTNode } from "simple-markdown";
import { escapeHTMLSpecialChars } from "./helpers";
import { escapeHTMLSpecialChars, customEmojiFilter, replaceAtWithHash, replaceExcessiveSpaces, removeNewlineSpaces } from "./helpers";
import R from "ramda";

/***********
Expand Down Expand Up @@ -119,5 +119,18 @@ export function md2html(text: string) {
return html + `${tags.start}${extractText(node)}${tags.end}`;
}, "");

return html;
// Making a couple of formatting adjustments
function htmlCleanup(input: string){
// Removing custom emojis from the HTML
input = customEmojiFilter(input)
// Replacing @ character with # character to prevent unintentional references in Telegram
input = replaceAtWithHash(input)
// Replacing excessive whitespaces with a single space (tends to be an issue after custom emoji filtering)
input = replaceExcessiveSpaces(input)
// Removing whitespaces if they're placed on the beginning of the newline (tends to be an issue after custom emoji filtering)
input = removeNewlineSpaces(input)
return input;
}

return htmlCleanup(html);
}

0 comments on commit a9f006b

Please sign in to comment.