How to unescape HTML entities in strings?

I want to unescape HTML entities like ' in response of sub-request, but I found HTMLRewriter doesn’t unescape them (HTMRewriter never decodes HTML entities).

And since workers’ runtime don’t have DOMParser, is there any way to do that?

Had a crack at this!

$ curl -X POST -H "Content-Type: application/json" -d '{"text": "Foo © bar 𝌆 baz ☃ qux"}'
Foo © bar 𝌆 baz ☃ qux

I got this working using the html-entities NPM package, works really well, just an encode and decode function.

Worker code is:

import {decode} from 'html-entities';

export async function handleRequest(request: Request): Promise<Response> {
  let data = await request.json();
  return new Response(decode(data["text"]), { headers: { "Content-Type": "text/plain" }});

Feel free to play with that worker, I’ll keep it up for a while :smile:


Works for me, thanks for amazingly fast reply!

1 Like

No problem!