const md5 = require('md5') const path = require('path') const escapeTags = require('escape-html') const unescapeTags = require('unescape-html') const stripTags = require('html-text') const is = require('@sindresorhus/is') function getCopyrightHTML (user, plain) { let html = '' const name = is.string(user) ? user : plain ? user.name || user.copyright : escapeTags(user.name || user.copyright) if (user.url) { html = `${name}` } else { html = name } if (user.email) { html += ` <${ plain ? user.email : escapeTags(user.email) }>` } return html } module.exports = (req, res) => { const { user, options } = res.locals let name let gravatar // No error and valid if (user.copyright) { if (is.string(user.copyright)) { name = getCopyrightHTML(user, options.format !== 'html') } else if (is.array(user.copyright) && user.copyright.every(val => is.string(val))) { // Supports: ['Remy Sharp', 'Richie Bendall'] name = user.copyright .map(v => (options.format !== 'html' ? v : escapeTags(v))) .join(', ') } else { name = user.copyright.map(getCopyrightHTML).join(', ') } } if (user.gravatar && user.email) { // Supports regular format gravatar = `Profile image` } else if (is.object(user.copyright[0]) && user.gravatar) { // Supports multi-user format gravatar = `Profile image` } const year = options.pinnedYear ? options.pinnedYear : [options.startYear, options.endYear].filter(Boolean).join('-') const license = (options.license || user.license || 'MIT').toUpperCase() const format = options.format || user.format || 'html' const args = { info: `${year} ${name}`, theme: user.theme || 'default', gravatar } const filename = path.join(__dirname, '..', 'licenses', license) req.app.render(filename, args, (error, content) => { if (error) { res.status(500).send(error) return } if (format === 'txt') { const plain = content.match(/
(.*)<\/article>/ms)[1] res .set('Content-Type', 'text/plain; charset=UTF-8') .send(unescapeTags(stripTags(plain)).trim()) return } if (format === 'html') { res.send(content) return } res.json({ ...user, ...options }) }) }