echogarden
Version:
An easy-to-use speech toolset. Includes tools for synthesis, recognition, alignment, speech translation, language detection, source separation and more.
40 lines (35 loc) • 800 B
text/typescript
export function escapeHtml(text: string) {
return text.replaceAll(
/[&<>"']/g,
(char) => htmlEscapeLookup[char]
)
}
const htmlEscapeLookup: Record<string, string> = {
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}
// This HTML unescape method is not fully standard compliant
// I implemented a fully compliant one in the package html-escape-compliant
function unescapeHtml(text: string) {
return text.replaceAll(
/&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160);/g,
(str) => htmlUnescapeLookup[str]
)
}
const htmlUnescapeLookup: Record<string, string> = {
'&': '&',
'&': '&',
'<': '<',
'<': '<',
'>': '>',
'>': '>',
''': "'",
''': "'",
'"': '"',
'"': '"',
' ': '\u00A0',
' ': '\u00A0'
}