-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simplify processors and split instances for each source type
- Loading branch information
Showing
5 changed files
with
96 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,36 @@ | ||
/** Function to process html string for normalization*/ | ||
export type UIPNormalizationProcessor = ((input: string) => string) & { | ||
htmlOnly?: boolean; | ||
}; | ||
import {UIPPreprocessorService} from './preprocessor'; | ||
|
||
export class UIPNormalizationService { | ||
/** Processor storage */ | ||
protected static processors: Record<string, UIPNormalizationProcessor> = {}; | ||
/** | ||
* Normalization processors store for JS content. | ||
* Normalization transformation are used to normalize content before displaying or processing. | ||
*/ | ||
export const UIPJSNormalizationPreprocessors = new UIPPreprocessorService(); | ||
/** | ||
* Normalization processors store for HTML content. | ||
* Normalization transformation are used to normalize content before displaying or processing. | ||
*/ | ||
export const UIPHTMLNormalizationPreprocessors = new UIPPreprocessorService(); | ||
|
||
/** Add processor function to normalization process */ | ||
public static addProcessor( | ||
name: string, | ||
processor: UIPNormalizationProcessor, | ||
htmlOnly = false | ||
): void { | ||
Object.assign(processor, {htmlOnly}); | ||
UIPNormalizationService.processors[name] = processor; | ||
} | ||
|
||
/** Normalizes passes content string by running all registered processors in chain */ | ||
public static normalize(content: string, isHtml = true): string { | ||
return Object.keys(UIPNormalizationService.processors) | ||
.map((name) => UIPNormalizationService.processors[name]) | ||
.filter((processor) => typeof processor === 'function') | ||
.filter((processor) => !processor.htmlOnly || isHtml) | ||
.reduce((input, processor) => processor(input), content); | ||
} | ||
/** Removes extra indents to beautify content alignment */ | ||
export function removeIndent(input: string): string { | ||
// Get all indents from text | ||
const indents = input.match(/^[^\S\n\r]*(?=\S)/gm); | ||
// No processing if no indent on the first line or input is empty | ||
if (!indents || !indents[0].length) return input; | ||
// Sort indents by length | ||
indents.sort((a, b) => a.length - b.length); | ||
// No processing if minimal indent is 0 | ||
if (!indents[0].length) return input; | ||
// Remove indents from text | ||
return input.replace(RegExp('^' + indents[0], 'gm'), ''); | ||
} | ||
UIPJSNormalizationPreprocessors.add('remove-leading-indent', removeIndent); | ||
UIPHTMLNormalizationPreprocessors.add('remove-leading-indent', removeIndent); | ||
|
||
/** Removes extra indents */ | ||
UIPNormalizationService.addProcessor( | ||
'remove-indent', | ||
(input: string): string => { | ||
// Get all indents from text | ||
const indents = input.match(/^[^\S\n\r]*(?=\S)/gm); | ||
// No processing if no indent on the first line or input is empty | ||
if (!indents || !indents[0].length) return input; | ||
// Sort indents by length | ||
indents.sort((a, b) => a.length - b.length); | ||
// No processing if minimal indent is 0 | ||
if (!indents[0].length) return input; | ||
// Remove indents from text | ||
return input.replace(RegExp('^' + indents[0], 'gm'), ''); | ||
} | ||
); | ||
/** Removes extra spaces */ | ||
// TODO: handle case with inline script with literal double spaces | ||
UIPNormalizationService.addProcessor( | ||
'remove-trailing', | ||
(input: string) => input.replace(/\s*?$/gm, ''), | ||
true | ||
); | ||
/** Remove beginning spaces */ | ||
UIPNormalizationService.addProcessor('left-trim', (input: string) => | ||
input.replace(/^\s+/, '') | ||
); | ||
/** Remove ending spaces */ | ||
UIPNormalizationService.addProcessor('right-trim', (input: string) => | ||
input.replace(/\s+$/, '') | ||
); | ||
/** Trim content */ | ||
UIPJSNormalizationPreprocessors.add('trim', (content: string) => content.trim()); | ||
UIPHTMLNormalizationPreprocessors.add('trim', (content: string) => content.trim()); | ||
|
||
/** Removes extra spaces inside the content. Applicable for HTML only */ | ||
UIPHTMLNormalizationPreprocessors.addRegexReplacer('remove-trailing', /\s*?$/gm, ''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** Pre-processor function, called to process content */ | ||
export type UIPTransformer = (input: string) => string; | ||
|
||
/** Rendering pre-processor service, that stores collection of {@link UIPRenderingPreprocessor}s */ | ||
export class UIPPreprocessorService { | ||
/** Pre-processor storage */ | ||
protected preprocessors: Record<string, UIPTransformer> = {}; | ||
|
||
/** Add pre-processor {@link UIPRenderingPreprocessor} */ | ||
public add(name: string, preprocessor: UIPTransformer): void { | ||
this.preprocessors[name] = preprocessor; | ||
} | ||
|
||
/** Add pre-processor alias */ | ||
public addAlias(name: string, alias: string): void { | ||
this.preprocessors[name] = this.preprocessors[alias]; | ||
} | ||
|
||
/** Add pre-processor with RegExp replacer */ | ||
public addRegexReplacer(name: string, regex: RegExp, replaceValue: string): void; | ||
/** Add pre-processor with RegExp replacer */ | ||
public addRegexReplacer(name: string, regex: RegExp, replacer: (substring: string, ...args: any[]) => string): void; | ||
public addRegexReplacer(name: string, regex: RegExp, replacer: any): void { | ||
this.add(name, (input) => input.replace(regex, replacer)); | ||
} | ||
|
||
public get(name: string): UIPTransformer | undefined { | ||
return this.preprocessors[name]; | ||
} | ||
|
||
/** Pre-process html content */ | ||
public preprocess(html: string): string { | ||
return Object.keys(this.preprocessors) | ||
.map((name) => this.preprocessors[name]) | ||
.filter((preprocessor) => typeof preprocessor === 'function') | ||
.reduce((input, preprocessor) => preprocessor(input), html); | ||
} | ||
|
||
/** Clear all pre-processors */ | ||
public clear(): void { | ||
this.preprocessors = {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,25 @@ | ||
import {ESLRandomText} from '@exadel/esl/modules/esl-random-text/core'; | ||
|
||
/** Rendering pre-processor function, called before rendering, does not affect the content in plugins */ | ||
export type UIPRenderingPreprocessor = (input: string) => string; | ||
|
||
/** Rendering pre-processor service, that stores collection of {@link UIPRenderingPreprocessor}s */ | ||
export class UIPRenderingPreprocessorService { | ||
protected static preprocessors: Record<string, UIPRenderingPreprocessor> = {}; | ||
|
||
/** Add pre-processor {@link UIPRenderingPreprocessor} */ | ||
public static addPreprocessor(name: string, preprocessor: UIPRenderingPreprocessor): void { | ||
UIPRenderingPreprocessorService.preprocessors[name] = preprocessor; | ||
} | ||
|
||
/** Add pre-processor with RegExp replacer */ | ||
public static addRegexPreprocessor(name: string, regex: RegExp, replacer: Parameters<typeof String.prototype.replace>[1]): void { | ||
this.addPreprocessor(name, (input) => input.replace(regex, replacer)); | ||
} | ||
|
||
/** Add pre-processor alias */ | ||
public static addPreprocessorAlias(name: string, alias: string): void { | ||
UIPRenderingPreprocessorService.preprocessors[name] = UIPRenderingPreprocessorService.preprocessors[alias]; | ||
} | ||
|
||
/** Pre-process html content */ | ||
public static preprocess(html: string): string { | ||
return Object.keys(UIPRenderingPreprocessorService.preprocessors) | ||
.map((name) => UIPRenderingPreprocessorService.preprocessors[name]) | ||
.filter((preprocessor) => typeof preprocessor === 'function') | ||
.reduce((input, preprocessor) => preprocessor(input), html); | ||
} | ||
|
||
/** Clear all pre-processors */ | ||
public static clear(): void { | ||
UIPRenderingPreprocessorService.preprocessors = {}; | ||
} | ||
} | ||
import {UIPPreprocessorService} from './preprocessor'; | ||
|
||
/** | ||
* Pre-processor services for JS content. | ||
* Rendering preprocessors applied to content before rendering and does not affect editor's content | ||
*/ | ||
export const UIPJSRenderingPreprocessors = new UIPPreprocessorService(); | ||
/** | ||
* Pre-processor services for HTML content. | ||
* Rendering preprocessors applied to content before rendering and does not affect editor's content | ||
*/ | ||
export const UIPHTMLRenderingPreprocessors = new UIPPreprocessorService(); | ||
|
||
// Register default pre-processors | ||
UIPRenderingPreprocessorService.addRegexPreprocessor('text', /<!--\s*text\s*x?(\d+)?\s*-->/g, (term, count) => { | ||
|
||
UIPHTMLRenderingPreprocessors.addRegexReplacer('text', /<!--\s*text\s*x?(\d+)?\s*-->/g, (term, count) => { | ||
const length = count ? parseInt(count, 10) : 10; | ||
return ESLRandomText.generateText(length); | ||
}); | ||
|
||
UIPRenderingPreprocessorService.addRegexPreprocessor('text-html', /<!--\s*(text-html|paragraph)\s*x?(\d+)?\s*-->/g, (term, name, count) => { | ||
UIPHTMLRenderingPreprocessors.addRegexReplacer('text-html', /<!--\s*(text-html|paragraph)\s*x?(\d+)?\s*-->/g, (term, name, count) => { | ||
const length = count ? parseInt(count, 10) : 100; | ||
return ESLRandomText.generateTextHTML(100 * length); | ||
}); |