showdown.js 6.6 KB

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