helpers.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * showdownjs helper functions
  3. */
  4. if (!showdown.hasOwnProperty('helper')) {
  5. showdown.helper = {};
  6. }
  7. /**
  8. * Check if var is string
  9. * @static
  10. * @param {string} a
  11. * @returns {boolean}
  12. */
  13. showdown.helper.isString = function isString(a) {
  14. 'use strict';
  15. return (typeof a === 'string' || a instanceof String);
  16. };
  17. /**
  18. * ForEach helper function
  19. * @static
  20. * @param {*} obj
  21. * @param {function} callback
  22. */
  23. showdown.helper.forEach = function forEach(obj, callback) {
  24. 'use strict';
  25. if (typeof obj.forEach === 'function') {
  26. obj.forEach(callback);
  27. } else {
  28. for (var i = 0; i < obj.length; i++) {
  29. callback(obj[i], i, obj);
  30. }
  31. }
  32. };
  33. /**
  34. * isArray helper function
  35. * @static
  36. * @param {*} a
  37. * @returns {boolean}
  38. */
  39. showdown.helper.isArray = function isArray(a) {
  40. 'use strict';
  41. return a.constructor === Array;
  42. };
  43. /**
  44. * Check if value is undefined
  45. * @static
  46. * @param {*} value The value to check.
  47. * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`.
  48. */
  49. showdown.helper.isUndefined = function isUndefined(value) {
  50. 'use strict';
  51. return typeof value === 'undefined';
  52. };
  53. /**
  54. * Standardidize extension name
  55. * @static
  56. * @param {string} s extension name
  57. * @returns {string}
  58. */
  59. showdown.helper.stdExtName = function (s) {
  60. 'use strict';
  61. return s.replace(/[_-]||\s/g, '').toLowerCase();
  62. };
  63. function escapeCharactersCallback(wholeMatch, m1) {
  64. 'use strict';
  65. var charCodeToEscape = m1.charCodeAt(0);
  66. return '~E' + charCodeToEscape + 'E';
  67. }
  68. /**
  69. * Callback used to escape characters when passing through String.replace
  70. * @static
  71. * @param {string} wholeMatch
  72. * @param {string} m1
  73. * @returns {string}
  74. */
  75. showdown.helper.escapeCharactersCallback = escapeCharactersCallback;
  76. /**
  77. * Escape characters in a string
  78. * @static
  79. * @param {string} text
  80. * @param {string} charsToEscape
  81. * @param {boolean} afterBackslash
  82. * @returns {XML|string|void|*}
  83. */
  84. showdown.helper.escapeCharacters = function escapeCharacters(text, charsToEscape, afterBackslash) {
  85. 'use strict';
  86. // First we have to escape the escape characters so that
  87. // we can build a character class out of them
  88. var regexString = '([' + charsToEscape.replace(/([\[\]\\])/g, '\\$1') + '])';
  89. if (afterBackslash) {
  90. regexString = '\\\\' + regexString;
  91. }
  92. var regex = new RegExp(regexString, 'g');
  93. text = text.replace(regex, escapeCharactersCallback);
  94. return text;
  95. };
  96. /**
  97. * POLYFILLS
  98. */
  99. if (showdown.helper.isUndefined(console)) {
  100. console = {
  101. warn: function (msg) {
  102. 'use strict';
  103. alert(msg);
  104. },
  105. log: function (msg) {
  106. 'use strict';
  107. alert(msg);
  108. }
  109. };
  110. }