UNPKG

2.29 kBJavaScriptView Raw
1const fs = require('fs')
2const path = require('path')
3const execSync = require('child_process').execSync
4const markdownMagic = require('../index') // 'markdown-magic'
5
6const config = {
7 matchWord: 'MD-MAGIC-EXAMPLE', // default matchWord is AUTO-GENERATED-CONTENT
8 transforms: {
9 /* Match <!-- AUTO-GENERATED-CONTENT:START (customTransform:optionOne=hi&optionOne=DUDE) --> */
10 customTransform(content, options) {
11 console.log('original content in comment block', content)
12 console.log('options defined on transform', options)
13 // options = { optionOne: hi, optionOne: DUDE}
14 return `This will replace all the contents of inside the comment ${options.optionOne}`
15 },
16 /* Match <!-- AUTO-GENERATED-CONTENT:START (RENDERDOCS:path=../file.js) --> */
17 RENDERDOCS(content, options) {
18 const fileContents = fs.readFileSync(options.path, 'utf8')
19 const docBlocs = require('dox').parseComments(fileContents, { raw: true, skipSingleStar: true })
20 let updatedContent = ''
21 docBlocs.forEach((data) => {
22 updatedContent += `${data.description.full}\n\n`
23 })
24 return updatedContent.replace(/^\s+|\s+$/g, '')
25 },
26 /* Match <!-- AUTO-GENERATED-CONTENT:START (pluginExample) --> */
27 pluginExample: require('./plugin-example')({ addNewLine: true }),
28 /* Plugins from npm */
29 // count: require('markdown-magic-wordcount'),
30 // github: require('markdown-magic-github-contributors')
31 }
32}
33
34/* This example callback automatically updates Readme.md and commits the changes */
35const callback = function autoGitCommit(err, output) {
36 // output is array of file information
37 output.forEach(function(data) {
38 const mdPath = data.outputFilePath
39 if(!mdPath) return false
40 const gitAdd = execSync(`git add ${mdPath}`, {}, (error) => {
41 if (error) console.warn(error)
42 const msg = `${mdPath} automatically updated by markdown-magic`
43 const gitCommitCommand = `git commit -m '${msg}' --no-verify`
44 // execSync(gitCommitCommand, {}, (err) => {
45 // if (err) console.warn(err)
46 // console.log('git commit automatically ran. Push up your changes!')
47 // })
48 })
49 })
50}
51
52const markdownPath = path.join(__dirname, '..', 'README.md')
53markdownMagic(markdownPath, config, callback)