showdown.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Created by Tivie on 06-01-2015.
  3. */
  4. // Private properties
  5. var showdown = {},
  6. parsers = {},
  7. extensions = {},
  8. defaultOptions = {
  9. omitExtraWLInCodeBlocks: false,
  10. prefixHeaderId: false,
  11. noHeaderId: false
  12. },
  13. globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
  14. /**
  15. * helper namespace
  16. * @type {{}}
  17. */
  18. showdown.helper = {};
  19. /**
  20. * TODO LEGACY SUPPORT CODE
  21. * @type {{}}
  22. */
  23. showdown.extensions = {};
  24. /**
  25. * Set a global option
  26. * @static
  27. * @param {string} key
  28. * @param {*} value
  29. * @returns {showdown}
  30. */
  31. showdown.setOption = function (key, value) {
  32. 'use strict';
  33. globalOptions[key] = value;
  34. return this;
  35. };
  36. /**
  37. * Get a global option
  38. * @static
  39. * @param {string} key
  40. * @returns {*}
  41. */
  42. showdown.getOption = function (key) {
  43. 'use strict';
  44. return globalOptions[key];
  45. };
  46. /**
  47. * Get the global options
  48. * @static
  49. * @returns {{}}
  50. */
  51. showdown.getOptions = function () {
  52. 'use strict';
  53. return globalOptions;
  54. };
  55. /**
  56. * Reset global options to the default values
  57. * @static
  58. */
  59. showdown.resetOptions = function () {
  60. 'use strict';
  61. globalOptions = JSON.parse(JSON.stringify(defaultOptions));
  62. };
  63. /**
  64. * Get the default options
  65. * @static
  66. * @returns {{}}
  67. */
  68. showdown.getDefaultOptions = function () {
  69. 'use strict';
  70. return defaultOptions;
  71. };
  72. /**
  73. * Get or set a subParser
  74. *
  75. * subParser(name) - Get a registered subParser
  76. * subParser(name, func) - Register a subParser
  77. * @static
  78. * @param {string} name
  79. * @param {function} [func]
  80. * @returns {*}
  81. */
  82. showdown.subParser = function (name, func) {
  83. 'use strict';
  84. if (showdown.helper.isString(name)) {
  85. if (typeof func !== 'undefined') {
  86. parsers[name] = func;
  87. } else {
  88. if (parsers.hasOwnProperty(name)) {
  89. return parsers[name];
  90. } else {
  91. throw Error('SubParser named ' + name + ' not registered!');
  92. }
  93. }
  94. }
  95. };
  96. /**
  97. * Gets or registers an extension
  98. * @static
  99. * @param {string} name
  100. * @param {object|function=} ext
  101. * @returns {*}
  102. */
  103. showdown.extension = function (name, ext) {
  104. 'use strict';
  105. if (!showdown.helper.isString(name)) {
  106. throw Error('Extension \'name\' must be a string');
  107. }
  108. name = showdown.helper.stdExtName(name);
  109. // Getter
  110. if (showdown.helper.isUndefined(ext)) {
  111. if (!extensions.hasOwnProperty(name)) {
  112. throw Error('Extension named ' + name + ' is not registered!');
  113. }
  114. return extensions[name];
  115. // Setter
  116. } else {
  117. // Expand extension if it's wrapped in a function
  118. if (typeof ext === 'function') {
  119. ext = ext();
  120. }
  121. // Ensure extension is an array
  122. if (!showdown.helper.isArray(ext)) {
  123. ext = [ext];
  124. }
  125. var validExtension = validate(ext, name);
  126. if (validExtension.valid) {
  127. extensions[name] = ext;
  128. } else {
  129. throw Error(validExtension.error);
  130. }
  131. }
  132. };
  133. /**
  134. * Gets all extensions registered
  135. * @returns {{}}
  136. */
  137. showdown.getAllExtensions = function () {
  138. 'use strict';
  139. return extensions;
  140. };
  141. /**
  142. * Remove an extension
  143. * @param {string} name
  144. */
  145. showdown.removeExtension = function (name) {
  146. 'use strict';
  147. delete extensions[name];
  148. };
  149. /**
  150. * Removes all extensions
  151. */
  152. showdown.resetExtensions = function () {
  153. 'use strict';
  154. extensions = {};
  155. };
  156. /**
  157. * Validate extension
  158. * @param {array} extension
  159. * @param {string} name
  160. * @returns {{valid: boolean, error: string}}
  161. */
  162. function validate(extension, name) {
  163. 'use strict';
  164. var errMsg = (name) ? 'Error in ' + name + ' extension->' : 'Error in unnamed extension',
  165. ret = {
  166. valid: true,
  167. error: ''
  168. };
  169. if (!showdown.helper.isArray(extension)) {
  170. extension = [extension];
  171. }
  172. for (var i = 0; i < extension.length; ++i) {
  173. var baseMsg = errMsg + 'sub-extension ' + i + ': ',
  174. ext = extension[i];
  175. if (typeof ext !== 'object') {
  176. ret.valid = false;
  177. ret.error = baseMsg + 'must be an object, but ' + typeof ext + ' given';
  178. return ret;
  179. }
  180. if (!showdown.helper.isString(ext.type)) {
  181. ret.valid = false;
  182. ret.error = baseMsg + 'property "type" must be a string, but ' + typeof ext.type + ' given';
  183. return ret;
  184. }
  185. var type = ext.type = ext.type.toLowerCase();
  186. // normalize extension type
  187. if (type === 'language') {
  188. type = ext.type = 'lang';
  189. }
  190. if (type === 'html') {
  191. type = ext.type = 'output';
  192. }
  193. if (type !== 'lang' && type !== 'output') {
  194. ret.valid = false;
  195. ret.error = baseMsg + 'type ' + type + ' is not recognized. Valid values: "lang" or "output"';
  196. return ret;
  197. }
  198. if (ext.filter) {
  199. if (typeof ext.filter !== 'function') {
  200. ret.valid = false;
  201. ret.error = baseMsg + '"filter" must be a function, but ' + typeof ext.filter + ' given';
  202. return ret;
  203. }
  204. } else if (ext.regex) {
  205. if (showdown.helper.isString(ext.regex)) {
  206. ext.regex = new RegExp(ext.regex, 'g');
  207. }
  208. if (!ext.regex instanceof RegExp) {
  209. ret.valid = false;
  210. ret.error = baseMsg + '"regex" property must either be a string or a RegExp object, but ' +
  211. typeof ext.regex + ' given';
  212. return ret;
  213. }
  214. if (showdown.helper.isUndefined(ext.replace)) {
  215. ret.valid = false;
  216. ret.error = baseMsg + '"regex" extensions must implement a replace string or function';
  217. return ret;
  218. }
  219. } else {
  220. ret.valid = false;
  221. ret.error = baseMsg + 'extensions must define either a "regex" property or a "filter" method';
  222. return ret;
  223. }
  224. if (showdown.helper.isUndefined(ext.filter) && showdown.helper.isUndefined(ext.regex)) {
  225. ret.valid = false;
  226. ret.error = baseMsg + 'output extensions must define a filter property';
  227. return ret;
  228. }
  229. }
  230. return ret;
  231. }
  232. /**
  233. * Validate extension
  234. * @param {object} ext
  235. * @returns {boolean}
  236. */
  237. showdown.validateExtension = function (ext) {
  238. 'use strict';
  239. var validateExtension = validate(ext, null);
  240. if (!validateExtension.valid) {
  241. console.warn(validateExtension.error);
  242. return false;
  243. }
  244. return true;
  245. };