<|start_filename|>markdown.config.js<|end_filename|>
/* CLI markdown.config.js file example */
module.exports = {
  matchWord: 'MD-MAGIC-EXAMPLE',
  transforms: {
    /* Match <!-- AUTO-GENERATED-CONTENT:START (LOLZ) --> */
    LOLZ(content, options) {
      return `This section was generated by the cli config markdown.config.js file`
    }
  },
  callback: function () {
    console.log('markdown processing done')
  }
}


<|start_filename|>lib/transforms/remote.js<|end_filename|>
const regexUtils = require('../utils/regex')
const remoteRequest = require('../utils/remoteRequest')

module.exports = function REMOTE(content, options, config) {
  const remoteContent = remoteRequest(options.url) || content
  if (!remoteContent) {
    return content
  }
  if (options.keepComments) {
    return remoteContent
  }
  const openTagRegex = regexUtils.matchOpeningCommentTag(config.matchWord)
  const closeTagRegex = regexUtils.matchClosingCommentTag(config.matchWord)
  return remoteContent.replace(openTagRegex, '').replace(closeTagRegex, '')
}


<|start_filename|>lib/utils/sortOrder.js<|end_filename|>

module.exports = function sortPluginOrder(transformsRegistered, transformsFound) {
  const order = []
  let transformsToRun = transformsFound

  const defaultOrder = transformsRegistered.sort((a, b) => {
    // put table of contents (TOC) at end of tranforms
    if (a !== 'TOC') return -1
    if (b !== 'TOC') return 1
    return 0
  })

  defaultOrder.forEach((key) => {
    transformsToRun = transformsToRun.filter((data) => {
      const command = data.transform.split(':')
      if (command[0] === key) {
        order.push(data)
        return false
      }
      return true
    })
  })

  return order
}


<|start_filename|>lib/transforms/file.js<|end_filename|>
const path = require('path')
const fs = require('fs')
const isLocalPath = require('is-local-path')

module.exports = function FILE(content, options, config) {
  let fileContents
  let syntax = options.syntax
  if (!options.src) {
    return false
  }
  if (isLocalPath(options.src)) {
    const fileDir = path.dirname(config.originalPath)
    const filePath = path.join(fileDir, options.src)
    try {
      fileContents = fs.readFileSync(filePath, 'utf8', (err, contents) => {
        if (err) {
          console.log(`FILE NOT FOUND ${filePath}`)
          console.log(err)
           // throw err
        }
        return contents
      })
    } catch (e) {
      console.log(`FILE NOT FOUND ${filePath}`)
      throw e
    }
    if (!syntax) {
      syntax = path.extname(filePath).replace(/^./, '')
    }
  }

  // trim leading and trailing spaces/line breaks in code and keeps the indentation of the first non-empty line
  fileContents = fileContents.replace(/^(?:[\t ]*(?:\r?\n|\r))+|\s+$/g, '')

  return `<!-- The below content is automatically added from ${options.src} -->
${fileContents}`
}


<|start_filename|>index.js<|end_filename|>
const globby = require('globby')
const processFile = require('./lib/processFile')
/**
 * ### API
 * ```js
 * markdownMagic(filePath, config, callback)
 * ```
 * - `filePaths` - *String or Array* - Path or glob pattern. Uses [globby patterns](https://github.com/sindresorhus/multimatch/blob/master/test.js)
 * - `config` - See configuration options below
 * - `callback` - callback to run after markdown updates
 *
 * @param  {string} filePath - Path to markdown file
 * @param  {object} [config] - configuration object
 * @param  {Function} [callback] - callback function with updated contents
 */
module.exports = async function markdownMagic(filePaths, config, callback) {
  const files = globby.sync(filePaths)
  const configuration = config || {}
  if (!callback && typeof configuration === 'function') {
    callback = configuration // eslint-disable-line
  } else if (typeof config === 'object' && config.callback) {
    // set callback in config for CLI usage
    callback = config.callback // eslint-disable-line
  }
  if (!files.length) {
    callback && callback('No files matched')
    console.log('No files matched pattern', filePaths)
    return false
  }
  configuration.originalFilePaths = files

  const data = files.map(async (file) => {
    const x = await processFile(file, configuration)
    return x
  })

  const values = await Promise.all(data)

  if (callback) {
    callback(null, values)
  }
  return values
}

// expose globby for use in plugins
module.exports.globby = globby
