{"size": 1129, "ext": "js", "max_stars_repo_path": "models/story/lede.js", "max_stars_repo_name": "brechtcs/enclave", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "models/story/lede.js", "max_issues_repo_name": "brechtcs/enclave", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "models/story/lede.js", "max_forks_repo_name": "brechtcs/enclave", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.3018867925, "max_line_length": 58, "alphanum_fraction": 0.6775907883}
var { hash, string } = require('stdopt')
var Base = require('stdopt/base')
var VError = require('verror')
var parse = require('rehype-parse')
var sanitize = require('rehype-sanitize')
var schema = require('./sanitize')
var stringify = require('rehype-stringify')
var unified = require('unified')
var textup = require('./textup')

var struct = {
  content: string
}

function Lede (html) {
  if (this instanceof Lede) Base.call(this, html)
  else return new Lede(html)
}

Lede.parse = function (html) {
  var content = rehype(html)

  return hash({ content }, struct).use(function (err, l) {
    if (err) throw new VError(err, 'Invalid Lede')
    return l
  })
}

module.exports = Lede

/**
 * Rehype parsing logic
 */
var allowed = ['br', 'p', 'strong', 'em']
var strip = schema.tagNames.filter(function (tag) {
  return allowed.includes(tag) === false
})

schema.strip.push.apply(schema.strip, strip)
schema.tagNames = allowed

var parser = unified()
  .use(parse)
  .use(sanitize, schema)
  .use(textup)
  .use(stringify)
  .freeze()

function rehype (html) {
  var result = parser.processSync(html)
  return result.contents
}
