Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
- interface FormatterPatternAndReplacement {
- pattern: RegExp;
- replacement: string;
- }
- @Injectable()
- export class Formatter {
- // define your characters
- static asCode = new RegExp(/`(.*?)`/g);
- static asPre = new RegExp(/`{3}(.*?)`{3}/);
- static asBold = new RegExp(/\*(.*?)\*/g);
- static asItalic = new RegExp(/_(.*?)_/g);
- static asStrikeThrough = new RegExp(/~(.*?)~/g);
- static allSupportedMatchers = new RegExp(/`(.*?)`|\*(.*?)\*|_(.*?)_|~(.*?)~/g);
- static formatters: FormatterPatternAndReplacement[] = [
- {pattern: Formatter.asItalic, replacement: 'i'},
- {pattern: Formatter.asCode, replacement: 'code'},
- {pattern: Formatter.asBold, replacement: 'b'},
- {pattern: Formatter.asStrikeThrough, replacement: 'del'}
- ];
- constructor(protected sanitizer: DomSanitizer) {}
- stripFormattersAndReturnHTML(textToFormat: string, tag): string {
- return `<${tag}>${textToFormat}</${tag}>`;
- }
- sanitizedOutput(formattedText: string): SafeHtml {
- return this.sanitizer.bypassSecurityTrustHtml(formattedText);
- }
- processword(word: string): string {
- return Formatter.formatters.reduce(
- (word, formatter) => word.replace(formatter.pattern, (match, enclosed) => {
- return `<${formatter.replacement}>${enclosed}</${formatter.replacement}>`;
- })
- , word);
- }
- formatText(text: string): SafeHtml {
- if (text.indexOf('```') > -1) {
- if (text.replace('```', '').indexOf('```') > -1) {
- const decoded = text.replace(Formatter.asPre, (match, enclosed) => enclosed)
- .replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
- .replace('```', '').replace('```', '');
- return this.sanitizedOutput(`<pre>${decoded}</pre>`);
- }
- }
- return this.sanitizedOutput(this.processword(text));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement