feat(parser): Text#toHTML (#300)

Added support to render Text nodes as HTML for use in web applications.
This commit is contained in:
Daniel Wykerd
2023-02-01 21:27:59 +02:00
committed by GitHub
parent f62c66db39
commit e82e23dfbb
4 changed files with 62 additions and 2 deletions

View File

@@ -1,6 +1,21 @@
import TextRun from './TextRun';
import EmojiRun from './EmojiRun';
export interface Run {
text: string;
toString(): string;
toHTML(): string;
}
export function escape(text: string) {
return text
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}
class Text {
text: string;
runs;
@@ -17,6 +32,10 @@ class Text {
}
}
toHTML() {
return this.runs ? this.runs.map((run) => run.toHTML()).join('') : this.text;
}
toString() {
return this.text;
}