build-site.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. var _ = require('lodash'),
  3. fs = require('fs'),
  4. marky = require('marky-markdown'),
  5. path = require('path'),
  6. util = require('../common/util');
  7. var basePath = path.join(__dirname, '..', '..'),
  8. docPath = path.join(basePath, 'doc'),
  9. readmePath = path.join(docPath, 'README.md');
  10. function build(type) {
  11. var markdown = fs
  12. // Load markdown.
  13. .readFileSync(readmePath, 'utf8')
  14. // Uncomment docdown HTML hints.
  15. .replace(/(<)!--\s*|\s*--(>)/g, '$1$2');
  16. var $ = marky(markdown, { 'sanitize': false }),
  17. $header = $('h1').first().remove(),
  18. version = _.trim($header.find('span').first().text()).slice(1);
  19. // Remove docdown horizontal rules.
  20. $('hr').remove();
  21. // Remove marky-markdown additions.
  22. $('[id^="user-content-"]')
  23. .attr('class', null)
  24. .attr('id', null);
  25. $(':header:not(h3) > a').each(function() {
  26. var $a = $(this);
  27. $a.replaceWith($a.html());
  28. });
  29. // Fix marky-markdown wrapping around headers.
  30. $('p:empty + h3').prev().remove();
  31. $('h3 ~ p:empty').each(function() {
  32. var $p = $(this),
  33. node = this.previousSibling;
  34. while ((node = node.previousSibling) && node.name != 'h3' && node.name != 'p') {
  35. $p.prepend(node.nextSibling);
  36. }
  37. });
  38. $('h3 code em').parent().each(function() {
  39. var $code = $(this);
  40. $code.html($code.html().replace(/<\/?em>/g, '_'));
  41. });
  42. var html = [
  43. // Append YAML front matter.
  44. '---',
  45. 'id: docs',
  46. 'layout: docs',
  47. 'title: Lodash Documentation',
  48. 'version: ' + (version || null),
  49. '---',
  50. '',
  51. // Wrap in raw tags to avoid Liquid template tag processing.
  52. '{% raw %}',
  53. _.trim($.html()),
  54. '{% endraw %}',
  55. ''
  56. ].join('\n');
  57. fs.writeFile(path.join(docPath, version + '.html'), html, util.pitch);
  58. }
  59. build();