git » sdk » commit 5c5334c

Helper to get plain text from HTML

author Stephen Paul Weber
2026-04-10 02:45:13 UTC
committer Stephen Paul Weber
2026-04-10 02:45:13 UTC
parent e3be43705cd582f5220cd36abcf8390b2b5d43d4

Helper to get plain text from HTML

Rendering some basic formatting to sigils that won't look bonkers to a human.

borogove/ChatMessageBuilder.hx +1 -2
borogove/Html.hx +10 -0

diff --git a/borogove/ChatMessageBuilder.hx b/borogove/ChatMessageBuilder.hx
index 016c491..ac3e730 100644
--- a/borogove/ChatMessageBuilder.hx
+++ b/borogove/ChatMessageBuilder.hx
@@ -16,7 +16,6 @@ import borogove.Hash;
 import borogove.JID;
 import borogove.Identicon;
 import borogove.StringUtil;
-import borogove.XEP0393;
 import borogove.EmojiUtil;
 import borogove.Message;
 import borogove.Stanza;
@@ -283,7 +282,7 @@ class ChatMessageBuilder {
 		final htmlIdx = payloads.findIndex((p) -> p.attr.get("xmlns") == "http://jabber.org/protocol/xhtml-im" && p.name == "html");
 		if (htmlIdx >= 0) payloads.splice(htmlIdx, 1);
 		payloads.push(htmlEl);
-		text = ~/\n$/.replace(XEP0393.render(body), "");
+		text = html.toPlainText();
 	}
 
 	/**
diff --git a/borogove/Html.hx b/borogove/Html.hx
index 8fca3e6..e90f403 100644
--- a/borogove/Html.hx
+++ b/borogove/Html.hx
@@ -214,6 +214,16 @@ class Html {
 		}).join("");
 	}
 
+	/**
+		Get plain text suitable for showing to a user
+	**/
+	public function toPlainText(): String {
+		// Could use reduce, but we already have XEP0393.render around
+		final body = new Stanza("body");
+		body.addChildNodes(xml);
+		return ~/\n$/.replace(XEP0393.render(body), "");
+	}
+
 	#if js
 	/**
 		Get HTML as a DocumentFragment