Improve typing

This commit is contained in:
Michael Telatynski 2021-10-12 11:01:40 +01:00
parent 193a060ec9
commit fe70e64a40

View file

@ -20,7 +20,7 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { walkDOMDepthFirst } from "./dom"; import { walkDOMDepthFirst } from "./dom";
import { checkBlockNode } from "../HtmlUtils"; import { checkBlockNode } from "../HtmlUtils";
import { getPrimaryPermalinkEntity } from "../utils/permalinks/Permalinks"; import { getPrimaryPermalinkEntity } from "../utils/permalinks/Permalinks";
import { PartCreator, Type } from "./parts"; import { Part, PartCreator, Type } from "./parts";
import SdkConfig from "../SdkConfig"; import SdkConfig from "../SdkConfig";
function parseAtRoomMentions(text: string, partCreator: PartCreator) { function parseAtRoomMentions(text: string, partCreator: PartCreator) {
@ -213,12 +213,12 @@ function prefixQuoteLines(isFirstNode, parts, partCreator) {
} }
} }
function parseHtmlMessage(html: string, partCreator: PartCreator, isQuotedMessage: boolean) { function parseHtmlMessage(html: string, partCreator: PartCreator, isQuotedMessage: boolean): Part[] {
// no nodes from parsing here should be inserted in the document, // no nodes from parsing here should be inserted in the document,
// as scripts in event handlers, etc would be executed then. // as scripts in event handlers, etc would be executed then.
// we're only taking text, so that is fine // we're only taking text, so that is fine
const rootNode = new DOMParser().parseFromString(html, "text/html").body; const rootNode = new DOMParser().parseFromString(html, "text/html").body;
const parts = []; const parts: Part[] = [];
let lastNode; let lastNode;
let inQuote = isQuotedMessage; let inQuote = isQuotedMessage;
const state: IState = { const state: IState = {
@ -233,7 +233,7 @@ function parseHtmlMessage(html: string, partCreator: PartCreator, isQuotedMessag
inQuote = true; inQuote = true;
} }
const newParts = []; const newParts: Part[] = [];
if (lastNode && (checkBlockNode(lastNode) || checkBlockNode(n))) { if (lastNode && (checkBlockNode(lastNode) || checkBlockNode(n))) {
newParts.push(partCreator.newline()); newParts.push(partCreator.newline());
} }
@ -288,7 +288,7 @@ function parseHtmlMessage(html: string, partCreator: PartCreator, isQuotedMessag
return parts; return parts;
} }
export function parsePlainTextMessage(body: string, partCreator: PartCreator, isQuotedMessage?: boolean) { export function parsePlainTextMessage(body: string, partCreator: PartCreator, isQuotedMessage?: boolean): Part[] {
const lines = body.split(/\r\n|\r|\n/g); // split on any new-line combination not just \n, collapses \r\n const lines = body.split(/\r\n|\r|\n/g); // split on any new-line combination not just \n, collapses \r\n
return lines.reduce((parts, line, i) => { return lines.reduce((parts, line, i) => {
if (isQuotedMessage) { if (isQuotedMessage) {
@ -300,7 +300,7 @@ export function parsePlainTextMessage(body: string, partCreator: PartCreator, is
parts.push(partCreator.newline()); parts.push(partCreator.newline());
} }
return parts; return parts;
}, []); }, [] as Part[]);
} }
export function parseEvent(event: MatrixEvent, partCreator: PartCreator, { isQuotedMessage = false } = {}) { export function parseEvent(event: MatrixEvent, partCreator: PartCreator, { isQuotedMessage = false } = {}) {