transform @room to AtRoomPill while deserializing html to md

pull/21833/head
Bruno Windels 2019-06-14 18:25:02 +02:00
parent 63b11f5001
commit 65d56d1490
1 changed files with 20 additions and 7 deletions

View File

@ -20,6 +20,21 @@ import { walkDOMDepthFirst } from "./dom";
const REGEX_MATRIXTO = new RegExp(MATRIXTO_URL_PATTERN); const REGEX_MATRIXTO = new RegExp(MATRIXTO_URL_PATTERN);
function parseAtRoomMentions(text, partCreator) {
const ATROOM = "@room";
const parts = [];
text.split(ATROOM).forEach((textPart, i, arr) => {
if (textPart.length) {
parts.push(partCreator.plain(textPart));
}
const isLast = i === arr.length - 1;
if (!isLast) {
parts.push(partCreator.atRoomPill(ATROOM));
}
});
return parts;
}
function parseLink(a, partCreator) { function parseLink(a, partCreator) {
const {href} = a; const {href} = a;
const pillMatch = REGEX_MATRIXTO.exec(href) || []; const pillMatch = REGEX_MATRIXTO.exec(href) || [];
@ -158,7 +173,7 @@ function parseHtmlMessage(html, partCreator) {
} }
if (n.nodeType === Node.TEXT_NODE) { if (n.nodeType === Node.TEXT_NODE) {
newParts.push(partCreator.plain(n.nodeValue)); newParts.push(...parseAtRoomMentions(n.nodeValue, partCreator));
} else if (n.nodeType === Node.ELEMENT_NODE) { } else if (n.nodeType === Node.ELEMENT_NODE) {
const parseResult = parseElement(n, partCreator); const parseResult = parseElement(n, partCreator);
if (parseResult) { if (parseResult) {
@ -210,13 +225,11 @@ export function parseEvent(event, partCreator) {
const lines = body.split("\n"); const lines = body.split("\n");
parts = lines.reduce((parts, line, i) => { parts = lines.reduce((parts, line, i) => {
const isLast = i === lines.length - 1; const isLast = i === lines.length - 1;
const text = partCreator.plain(line); const newParts = parseAtRoomMentions(line, partCreator);
const newLine = !isLast && partCreator.newline(); if (!isLast) {
if (newLine) { newParts.push(partCreator.newline());
return parts.concat(text, newLine);
} else {
return parts.concat(text);
} }
return parts.concat(newParts);
}, []); }, []);
} }
if (content.msgtype === "m.emote") { if (content.msgtype === "m.emote") {