aboutsummaryrefslogtreecommitdiff
path: root/lib/render-msg.js
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-02-01 18:10:31 -0800
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-02-04 02:53:52 -0800
commit0a454a46848521e113aad74e294f5d93fee54368 (patch)
tree865a966edea87162d3afddf519bee10acd6ef8d4 /lib/render-msg.js
parentf6404aeb448ac219173dc1740a026c19696ac764 (diff)
downloadpatchfoo-0a454a46848521e113aad74e294f5d93fee54368.tar.gz
patchfoo-0a454a46848521e113aad74e294f5d93fee54368.zip
raw msg json: linkify sequence, type and channel
Diffstat (limited to 'lib/render-msg.js')
-rw-r--r--lib/render-msg.js56
1 files changed, 53 insertions, 3 deletions
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) {