fix(route): people charset decoding (#16734) (#16743)

This commit is contained in:
Cieons
2024-09-13 23:10:45 +08:00
committed by GitHub
parent 810aae3997
commit 487c0469c6
+6 -2
View File
@@ -32,8 +32,12 @@ async function handler(ctx) {
responseType: 'buffer',
});
const encoding = site === 'www' ? 'gbk' : 'utf-8';
const decodedResponse = iconv.decode(response, encoding);
// not seen Content-Type in response headers
// try to parse charset from meta tag
let decodedResponse = iconv.decode(response, 'utf-8');
const parsedCharset = decodedResponse.match(/<meta.*?charset=["']?([^"'>]+)["']?/i);
const encoding = parsedCharset ? parsedCharset[1].toLowerCase() : 'utf-8';
decodedResponse = encoding === 'utf-8' ? decodedResponse : iconv.decode(response, encoding);
const $ = load(decodedResponse);
$('em').remove();