-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutils.ts
More file actions
37 lines (36 loc) · 881 Bytes
/
utils.ts
File metadata and controls
37 lines (36 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Decodes HTML entities in a string.
* This handles common HTML entities found in WordPress Plugin Check output.
*
* @param text - The text containing HTML entities
* @returns The decoded text
*/
export function decodeHtmlEntities(text: string): string {
const namedEntities: Record<string, string> = {
'"': '"',
''': "'",
'&': '&',
'<': '<',
'>': '>',
' ': ' ',
};
return text.replace(
/&(?:#x([0-9a-fA-F]+)|#(\d+)|([a-zA-Z]+));/g,
(match, hex, dec, named) => {
if (hex) {
// Hexadecimal entity
return String.fromCharCode(parseInt(hex, 16));
}
if (dec) {
// Decimal entity
return String.fromCharCode(parseInt(dec, 10));
}
if (named && namedEntities[`&${named};`]) {
// Named entity
return namedEntities[`&${named};`];
}
// Unknown entity, return as-is
return match;
},
);
}