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:
parent
787d438a74
commit
55f2d79d07
1 changed files with 19 additions and 0 deletions
|
@ -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 });
|
||||
|
|
Reference in a new issue