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