@@ -36,21 +36,49 @@ type DuckDuckGoResult = {
|
36 | 36 | }; |
37 | 37 | |
38 | 38 | function decodeHtmlEntities(text: string): string { |
39 | | -return text |
40 | | -.replace(/&/g, "&") |
41 | | -.replace(/</g, "<") |
42 | | -.replace(/>/g, ">") |
43 | | -.replace(/"/g, '"') |
44 | | -.replace(/'/g, "'") |
45 | | -.replace(/'/g, "'") |
46 | | -.replace(/'/g, "'") |
47 | | -.replace(///g, "/") |
48 | | -.replace(/ /g, " ") |
49 | | -.replace(/–/g, "-") |
50 | | -.replace(/—/g, "--") |
51 | | -.replace(/…/g, "...") |
52 | | -.replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(Number(code))) |
53 | | -.replace(/&#x([0-9a-f]+);/gi, (_, code) => String.fromCodePoint(Number.parseInt(code, 16))); |
| 39 | +return text.replace( |
| 40 | +/&(?:lt|gt|quot|apos|#39|#x27|#x2F|nbsp|ndash|mdash|hellip|amp|#\d+|#x[0-9a-f]+);/gi, |
| 41 | +(entity) => { |
| 42 | +const normalized = entity.toLowerCase(); |
| 43 | +if (normalized === "<") { |
| 44 | +return "<"; |
| 45 | +} |
| 46 | +if (normalized === ">") { |
| 47 | +return ">"; |
| 48 | +} |
| 49 | +if (normalized === """) { |
| 50 | +return '"'; |
| 51 | +} |
| 52 | +if (normalized === "'" || normalized === "'" || normalized === "'") { |
| 53 | +return "'"; |
| 54 | +} |
| 55 | +if (normalized === "/") { |
| 56 | +return "/"; |
| 57 | +} |
| 58 | +if (normalized === " ") { |
| 59 | +return " "; |
| 60 | +} |
| 61 | +if (normalized === "–") { |
| 62 | +return "-"; |
| 63 | +} |
| 64 | +if (normalized === "—") { |
| 65 | +return "--"; |
| 66 | +} |
| 67 | +if (normalized === "…") { |
| 68 | +return "..."; |
| 69 | +} |
| 70 | +if (normalized === "&") { |
| 71 | +return "&"; |
| 72 | +} |
| 73 | +if (normalized.startsWith("&#x")) { |
| 74 | +return String.fromCodePoint(Number.parseInt(normalized.slice(3, -1), 16)); |
| 75 | +} |
| 76 | +if (normalized.startsWith("&#")) { |
| 77 | +return String.fromCodePoint(Number.parseInt(normalized.slice(2, -1), 10)); |
| 78 | +} |
| 79 | +return entity; |
| 80 | +}, |
| 81 | +); |
54 | 82 | } |
55 | 83 | |
56 | 84 | function stripHtml(html: string): string { |
|