diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/render-msg.js | 109 | ||||
-rw-r--r-- | lib/render.js | 16 | ||||
-rw-r--r-- | lib/util.js | 10 |
3 files changed, 129 insertions, 6 deletions
diff --git a/lib/render-msg.js b/lib/render-msg.js index 8346baf..262b6e7 100644 --- a/lib/render-msg.js +++ b/lib/render-msg.js @@ -241,6 +241,7 @@ RenderMsg.prototype.message = function (cb) { case 'wifi-network': return this.wifiNetwork(cb) case 'mutual/credit': return this.mutualCredit(cb) case 'mutual/account': return this.mutualAccount(cb) + case 'npm-publish': return this.npmPublish(cb) default: return this.object(cb) } } @@ -268,13 +269,9 @@ RenderMsg.prototype.markdownSource = function (text, mentions) { RenderMsg.prototype.post = function (cb) { var self = this var done = multicb({pluck: 1, spread: true}) - var branchDone = multicb({pluck: 1}) - u.toArray(self.c.branch).forEach(function (branch) { - self.link(branch, branchDone()) - }) if (self.c.root === self.c.branch) done()() else self.link(self.c.root, done()) - branchDone(done()) + self.links(self.c.branch, done()) done(function (err, rootLink, branchLinks) { if (err) return self.wrap(u.renderError(err), cb) self.wrap(h('div.ssb-post', @@ -347,6 +344,8 @@ RenderMsg.prototype.title = function (cb) { } else { if (self.c.type === 'ssb-dns') cb(null, self.c.record && JSON.stringify(self.c.record.data) || self.msg.key) + else if (self.c.type === 'npm-publish') + self.npmPublishTitle(cb) else self.app.getAbout(self.msg.key, function (err, about) { if (err) return cb(err) @@ -389,6 +388,15 @@ RenderMsg.prototype.link1 = function (link, cb) { return a } +RenderMsg.prototype.links = function (links, cb) { + var self = this + var done = multicb({pluck: 1}) + u.toArray(links).forEach(function (link) { + self.link(link, done()) + }) + done(cb) +} + function dateTime(d) { var date = new Date(d.epoch) return date.toString() @@ -840,3 +848,94 @@ RenderMsg.prototype.micro = function (cb) { var el = h('span', {innerHTML: unwrapP(this.markdown())}) this.wrapMini(el, cb) } + +function hJoin(els, seperator) { + return els.map(function (el, i) { + return [i === 0 ? '' : separator, el] + }) +} + +function asNpmReadme(readme) { + if (!readme || readme === 'ERROR: No README data found!') return + return u.ifString(readme) +} + +RenderMsg.prototype.npmPublish = function (cb) { + var self = this + var render = self.render + var pkg = self.c.meta || {} + var pkgReadme = asNpmReadme(pkg.readme) + var pkgDescription = u.ifString(pkg.description) + + var versions = Object.keys(pkg.versions || {}) + var singleVersion = versions.length === 1 ? versions[0] : null + var singleRelease = singleVersion && pkg.versions[singleVersion] + var singleReadme = singleRelease && asNpmReadme(singleRelease.readme) + + var distTags = pkg['dist-tags'] || {} + var distTagged = {} + for (var distTag in distTags) + if (distTag !== 'latest') + distTagged[distTags[distTag]] = distTag + + self.links(self.c.previousPublish, function (err, prevLinks) { + if (err) return cb(err) + self.wrap([ + h('div', + 'published ', + h('u', pkg.name), ' ', + hJoin(versions.map(function (version) { + var distTag = distTagged[version] + return [h('b', version), distTag ? [' (', h('i', distTag), ')'] : ''] + }), ', ') + ), + pkgDescription ? h('div', h('q', self.linkify(pkgDescription))) : '', + prevLinks.length ? h('div', 'previous: ', prevLinks) : '', + pkgReadme && pkgReadme !== singleReadme ? + h('blockquote', {innerHTML: render.markdown(pkgReadme)}) : '', + versions.map(function (version, i) { + var release = pkg.versions[version] || {} + var license = u.ifString(release.license) + var author = release.author + var description = u.ifString(release.description) + var readme = asNpmReadme(release.readme) + var keywords = u.toArray(release.keywords).map(u.ifString) + var dist = release.dist || {} + var size = u.ifNumber(dist.size) + return [ + h > 0 ? h('br') : '', + version !== singleVersion ? h('div', 'version: ', version) : '', + author ? h('div', 'author: ', render.npmAuthorLink(author)) : '', + license ? h('div', 'license: ', h('code', license)) : '', + keywords.length ? h('div', 'keywords: ', keywords.join(', ')) : '', + size ? h('div', 'size: ', render.formatSize(size)) : '', + description && description !== pkgDescription ? + h('div', h('q', self.linkify(description))) : '', + readme ? h('blockquote', {innerHTML: render.markdown(readme)}) : '' + ] + }) + ], cb) + }) +} + +RenderMsg.prototype.npmPublishTitle = function (cb) { + var pkg = this.c.meta || {} + var name = pkg.name || pkg._id || '?' + + var taggedVersions = {} + for (var version in pkg.versions || {}) + taggedVersions[version] = [] + + var distTags = pkg['dist-tags'] || {} + for (var distTag in distTags) { + if (distTag === 'latest') continue + var version = distTags[distTag] || '?' + var tags = taggedVersions[version] || (taggedVersions[version] = []) + tags.push(distTag) + } + + cb(null, name + '@' + Object.keys(taggedVersions).map(function (version) { + var tags = taggedVersions[version] + return (tags.length ? tags.join(',') + ':' : '') + version + }).join(',')) +} diff --git a/lib/render.js b/lib/render.js index fdff95c..2aef6b4 100644 --- a/lib/render.js +++ b/lib/render.js @@ -346,3 +346,19 @@ Render.prototype.getNameLink = function (id, cb) { cb(null, h('a', {href: self.toUrl(id)}, name)) }) } + +Render.prototype.npmAuthorLink = function (author) { + if (!author) return + var url = u.ifString(author.url) + var email = u.ifString(author.email) + var name = u.ifString(author.name) + if (!url && !email) return name || JSON.stringify(author) + if (!url && email) url = 'mailto:' + email, email = null + if (!name && email) name = email, email = null + if (!name && url) name = url + var secondaryLink = email && h('a', {href: this.toUrl('mailto:' + email)}, email) + return [ + h('a', {href: this.toUrl(url)}, name), + secondaryLink ? [' (', secondaryLink, ')'] : '' + ] +} diff --git a/lib/util.js b/lib/util.js index 57183d2..713705d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -63,12 +63,20 @@ u.hyperwrap = function (fn) { } } +u.ifString = function (str) { + if (typeof str === 'string') return str +} + +u.ifNumber = function (num) { + if (!isNaN(num)) return num +} + u.toLink = function (link) { return typeof link === 'string' ? {link: link} : link } u.linkDest = function (link) { - return typeof link === 'string' ? link : link && link.link || link + return link && (u.ifString(link) || u.ifString(link.link)) } u.toArray = function (x) { |