diff options
author | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2019-10-27 21:53:27 -1000 |
---|---|---|
committer | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2019-11-08 14:46:09 -1000 |
commit | afc11752be26c10ad7084d2555572e08c9cd7782 (patch) | |
tree | ba792e6196c8cdf5dceb75dfd7f32af2f301dd1e /lib | |
parent | c7b1a484c3b62b17f138fe05b1c5df92ea8b0db0 (diff) | |
download | patchfoo-afc11752be26c10ad7084d2555572e08c9cd7782.tar.gz patchfoo-afc11752be26c10ad7084d2555572e08c9cd7782.zip |
Smarter rendering of link objects
Diffstat (limited to 'lib')
-rw-r--r-- | lib/app.js | 9 | ||||
-rw-r--r-- | lib/render-msg.js | 41 |
2 files changed, 45 insertions, 5 deletions
@@ -486,10 +486,17 @@ function sbotGet(sbot, id, cb) { } function getMsgWithValue(sbot, id, cb) { + var self = this if (!id) return cb() + var parts = id.split('?unbox=') + id = parts[0] + var unbox = parts[1] && parts[1].replace(/ /g, '+') sbotGet(sbot, id, function (err, value) { if (err) return cb(err) - cb(null, {key: id, value: value}) + var msg = {key: id, value: value} + if (unbox && value && typeof value.content === 'string') + return self.app.unboxMsgWithKey(msg, unbox, cb) + cb(null, msg) }) } diff --git a/lib/render-msg.js b/lib/render-msg.js index 97b71d7..475965a 100644 --- a/lib/render-msg.js +++ b/lib/render-msg.js @@ -4,6 +4,7 @@ var multicb = require('multicb') var u = require('./util') var mdInline = require('./markdown-inline') var ssbKeys = require('ssb-keys') +var qs = require('querystring') module.exports = RenderMsg @@ -546,7 +547,8 @@ RenderMsg.prototype.link = function (link, cb) { RenderMsg.prototype.link1 = function (link, cb) { var self = this - var ref = u.linkDest(link) + var ref = u.linkDest(link) + + (link && link.query ? '?' + qs.stringify(link.query) : '') if (!ref) return cb(), '' var a = h('a', {href: self.toUrl(ref)}, ref) self.getName(ref, function (err, name) { @@ -999,6 +1001,37 @@ function linkName(link) { return link.name } +var knownLinkProps = { + link: true, + query: true, + name: true, + type: true, + size: true +} + +RenderMsg.prototype.linkValue = function (link, cb) { + var self = this + var done = multicb({pluck: 1, spread: true}) + var extra + for (var k in link) { + if (knownLinkProps[k]) continue + if (!extra) extra = {} + extra[k] = link[k] + } + // TODO should check for error thrown by stringify? + var ref = link.link + (link.query ? '?' + qs.stringify(link.query) : '') + var el = h('div', [ + link.name + ? h('a', {href: self.toUrl(ref)}, linkName(link)) + : self.link1(link, done()), + (link.type ? ' [' + link.type + ']' : ''), + (link.size != null ? ' (' + self.render.formatSize(link.size) + ')' : ''), + extra ? self.valueTable(extra, NaN, done()) : '' + ]) + done(cb) + return el +} + RenderMsg.prototype.valueTable = function (val, depth, cb) { var isContent = depth === 1 var self = this @@ -1011,9 +1044,9 @@ RenderMsg.prototype.valueTable = function (val, depth, cb) { ? h('ul', val.map(function (item) { return h('li', self.valueTable(item, depth + 1, done())) })) - : (keys = Object.keys(val)).sort().join() === 'link,name' && val.link && val.name - ? h('a', {href: self.toUrl(val.link)}, linkName(val)) - : h('table.ssb-object', keys.map(function (key) { + : typeof val.link === 'string' && u.isRef(val.link) + ? self.linkValue(val, done()) + : h('table.ssb-object', Object.keys(val).map(function (key) { if (key === 'text') { return h('tr', h('td', h('strong', 'text')), |