From fea233fa75faed4eec5564f811562cb6eda3c089 Mon Sep 17 00:00:00 2001 From: igor <igor.markin@vereign.com> Date: Mon, 6 Sep 2021 14:55:04 +0300 Subject: [PATCH] Add option to normalize image plain --- src/PlainNormalizer/PlainNormalizer.ts | 29 +++++++++++++++++--------- src/PlainNormalizer/index.ts | 3 ++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/PlainNormalizer/PlainNormalizer.ts b/src/PlainNormalizer/PlainNormalizer.ts index d9bf613..b8fa492 100644 --- a/src/PlainNormalizer/PlainNormalizer.ts +++ b/src/PlainNormalizer/PlainNormalizer.ts @@ -3,23 +3,32 @@ const URL = require("url-parse"); import { normalizeTextSpacings } from "../utils"; -export interface SealRemovalOptions { - sealUrl: string; +export interface Options { + sealUrl?: string; + normalizeImagePlain?: boolean; } -export const normalizePlainPart = ( - text: string, - sealRemovalOptions?: SealRemovalOptions -): string => { +const defaultOptions: Options = { + normalizeImagePlain: true, +}; + +export const normalizePlainPart = (text: string, options?: Options): string => { + options = options + ? Object.assign(defaultOptions, { ...options }) + : defaultOptions; + text = cleanupHiddenCharacters(text); text = patchOutlookSafelinksWrappers(text); - if (sealRemovalOptions) { - text = removeSeal(text, sealRemovalOptions.sealUrl); + if (options?.sealUrl) { + text = removeSeal(text, options.sealUrl); + } + + if (options?.normalizeImagePlain) { + text = cleanupImageText(text); } - text = cleanupImageText(text); text = normalizeTextSpacings(text); return text.trim(); @@ -58,7 +67,7 @@ const removeSeal = (plain: string, sealUrl: string): string => { .replace(new RegExp(sealRegexReversed), ""); }; -const cleanupImageText = (plain: string): string => { +export const cleanupImageText = (plain: string): string => { // For cases [image: IMAGE_NAME.EXTENSION] const sealRegex = `\\[(image:\\s)(.*)\\]`; diff --git a/src/PlainNormalizer/index.ts b/src/PlainNormalizer/index.ts index 0822822..21891a5 100644 --- a/src/PlainNormalizer/index.ts +++ b/src/PlainNormalizer/index.ts @@ -1,5 +1,6 @@ -import { normalizePlainPart } from "./PlainNormalizer"; +import { normalizePlainPart, cleanupImageText } from "./PlainNormalizer"; export default { normalizePlain: normalizePlainPart, + cleanupImageText: cleanupImageText, }; -- GitLab