Skip to content

Instantly share code, notes, and snippets.

@hoyangtsai
Created November 11, 2019 11:03
Show Gist options
  • Save hoyangtsai/56c808029f2af6346991a34765fdfd82 to your computer and use it in GitHub Desktop.
Save hoyangtsai/56c808029f2af6346991a34765fdfd82 to your computer and use it in GitHub Desktop.
split js function into each markdown
const jsdoc2md = require('jsdoc-to-markdown')
const gulp = require('gulp')
const glob = require('glob')
const path = require('path')
const fs = require('fs-extra')
const docDir = './docs'
async function doJsdoc2md() {
const fileSrc = 'src/**/*.js';
const files = glob.sync(fileSrc);
files.forEach(async file => {
if (file.indexOf('src/index') >= 0) return;
const modName = path.basename(file, '.js');
let filename = modName + '.md';
let jsdocOptions = {
files: file,
template: fs.readFileSync('./template.hbs', 'utf8'),
plugin: ['dmd-plugin-gitbook'],
noCache: true
}
await jsdoc2md.getTemplateData({
files: file,
}).then(blocks => {
const dir = path.join(outputDir, modName);
fs.ensureDirSync(dir)
blocks.forEach(async b => {
if (b.kind == 'function') {
let name = b.name
await jsdoc2md
.render({...jsdocOptions, data: [b], template: fs.readFileSync('./template.hbs', 'utf8')})
.then(output => {
fs.outputFileSync(path.join(dir, `${name}.md`), output)
});
} else if (b.kind == 'module') {
await jsdoc2md
.render({...jsdocOptions, source: b, template: fs.readFileSync('./readme.hbs', 'utf8')})
.then(output => fs.outputFileSync(path.join(dir, `readme.md`), output));
}
})
})
})
const readmeFile = 'README.md';
fs.copySync(`./${readmeFile}`, path.join(docDir, readmeFile))
}
exports.js2doc = gulp.parallel(doJsdoc2md);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment