Add a flag to strip replies

This commit is contained in:
Danila Fedorin 2021-08-04 15:31:25 -07:00
parent 06961ff693
commit b5f16468ce
2 changed files with 15 additions and 6 deletions

View file

@ -34,7 +34,8 @@ const baseUrl = 'https://matrix.to';
const linkPrefix = `${baseUrl}/#/`; const linkPrefix = `${baseUrl}/#/`;
class Deserializer { class Deserializer {
constructor(result, mediaRepository) { constructor(result, mediaRepository, allowReplies) {
this.allowReplies = allowReplies;
this.result = result; this.result = result;
this.mediaRepository = mediaRepository; this.mediaRepository = mediaRepository;
} }
@ -287,6 +288,10 @@ class Deserializer {
return true; return true;
} }
_isAllowedNode(node) {
return this.allowReplies || !this._ensureElement(node, "MX-REPLY");
}
_parseInlineNodes(nodes, into) { _parseInlineNodes(nodes, into) {
for (const htmlNode of nodes) { for (const htmlNode of nodes) {
if (this._parseTextParts(htmlNode, into)) { if (this._parseTextParts(htmlNode, into)) {
@ -301,9 +306,11 @@ class Deserializer {
} }
// Node is either block or unrecognized. In // Node is either block or unrecognized. In
// both cases, just move on to its children. // both cases, just move on to its children.
if (this._isAllowedNode(htmlNode)) {
this._parseInlineNodes(this.result.getChildNodes(htmlNode), into); this._parseInlineNodes(this.result.getChildNodes(htmlNode), into);
} }
} }
}
parseInlineNodes(nodes) { parseInlineNodes(nodes) {
const into = []; const into = [];
@ -325,9 +332,11 @@ class Deserializer {
continue; continue;
} }
// Node is unrecognized. Just move on to its children. // Node is unrecognized. Just move on to its children.
if (this._isAllowedNode(htmlNode)) {
this._parseAnyNodes(this.result.getChildNodes(htmlNode), into); this._parseAnyNodes(this.result.getChildNodes(htmlNode), into);
} }
} }
}
parseAnyNodes(nodes) { parseAnyNodes(nodes) {
const into = []; const into = [];
@ -336,9 +345,9 @@ class Deserializer {
} }
} }
export function parseHTMLBody(platform, mediaRepository, html) { export function parseHTMLBody(platform, mediaRepository, allowReplies, html) {
const parseResult = platform.parseHTML(html); const parseResult = platform.parseHTML(html);
const deserializer = new Deserializer(parseResult, mediaRepository); const deserializer = new Deserializer(parseResult, mediaRepository, allowReplies);
const parts = deserializer.parseAnyNodes(parseResult.rootNodes); const parts = deserializer.parseAnyNodes(parseResult.rootNodes);
return new MessageBody(html, parts); return new MessageBody(html, parts);
} }

View file

@ -54,7 +54,7 @@ export class TextTile extends BaseTextTile {
_parseBody(body, format) { _parseBody(body, format) {
if (format === BodyFormat.Html) { if (format === BodyFormat.Html) {
return parseHTMLBody(this.platform, this._mediaRepository, body); return parseHTMLBody(this.platform, this._mediaRepository, this._entry.isReply, body);
} else { } else {
return parsePlainBody(body); return parsePlainBody(body);
} }