Last active
August 31, 2019 15:33
-
-
Save oneEyedSunday/798028669174625f58b6030932350440 to your computer and use it in GitHub Desktop.
Describes how to use the basic formatter
This file contains hidden or 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
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)); | |
} |
This file contains hidden or 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
constructor(public _formatter: Formatter) {} |
This file contains hidden or 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
[innerHTML]="_formatter.formatText('text content to format')" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment