kramed.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * kramed - a markdown parser
  3. * Copyright (c) 2011-2014, Christopher Jeffrey. (MIT Licensed)
  4. * https://github.com/GitbookIO/kramed
  5. */
  6. /**
  7. * kramed - a kramdown parser, based off chjj's kramed
  8. * Copyright (c) 2014, Aaron O'Mullan. (MIT Licensed)
  9. * https://github.com/GitbookIO/kramed
  10. */
  11. var _utils = require('./utils');
  12. var merge = _utils.merge;
  13. var Lexer = require('./lex/block');
  14. var InlineLexer = require('./lex/inline');
  15. var Parser = require('./parser');
  16. var Renderer = require('./renderer');
  17. /**
  18. * Kramed
  19. */
  20. function kramed(src, opt, callback) {
  21. if (callback || typeof opt === 'function') {
  22. if (!callback) {
  23. callback = opt;
  24. opt = null;
  25. }
  26. opt = merge({}, kramed.defaults, opt || {});
  27. var highlight = opt.highlight
  28. , tokens
  29. , pending
  30. , i = 0;
  31. try {
  32. tokens = Lexer.lex(src, opt)
  33. } catch (e) {
  34. return callback(e);
  35. }
  36. pending = tokens.length;
  37. var done = function(err) {
  38. if (err) {
  39. opt.highlight = highlight;
  40. return callback(err);
  41. }
  42. var out;
  43. try {
  44. out = Parser.parse(tokens, opt);
  45. } catch (e) {
  46. err = e;
  47. }
  48. opt.highlight = highlight;
  49. return err
  50. ? callback(err)
  51. : callback(null, out);
  52. };
  53. if (!highlight || highlight.length < 3) {
  54. return done();
  55. }
  56. delete opt.highlight;
  57. if (!pending) return done();
  58. for (; i < tokens.length; i++) {
  59. (function(token) {
  60. if (token.type !== 'code') {
  61. return --pending || done();
  62. }
  63. return highlight(token.text, token.lang, function(err, code) {
  64. if (err) return done(err);
  65. if (code == null || code === token.text) {
  66. return --pending || done();
  67. }
  68. token.text = code;
  69. token.escaped = true;
  70. --pending || done();
  71. });
  72. })(tokens[i]);
  73. }
  74. return;
  75. }
  76. try {
  77. if (opt) opt = merge({}, kramed.defaults, opt);
  78. return Parser.parse(Lexer.lex(src, opt), opt);
  79. } catch (e) {
  80. e.message += '\nPlease report this to https://github.com/GitbookIO/kramed.';
  81. if ((opt || kramed.defaults).silent) {
  82. return '<p>An error occured:</p><pre>'
  83. + escape(e.message + '', true)
  84. + '</pre>';
  85. }
  86. throw e;
  87. }
  88. }
  89. /**
  90. * Options
  91. */
  92. kramed.options =
  93. kramed.setOptions = function(opt) {
  94. merge(kramed.defaults, opt);
  95. return kramed;
  96. };
  97. kramed.defaults = {
  98. // Lexer options (both block and inline lexers)
  99. gfm: true,
  100. tables: true,
  101. breaks: false,
  102. pedantic: false,
  103. sanitize: false,
  104. smartLists: false,
  105. mathjax: true,
  106. // Kramed options
  107. silent: false,
  108. highlight: null,
  109. // Renderer options
  110. langPrefix: 'lang-',
  111. smartypants: false,
  112. headerPrefix: '',
  113. headerAutoId: true,
  114. xhtml: false,
  115. // Default rendrer passed to Parser
  116. renderer: new Renderer,
  117. };
  118. /**
  119. * Expose
  120. */
  121. kramed.Parser = Parser;
  122. kramed.parser = Parser.parse;
  123. kramed.Renderer = Renderer;
  124. kramed.Lexer = Lexer;
  125. kramed.lexer = Lexer.lex;
  126. kramed.InlineLexer = InlineLexer;
  127. kramed.inlineLexer = InlineLexer.output;
  128. kramed.parse = kramed;
  129. module.exports = kramed;