Move newline handling to MessageBodyBuilder

- Also add a insert method to reflect new design.

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-05-10 21:37:27 +05:30
parent 787d438a74
commit 55f2d79d07

View File

@ -1,9 +1,28 @@
import { linkify } from "./linkify.js";
export class MessageBodyBuilder {
constructor(message = []) {
this._root = message;
}
fromText(text) {
const components = text.split("\n");
components.slice(0, -1).forEach(t => {
linkify(t, this.insert.bind(this));
});
const [last] = components.slice(-1);
linkify(last, this.insert.bind(this));
}
insert(text, isLink) {
if (!text.length) return;
if (isLink)
this.insertLink(text, text);
else
this.insertText(text);
}
insertText(text) {
if (text.length)
this._root.push({ type: "text", text: text });