index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. // Map the characters to escape to their escaped values. The list is derived
  3. // from http://www.cespedes.org/blog/85/how-to-escape-latex-special-characters
  4. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  5. var defaultEscapes = {
  6. "{": "\\{",
  7. "}": "\\}",
  8. "\\": "\\textbackslash{}",
  9. "#": "\\#",
  10. $: "\\$",
  11. "%": "\\%",
  12. "&": "\\&",
  13. "^": "\\textasciicircum{}",
  14. _: "\\_",
  15. "~": "\\textasciitilde{}"
  16. };
  17. var formatEscapes = {
  18. "\u2013": "\\--",
  19. "\u2014": "\\---",
  20. " ": "~",
  21. "\t": "\\qquad{}",
  22. "\r\n": "\\newline{}",
  23. "\n": "\\newline{}"
  24. };
  25. var defaultEscapeMapFn = function defaultEscapeMapFn(defaultEscapes, formatEscapes) {
  26. return _extends({}, defaultEscapes, formatEscapes);
  27. };
  28. /**
  29. * Escape a string to be used in LaTeX documents.
  30. * @param {string} str the string to be escaped.
  31. * @param {boolean} params.preserveFormatting whether formatting escapes should
  32. * be performed (default: false).
  33. * @param {function} params.escapeMapFn the function to modify the escape maps.
  34. * @return {string} the escaped string, ready to be used in LaTeX.
  35. */
  36. module.exports = function (str) {
  37. var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  38. _ref$preserveFormatti = _ref.preserveFormatting,
  39. preserveFormatting = _ref$preserveFormatti === undefined ? false : _ref$preserveFormatti,
  40. _ref$escapeMapFn = _ref.escapeMapFn,
  41. escapeMapFn = _ref$escapeMapFn === undefined ? defaultEscapeMapFn : _ref$escapeMapFn;
  42. var runningStr = String(str);
  43. var result = "";
  44. var escapes = escapeMapFn(_extends({}, defaultEscapes), preserveFormatting ? _extends({}, formatEscapes) : {});
  45. var escapeKeys = Object.keys(escapes); // as it is reused later on
  46. // Algorithm: Go through the string character by character, if it matches
  47. // with one of the special characters then we'll replace it with the escaped
  48. // version.
  49. var _loop = function _loop() {
  50. var specialCharFound = false;
  51. escapeKeys.forEach(function (key, index) {
  52. if (specialCharFound) {
  53. return;
  54. }
  55. if (runningStr.length >= key.length && runningStr.slice(0, key.length) === key) {
  56. result += escapes[escapeKeys[index]];
  57. runningStr = runningStr.slice(key.length, runningStr.length);
  58. specialCharFound = true;
  59. }
  60. });
  61. if (!specialCharFound) {
  62. result += runningStr.slice(0, 1);
  63. runningStr = runningStr.slice(1, runningStr.length);
  64. }
  65. };
  66. while (runningStr) {
  67. _loop();
  68. }
  69. return result;
  70. };