From 0a454a46848521e113aad74e294f5d93fee54368 Mon Sep 17 00:00:00 2001 From: cel Date: Wed, 1 Feb 2017 18:10:31 -0800 Subject: raw msg json: linkify sequence, type and channel --- lib/render-msg.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'lib/render-msg.js') diff --git a/lib/render-msg.js b/lib/render-msg.js index be055ff..88e6d97 100644 --- a/lib/render-msg.js +++ b/lib/render-msg.js @@ -28,10 +28,60 @@ RenderMsg.prototype.linkify = function (text) { return arr } +function token() { + return '__' + Math.random().toString(36).substr(2) + '__' +} + RenderMsg.prototype.raw = function (cb) { - this.wrap(h('pre', - this.linkify(JSON.stringify(this.msg, 0, 2)) - ), cb) + // linkify various things in the JSON. TODO: abstract this better + + // clone the message for linkifying + var m = {}, k + for (k in this.msg) m[k] = this.msg[k] + m.value = {} + for (k in this.msg.value) m.value[k] = this.msg.value[k] + var tokens = {} + + // link to feed starting from this message + var tok = token() + tokens[tok] = h('a', {href: + this.toUrl(m.value.author + '?gt=' + (m.value.sequence-1))}, + m.value.sequence) + m.value.sequence = tok + + if (typeof m.value.content === 'object' && m.value.content != null) { + var c = m.value.content = {} + for (k in this.c) c[k] = this.c[k] + + // link to messages of same type + tok = token() + tokens[tok] = h('a', {href: this.toUrl('/type/' + c.type)}, c.type) + c.type = tok + + // link to channel + if (c.channel) { + tok = token() + tokens[tok] = h('a', {href: this.toUrl('#' + c.channel)}, c.channel) + c.channel = tok + } + } + + // link refs + var els = this.linkify(JSON.stringify(m, 0, 2)) + + // stitch it all together + for (var i = 0; i < els.length; i++) { + if (typeof els[i] === 'string') { + for (var tok in tokens) { + if (els[i].indexOf(tok) !== -1) { + var parts = els[i].split(tok) + els.splice(i, 1, parts[0], tokens[tok], parts[1]) + continue + } + } + } + } + this.wrap(h('pre', els), cb) } RenderMsg.prototype.wrap = function (content, cb) { -- cgit v1.2.3