UNPKG

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