mirror of
https://github.com/samsonjs/mit-license.git
synced 2026-04-03 10:55:49 +00:00
98 lines
2.6 KiB
JavaScript
98 lines
2.6 KiB
JavaScript
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 = `<a href="${stripTags(user.url)}">${name}</a>`
|
|
} else {
|
|
html = name
|
|
}
|
|
|
|
if (user.email) {
|
|
html += ` <<a href="mailto:${stripTags(user.email)}">${
|
|
plain ? user.email : escapeTags(user.email)
|
|
}</a>>`
|
|
}
|
|
|
|
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 = `<img id="gravatar" alt="Profile image" src="https://www.gravatar.com/avatar/${md5(
|
|
user.email.trim().toLowerCase()
|
|
)}" />`
|
|
} else if (is.object(user.copyright[0]) && user.gravatar) {
|
|
// Supports multi-user format
|
|
gravatar = `<img id="gravatar" alt="Profile image" src="https://www.gravatar.com/avatar/${md5(
|
|
user.copyright[0].email.trim().toLowerCase()
|
|
)}" />`
|
|
}
|
|
|
|
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>(.*)<\/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 })
|
|
})
|
|
}
|