Newer
Older
// this is a Node module. require is a must to work across different envs
const URL = require("url-parse");
import { normalizeTextSpacings } from "../utils";
export interface SealRemovalOptions {
sealUrl: string;
}
export const normalizePlainPart = (
text: string,
sealRemovalOptions?: SealRemovalOptions
): string => {
text = patchOutlookSafelinksWrappers(text);
if (sealRemovalOptions) {
text = removeSeal(text, sealRemovalOptions.sealUrl);
}

Alexey Lunin
committed
text = cleanupImageText(text);
text = normalizeTextSpacings(text);
return text.trim();
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 removeSeal = (plain: string, sealUrl: string): string => {
// For cases [<image-alt>]<<seal-url>>
const sealRegex = `\\[.+?]\\s*<${sealUrl}>`;
// For cases <<seal-url>>[<image-alt>]
const sealRegexReversed = `<${sealUrl}>\\s*\\[.+?]`;
return plain
.replace(new RegExp(sealRegex), "")
.replace(new RegExp(sealRegexReversed), "");

Alexey Lunin
committed
const cleanupImageText = (plain: string): string => {
// For cases [image: IMAGE_NAME.EXTENSION]
const sealRegex = `\\[(image:\\s)(.*)\\]`;
return plain.replace(new RegExp(sealRegex), "[$2]");
};
export const cleanupHiddenCharacters = (s: string): string => {
const removeSymbols = new RegExp(/[\u200B]+/g);
return s.replace(removeSymbols, "");
};