mdown.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /** @license
  2. * RequireJS plugin for loading Markdown files and converting them into HTML.
  3. * Author: Miller Medeiros
  4. * Version: 0.1.1 (2012/02/17)
  5. * Released under the MIT license
  6. */
  7. // NOTE :: if you don't need to load markdown files in production outside of
  8. // the build, precompile them into modules and set
  9. // `pragmasOnSave.excludeMdown=true`
  10. define(
  11. [
  12. //>>excludeStart('excludeMdown', pragmas.excludeMdown)
  13. 'text',
  14. 'markdownConverter'
  15. //>>excludeEnd('excludeMdown')
  16. ],
  17. function (
  18. //>>excludeStart('excludeMdown', pragmas.excludeMdown)
  19. text, markdownConverter
  20. //>>excludeEnd('excludeMdown')
  21. ) {
  22. //>>excludeStart('excludeMdown', pragmas.excludeMdown)
  23. var buildMap = {};
  24. //>>excludeEnd('excludeMdown')
  25. //API
  26. return {
  27. load : function(name, req, onLoad, config) {
  28. //>>excludeStart('excludeMdown', pragmas.excludeMdown)
  29. text.get(req.toUrl(name), function(data){
  30. data = markdownConverter.makeHtml(data);
  31. if (config.isBuild) {
  32. buildMap[name] = data;
  33. onLoad(data);
  34. } else {
  35. onLoad(data);
  36. }
  37. });
  38. },
  39. //write method based on RequireJS official text plugin by James Burke
  40. //https://github.com/jrburke/requirejs/blob/master/text.js
  41. write : function(pluginName, moduleName, write){
  42. if(moduleName in buildMap){
  43. var content = text.jsEscape(buildMap[moduleName]);
  44. write.asModule(pluginName + "!" + moduleName,
  45. "define(function () { return '" +
  46. content +
  47. "';});\n");
  48. }
  49. //>>excludeEnd('excludeMdown')
  50. }
  51. };
  52. });