Text: bounded CommonMark-subset Markdown parser and safe HTML renderer

Add Void/Text/Markdown.hpp: a bounded CommonMark-subset parser with a safe
HTML renderer. Raw HTML is always escaped, link/image destinations are
allow-listed (http, https, mailto or relative), and unsafe schemes such as
javascript: and data: are dropped, including entity-smuggled variants.
Add html_escape to Void/Text/Html.hpp.

Closes #58
This commit is contained in:
faumaray committed 2026-12-04 13:29:25 +03:00
1 parent 1f98938061
commit 549c7cfccb
3 files changed
+1459

No files matched your search

+20
View File
@@ -42,6 +42,26 @@ inline constexpr std::array<NamedEntity, 12> named_entities{{
}};
} // namespace Detail
/// Escape `s` for safe interpolation into HTML text or a quoted attribute:
/// `&` -> `&amp;`, `<` -> `&lt;`, `>` -> `&gt;`, `"` -> `&quot;`, `'` -> `&#39;`.
/// Everything else is passed through byte-for-byte.
[[nodiscard]] inline std::string html_escape(std::string_view s)
{
std::string out;
out.reserve(s.size());
for (const char c : s) {
switch (c) {
case '&': out += "&amp;"; break;
case '<': out += "&lt;"; break;
case '>': out += "&gt;"; break;
case '"': out += "&quot;"; break;
case '\'': out += "&#39;"; break;
default: out += c; break;
}
}
return out;
}
/// Decode HTML character references in `s` to UTF-8: numeric (`&#1234;`,
/// `&#x1F600;`) and the common named entities (&amp; &lt; &gt; &quot; &apos;
/// &nbsp; &copy; …). Unknown or malformed references are left verbatim.
File diff suppressed because it is too large. Load diff