markdown.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * compile markdown to html
  3. */
  4. var marked = require('marked'),
  5. path = require('path'),
  6. highlightjs = require('highlight.js');
  7. module.exports = function(grunt) {
  8. function capitalize (str) {
  9. var words = str.split('-');
  10. return words.map(function(value){
  11. return value.charAt(0).toUpperCase() + value.slice(1);
  12. }).join('');
  13. }
  14. function preCompile(src, basename) {
  15. var exampleCount = 1;
  16. var match = src.match(/<example>[\s\S]+?<\/example>/mg);
  17. if(match && match.length > 0) {
  18. match.forEach(function(example) {
  19. var subMatch = example.match(/<file\s*name=".+?">[\s\S]+?<\/file>/mg);
  20. var group, newExample = example,
  21. htmlContent,
  22. appName = capitalize(basename) + 'Example' + exampleCount + 'App',
  23. folderName = basename + '-example-' + exampleCount,
  24. htmlFileName,
  25. jsTemplate,
  26. jsFileName;
  27. if(subMatch && subMatch.length > 0) {
  28. for( var i = 0; i < subMatch.length; i++) {
  29. group = subMatch[i].match(/<file\s*name="(.+?)">\n*([\s\S]+?)\n*<\/file>/m);
  30. var fileName = group[1],
  31. code = group[2],
  32. lang, codeWrapped;
  33. if(path.extname(fileName) === '.html') {
  34. lang = 'html';
  35. htmlFileName = fileName;
  36. htmlContent = code;
  37. } else if(path.extname(fileName) === '.js') {
  38. lang = 'javascript';
  39. jsFileName = fileName;
  40. jsTemplate = grunt.template.process(grunt.file.read('docs/src/script-template'), {
  41. data: {
  42. appName: appName,
  43. jsContent: code
  44. }
  45. });
  46. grunt.file.write(path.join( 'docs/dist/examples/' + folderName, fileName), jsTemplate);
  47. }
  48. codeWrapped = marked('\n```'+lang+'\n'+code + '\n```\n', {
  49. highlight: function(code){
  50. return highlightjs.highlightAuto(code).value;
  51. }
  52. });
  53. newExample = newExample.replace(code, codeWrapped);
  54. }
  55. var htmlTemplate = grunt.file.read('docs/src/example-template.html');
  56. console.log({
  57. appName: appName,
  58. jsFile: jsFileName,
  59. templateHTML: htmlContent
  60. });
  61. htmlTemplate = grunt.template.process(htmlTemplate, {
  62. data:{
  63. appName: appName,
  64. jsFile: jsFileName,
  65. templateHTML: htmlContent
  66. }
  67. });
  68. grunt.file.write(path.join('docs/dist/examples/' + folderName, htmlFileName), htmlTemplate);
  69. }
  70. newExample = newExample +
  71. '\n\n<iframe class="runnable-example-frame" src="examples/' + folderName +'/' + htmlFileName + '"></iframe>';
  72. src = src.replace(example, newExample);
  73. exampleCount++;
  74. });
  75. }
  76. console.log(src);
  77. return src;
  78. }
  79. function postCompile(src) {
  80. // prevent {{ }} being parsed by angular
  81. var match = src.match(/<pre>[\s\S]+?<\/pre>/mg);
  82. if(match && match.length > 0) {
  83. match.forEach(function(str) {
  84. var subMatch = str.match(/{{[\s\S]+?}}/mg);
  85. var group, newStr = str;
  86. if(subMatch && subMatch.length > 0) {
  87. for( var i = 0; i < subMatch.length; i++) {
  88. group = subMatch[i].match(/{{([\s\S]+?)}}/m);
  89. newStr = newStr.replace(subMatch[i], '<span>{{</span>' + group[1] + '<span>}}</span>');
  90. }
  91. }
  92. src = src.replace(str, newStr);
  93. });
  94. }
  95. return src;
  96. }
  97. grunt.registerMultiTask('markdown', 'convert markdown to html', function(){
  98. var template = this.options().template;
  99. this.files.forEach(function(file) {
  100. file.src.filter(function(filepath) {
  101. if(!grunt.file.exists(filepath)) {
  102. grunt.log.warn('Source file "' + filepath + '" not found.');
  103. return false;
  104. } else {
  105. return true;
  106. }
  107. }).map(function(filepath) {
  108. var content = grunt.file.read(filepath);
  109. var basename = path.basename(filepath, '.md');
  110. content = preCompile(content, basename);
  111. content = marked(content, {
  112. highlight: function(code){
  113. return highlightjs.highlightAuto(code).value;
  114. }
  115. });
  116. return postCompile(content);
  117. }).map(function(content) {
  118. var html = grunt.template.process(grunt.file.read(template), {
  119. data: {
  120. content: content
  121. }
  122. });
  123. grunt.file.write(file.dest, html);
  124. });
  125. });
  126. });
  127. };