From a73918fe22e481b5f44479740dd3d5910970bf6f Mon Sep 17 00:00:00 2001 From: cel Date: Wed, 1 May 2019 16:51:09 -1000 Subject: Handle boxed markdown blobs --- lib/app.js | 17 +++++++++++++++++ lib/render.js | 9 ++++++--- lib/serve.js | 9 +++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/app.js b/lib/app.js index 3803281..9da761e 100644 --- a/lib/app.js +++ b/lib/app.js @@ -379,6 +379,13 @@ App.prototype.addBlobPrivate = function (cb) { } App.prototype.getBlob = function (id, key) { + if (!key) { + var m = /^(.*)\?unbox=(.*)$/.exec(id) + if (m) { + id = m[1] + key = m[2] + } + } if (!key) return this.sbot.blobs.get(id) if (typeof key === 'string') key = new Buffer(key, 'base64') return pull( @@ -387,6 +394,16 @@ App.prototype.getBlob = function (id, key) { ) } +App.prototype.getHasBlob = function (id, cb) { + id = id.replace(/\?unbox=.*/, '') + if (!this.sbot.blobs) return cb(new TypeError('missing ssb-blobs plugin')) + if (!this.sbot.blobs.size) return cb(new TypeError('missing blobs.size method')) + this.sbot.blobs.size(id, function (err, size) { + if (err) return cb(err) + cb(null, size != null) + }) +} + App.prototype.pushBlob = function (id, cb) { console.error('pushing blob', id) this.sbot.blobs.push(id, cb) diff --git a/lib/render.js b/lib/render.js index 055d3d2..ec62b31 100644 --- a/lib/render.js +++ b/lib/render.js @@ -66,7 +66,7 @@ MdRenderer.prototype.link = function (ref, title, text) { var link = this.render._mentionsByLink[ref] if (link && link.type === 'text/x-markdown') { html += h('sup', ' [', h('a', { - href: this.render.toUrl('/markdown/' + encodeURIComponent(ref)), + href: this.render.toUrl('/markdown/' + ref), title: 'view rendered markdown' }, 'md'), ']').outerHTML } @@ -86,7 +86,7 @@ MdRenderer.prototype.mention = function (preceding, id) { var link = this.render._mentionsByLink[id] if (link && link.type === 'text/x-markdown') { html += h('sup', ' [', h('a', { - href: this.render.toUrl('/markdown/' + encodeURIComponent(id)), + href: this.render.toUrl('/markdown/' + id), title: 'view rendered markdown' }, 'md'), ']').outerHTML } @@ -179,7 +179,10 @@ Render.prototype.markdown = function (text, mentions) { else if (link.host === 'http://localhost:7777') mentionsObj[link.href] = link.link if (link.link) - mentionsByLink[link.link + (link.key ? '#' + link.key : '')] = link + mentionsByLink[link.link + + (link.query && typeof link.query.unbox === 'string' ? + '?unbox=' + link.query.unbox.replace(/\s/g, '+') : '') + + (link.key ? '#' + link.key : '')] = link }) var out = marked(String(text), this.markedOpts) delete this._mentions diff --git a/lib/serve.js b/lib/serve.js index c2f7cca..d8be4fc 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -3028,16 +3028,17 @@ Serve.prototype.npmRegistry = function (url) { Serve.prototype.markdown = function (url) { var self = this var id = decodeURIComponent(url.substr(1)) - var blobs = self.app.sbot.blobs + if (typeof self.query.unbox === 'string') id += '?unbox=' + self.query.unbox.replace(/\s/g, '+') return pull( ph('section', {}, [ ph('h3', [ ph('a', {href: '/links/' + id}, id.substr(0, 8) + '…') ]), u.readNext(function (cb) { - blobs.size(id, function (err, size) { - if (size == null) return cb(null, self.askWantBlobsForm([id])) - pull(blobs.get(id), pull.collect(function (err, chunks) { + self.app.getHasBlob(id, function (err, has) { + if (err) return cb(err) + if (!has) return cb(null, self.askWantBlobsForm([id])) + pull(self.app.getBlob(id), pull.collect(function (err, chunks) { if (err) return cb(null, ph('div', u.renderError(err).outerHTML)) var text = Buffer.concat(chunks).toString() cb(null, ph('blockquote', self.app.render.markdown(text))) -- cgit v1.2.3