2021-05-17 11:26:30 +02:00
|
|
|
import { linkify } from "./linkify/linkify.js";
|
|
|
|
|
2021-05-17 17:09:23 +02:00
|
|
|
/**
|
|
|
|
* Parse text into parts such as newline, links and text.
|
|
|
|
* @param {string} body A string to parse into parts
|
|
|
|
* @returns {MessageBody} Parsed result
|
|
|
|
*/
|
2021-05-17 11:26:30 +02:00
|
|
|
export function parsePlainBody(body) {
|
|
|
|
const parts = [];
|
|
|
|
const lines = body.split("\n");
|
|
|
|
|
|
|
|
// create callback outside of loop
|
|
|
|
const linkifyCallback = (text, isLink) => {
|
|
|
|
if (isLink) {
|
|
|
|
parts.push(new LinkPart(text, text));
|
|
|
|
} else {
|
|
|
|
parts.push(new TextPart(text));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i = 0; i < lines.length; i += 1) {
|
|
|
|
const line = lines[i];
|
|
|
|
if (line.length) {
|
2021-05-18 10:56:56 +02:00
|
|
|
linkify(line, linkifyCallback);
|
2021-05-17 11:26:30 +02:00
|
|
|
}
|
|
|
|
const isLastLine = i >= (lines.length - 1);
|
|
|
|
if (!isLastLine) {
|
|
|
|
parts.push(new NewLinePart());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MessageBody(body, parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stringAsBody(body) {
|
|
|
|
return new MessageBody(body, [new TextPart(body)]);
|
|
|
|
}
|
|
|
|
|
2021-07-01 09:19:01 +02:00
|
|
|
class HeaderBlock {
|
|
|
|
constructor(level, inlines) {
|
|
|
|
this.level = level;
|
|
|
|
this.inlines = inlines;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() { return "header"; }
|
|
|
|
isBlock() { return true; }
|
|
|
|
}
|
|
|
|
|
|
|
|
class CodeBlock {
|
|
|
|
constructor(text) {
|
|
|
|
this.text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() { return "codeblock"; }
|
|
|
|
isBlock() { return true; }
|
|
|
|
}
|
|
|
|
|
2021-05-17 11:26:30 +02:00
|
|
|
class NewLinePart {
|
|
|
|
get type() { return "newline"; }
|
2021-07-01 09:19:01 +02:00
|
|
|
isBlock() { return false; }
|
|
|
|
}
|
|
|
|
|
|
|
|
class EmphPart {
|
|
|
|
constructor(wraps) {
|
|
|
|
this.wraps = wraps;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() { return "emph"; }
|
|
|
|
isBlock() { return false; }
|
|
|
|
}
|
|
|
|
|
|
|
|
class CodePart {
|
2021-07-01 09:44:41 +02:00
|
|
|
constructor(text) {
|
|
|
|
this.text = text;
|
2021-07-01 09:19:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get type() { return "code"; }
|
|
|
|
isBlock() { return false; }
|
2021-05-17 11:26:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class LinkPart {
|
|
|
|
constructor(url, text) {
|
|
|
|
this.url = url;
|
|
|
|
this.text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() { return "link"; }
|
2021-07-01 09:19:01 +02:00
|
|
|
isBlock() { return false; }
|
2021-05-17 11:26:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class TextPart {
|
|
|
|
constructor(text) {
|
|
|
|
this.text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() { return "text"; }
|
2021-07-01 09:19:01 +02:00
|
|
|
isBlock() { return false; }
|
2021-05-17 11:26:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class MessageBody {
|
|
|
|
constructor(sourceString, parts) {
|
2021-05-17 12:45:55 +02:00
|
|
|
this.sourceString = sourceString;
|
|
|
|
this.parts = parts;
|
2021-05-17 11:26:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tests() {
|
|
|
|
|
|
|
|
function test(assert, input, output) {
|
|
|
|
assert.deepEqual(parsePlainBody(input), new MessageBody(input, output));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
// Tests for text
|
|
|
|
"Text only": assert => {
|
|
|
|
const input = "This is a sentence";
|
|
|
|
const output = [new TextPart(input)];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
|
|
|
|
|
|
|
"Text with newline": assert => {
|
|
|
|
const input = "This is a sentence.\nThis is another sentence.";
|
|
|
|
const output = [
|
|
|
|
new TextPart("This is a sentence."),
|
|
|
|
new NewLinePart(),
|
|
|
|
new TextPart("This is another sentence.")
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
},
|
|
|
|
|
|
|
|
"Text with newline & trailing newline": assert => {
|
|
|
|
const input = "This is a sentence.\nThis is another sentence.\n";
|
|
|
|
const output = [
|
|
|
|
new TextPart("This is a sentence."),
|
|
|
|
new NewLinePart(),
|
|
|
|
new TextPart("This is another sentence."),
|
|
|
|
new NewLinePart()
|
|
|
|
];
|
|
|
|
test(assert, input, output);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|