Skip to content
Snippets Groups Projects
PlainNormalizer.ts 1.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • // this is a Node module. require is a must to work across different envs
    const URL = require("url-parse");
    
    
    import { removeSpacesAndLinebreaks } from "../utils";
    
    export const normalizePlainPart = (text: string): string => {
    
    Gospodin Bodurov's avatar
    Gospodin Bodurov committed
      text = removeListBullets(text);
    
      text = removeSpacesAndLinebreaks(text);
    
      text = removeQRCodes(text);
    
      text = patchOutlookSafelinksWrappers(text);
    
      return text;
    
    const patchOutlookSafelinksWrappers = (text: string) => {
      const links = text.match(
        /<https:.+?(safelinks\.protection\.outlook\.com).+?>/g
      );
    
      if (links) {
        links.forEach((link) => {
    
          const url = new URL(link.slice(1, link.length - 1), true);
          const originalUrl = url.query["url"];
    
          text = text.replace(link, `<${originalUrl}>`);
        });
      }
    
      return text;
    };
    
    
    const removeQRCodes = (s: string): string => {
      return s
    
        .replace(/\[(image:)*qrcode.png]\s*<https:\/\/.+?>/g, "")
        .replace(/<https:\/\/.+?>\s*\[(image: )*qrcode.png]/g, "");
    
    Gospodin Bodurov's avatar
    Gospodin Bodurov committed
    
    const removeListBullets = (s: string): string => {
    
      return s.replace("\n[o§]\n+/g", "");
    
    Gospodin Bodurov's avatar
    Gospodin Bodurov committed
    };