Skip to content

Instantly share code, notes, and snippets.

@Alhamou
Forked from mikeal/get-comments.js
Created June 14, 2022 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alhamou/4239d08ac46c54d5be713c6476964051 to your computer and use it in GitHub Desktop.
Save Alhamou/4239d08ac46c54d5be713c6476964051 to your computer and use it in GitHub Desktop.
The easiest way to get comments out of any code file... seriously?!?
var highlight = require('highlight.js')
var cheerio = require('cheerio')
var strip = ['/', '#', ' ', '*', "<", ">", '-', '\\']
function getComments (str) {
var html = highlight.highlightAuto(str).value
var $ = cheerio.load(html)
var lines = $('span.hljs-comment').map(function(i, el) {return $(this).text();}).get()
return lines.map(function (l) {
while (l.length && strip.indexOf(l[0]) !== -1) {
l = l.slice(1)
}
return l
})
}
module.exports = getComments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment