This crude little module converts JavaScript files to MarkDown files. It puts the multi-line comments as the normal text, and the code into code blocks.
This is useful when you want commented code in a MarkDown format (for a README.md for instance).
npm i -g
js2md index.js > README.md
This README.md is the effect of running js2md on index.js
var output
var hasbegun
var incomments
var title = /([^#]|^)#[^#]/
var openingComment = /\/\*/
var closingComment = /\*\//
var indent
For reading the file I used the line-by-line
module
lr.on('line', function (line) {
var toWrite = null
var change = false
if(incomments) {
if(closingComment.test(line)) {
change = true
toWrite = hasbegun && '\n```javascript\n'
}
} else {
if(openingComment.test(line)) {
change = true
toWrite = hasbegun && '```\n\n'
}
}
if(hasbegun) {
if(toWrite) {
output += toWrite
} else {
output += line.slice(indent) + '\n'
}
} else if(incomments && (hasbegun = line.match(title))) {
indent = hasbegun.index + hasbegun[0].indexOf('#')
output = line.slice(indent) + '\n'
}
if(change) {
incomments = !incomments
}
})
lr.on('end', function () {
output += '```\n\n'
process.stdout.write(output)
})
lr.on('error', function (err) {
throw err
})