UNPKG

sanitize-latex

Version:

A string sanitizer for LaTeX symbols.

35 lines (33 loc) 967 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sanitize = void 0; /** * Sanitizes a string by replacing all hazardous LaTeX symbols with their descriptive names. * * @param source The string to be sanitized. * @return A sanitized version of the string with LaTeX symbols normalized. */ function sanitize(source) { var symbolMap = { "'": '\\textquotesingle{}', '"': '\\textquotedbl{}', '`': '\\textasciigrave{}', '^': '\\textasciicircum{}', '~': '\\textasciitilde{}', '<': '\\textless{}', '>': '\\textgreater{}', '|': '\\textbar{}', '\\': '\\textbackslash{}', '{': '\\{', '}': '\\}', $: '\\$', '&': '\\&', '#': '\\#', _: '\\_', '%': '\\%' }; return Array.from(source) .map(function (char) { return symbolMap[char] || char; }) .join(''); } exports.sanitize = sanitize;