build-doc.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var _ = require('lodash'),
  3. docdown = require('docdown'),
  4. fs = require('fs-extra'),
  5. path = require('path');
  6. var util = require('../common/util');
  7. var basePath = path.join(__dirname, '..', '..'),
  8. docPath = path.join(basePath, 'doc'),
  9. readmePath = path.join(docPath, 'README.md');
  10. var pkg = require('../../package.json'),
  11. version = pkg.version;
  12. var config = {
  13. 'base': {
  14. 'path': path.join(basePath, 'lodash.js'),
  15. 'title': '<a href="https://lodash.com/">lodash</a> <span>v' + version + '</span>',
  16. 'toc': 'categories',
  17. 'url': 'https://github.com/lodash/lodash/blob/' + version + '/lodash.js'
  18. },
  19. 'github': {
  20. 'style': 'github',
  21. 'sublinks': [npmLink('&#x24C3;', 'See the npm package')]
  22. },
  23. 'site': {
  24. 'entryLink': '<a href="${entryHref}" class="fa fa-link"></a>',
  25. 'sourceLink': '[source](${sourceHref})',
  26. 'tocHref': '',
  27. 'tocLink': '',
  28. 'sublinks': [npmLink('npm package')]
  29. }
  30. };
  31. /**
  32. * Composes a npm link from `text` and optional `title`.
  33. *
  34. * @private
  35. * @param {string} text The link text.
  36. * @param {string} [title] The link title.
  37. * @returns {string} Returns the composed npm link.
  38. */
  39. function npmLink(text, title) {
  40. return (
  41. '<% if (name == "templateSettings" || !/^(?:methods|properties|seq)$/i.test(category)) {' +
  42. 'print(' +
  43. '"[' + text + '](https://www.npmjs.com/package/lodash." + name.toLowerCase() + ' +
  44. '"' + (title == null ? '' : ' \\"' + title + '\\"') + ')"' +
  45. ');' +
  46. '} %>'
  47. );
  48. }
  49. /**
  50. * Post-process `markdown` to make adjustments.
  51. *
  52. * @private
  53. * @param {string} markdown The markdown to process.
  54. * @returns {string} Returns the processed markdown.
  55. */
  56. function postprocess(markdown) {
  57. // Wrap symbol property identifiers in brackets.
  58. return markdown.replace(/\.(Symbol\.(?:[a-z]+[A-Z]?)+)/g, '[$1]');
  59. }
  60. /*----------------------------------------------------------------------------*/
  61. /**
  62. * Creates the documentation markdown formatted for 'github' or 'site'.
  63. *
  64. * @private
  65. * @param {string} type The format type.
  66. */
  67. function build(type) {
  68. var options = _.defaults({}, config.base, config[type]),
  69. markdown = docdown(options);
  70. fs.writeFile(readmePath, postprocess(markdown), util.pitch);
  71. }
  72. build(_.last(process.argv));