UNPKG

1.54 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const globby = require('globby')
3const processFile = require('./lib/processFile')
4/**
5 * ### API
6 * ```js
7 * markdownMagic(filePath, config, callback)
8 * ```
9 * - `filePaths` - *String or Array* - Path or glob pattern. Uses [globby patterns](https://github.com/sindresorhus/multimatch/blob/master/test.js)
10 * - `config` - See configuration options below
11 * - `callback` - callback to run after markdown updates
12 *
13 * @param {string} filePath - Path to markdown file
14 * @param {object} [config] - configuration object
15 * @param {Function} [callback] - callback function with updated contents
16 */
17module.exports = function markdownMagic(filePaths, config, callback) {
18 const files = globby.sync(filePaths)
19 const configuration = config || {}
20 if (!callback && typeof configuration === 'function') {
21 callback = configuration // eslint-disable-line
22 } else if (typeof config === 'object' && config.callback) {
23 // set callback in config for CLI usage
24 callback = config.callback // eslint-disable-line
25 }
26 if (!files.length) {
27 callback && callback('No files matched')
28 console.log('No files matched pattern', filePaths)
29 return false
30 }
31 configuration.originalFilePaths = files
32 const data = []
33 files.forEach((file) => {
34 const output = processFile(file, configuration)
35 data.push(output)
36 })
37 if (callback) {
38 callback(null, data)
39 }
40}
41
42// expose globby for use in plugins
43module.exports.globby = globby
44
45// expose fs-extra for use in plugins
46module.exports.fsExtra = fs