hackmd-to-html-cli
Version:
A node.js CLI tool for converting HackMD markdown to HTML.
26 lines (23 loc) • 663 B
text/typescript
// modified from:
// https://github.com/markdown-it/markdown-it/blob/0fe7ccb4b7f30236fb05f623be6924961d296d3d/lib/common/utils.mjs
const HTML_ESCAPE_TEST_RE = /[&<>"]/
const HTML_ESCAPE_REPLACE_RE = /[&<>"]/g
function replaceUnsafeChar(ch: string): string {
switch (ch) {
case '&':
return '&'
case '<':
return '<'
case '>':
return '>'
case '"':
return '"'
}
return ""
}
export function escapeHtml(str: string): string {
if (HTML_ESCAPE_TEST_RE.test(str)) {
return str.replace(HTML_ESCAPE_REPLACE_RE, replaceUnsafeChar)
}
return str
}