Skip to content
Snippets Groups Projects
PlainNormalizer.js 1.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Igor Markin's avatar
    Igor Markin committed
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    
    Igor Markin's avatar
    Igor Markin committed
    exports.cleanupHiddenCharacters = exports.normalizePlainPart = void 0;
    
    Igor Markin's avatar
    Igor Markin committed
    // this is a Node module. require is a must to work across different envs
    const URL = require("url-parse");
    
    Igor Markin's avatar
    Igor Markin committed
    const utils_1 = require("../utils");
    const normalizePlainPart = (text) => {
    
    Igor Markin's avatar
    Igor Markin committed
        text = exports.cleanupHiddenCharacters(text);
    
    Igor Markin's avatar
    Igor Markin committed
        text = removeQRCodes(text);
    
    Igor Markin's avatar
    Igor Markin committed
        text = utils_1.normalizeTextSpacings(text);
    
    Igor Markin's avatar
    Igor Markin committed
        text = patchOutlookSafelinksWrappers(text);
    
    Igor Markin's avatar
    Igor Markin committed
        return text.trim();
    
    Igor Markin's avatar
    Igor Markin committed
    };
    exports.normalizePlainPart = normalizePlainPart;
    
    Igor Markin's avatar
    Igor Markin committed
    const patchOutlookSafelinksWrappers = (text) => {
        const links = text.match(/<https:.+?(safelinks\.protection\.outlook\.com).+?>/g);
        if (links) {
            links.forEach((link) => {
    
    Igor Markin's avatar
    Igor Markin committed
                const url = new URL(link.slice(1, link.length - 1), true);
                const originalUrl = url.query["url"];
    
    Igor Markin's avatar
    Igor Markin committed
                text = text.replace(link, `<${originalUrl}>`);
            });
        }
        return text;
    };
    
    Igor Markin's avatar
    Igor Markin committed
    /**
     * Function removes seal from the plain text.
     * Function has to support the next possible file names of the seal:
     * qrcode.png, qrcode-xxx.png, seal-xxx.png
     * @param s
     */
    
    Igor Markin's avatar
    Igor Markin committed
    const removeQRCodes = (s) => {
        return s
    
    Igor Markin's avatar
    Igor Markin committed
            .replace(/\[(image:\s)*(qrcode|seal).*?.png]\s*<https:\/\/.+(vereign\.com|vrgnservices\.com).*?>/g, "")
            .replace(/<https:\/\/.+(vereign\.com|vrgnservices\.com).*?>\s*\[(image: )*(qrcode|seal).*?.png]/g, "");
    
    Igor Markin's avatar
    Igor Markin committed
    };
    
    Igor Markin's avatar
    Igor Markin committed
    const cleanupHiddenCharacters = (s) => {
        const removeSymbols = new RegExp(/[\u200B]+/g);
        return s.replace(removeSymbols, "");
    };
    exports.cleanupHiddenCharacters = cleanupHiddenCharacters;