const md5 = require('md5'); const path = require('path'); const { stripTags, escapeTags, unescapeTags } = require('./utils'); const _ = require('lodash'); function getCopyrightHTML(user, plain) { let html = ''; const name = _.isString(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 (_.isString(user.copyright)) { name = getCopyrightHTML(user, options.format !== 'html'); } else if (user.copyright.every(val => _.isString(val))) { // Supports: ['Remy Sharp', 'Richie Bendall'] name = user .map(_ => (options.format !== 'html' ? _ : escapeTags(_))) .join(', '); } else { name = user.copyright.map(getCopyrightHTML).join(', '); } } if (user.gravatar && user.email) { // Supports regular format gravatar = `Profile image`; } else if (_.isObject(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 }); }); };