Files: 652ecbb4cd7581d318f8a02e163001b78d13ee25 / message / html / meta / raw.js
1337 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | |
4 | exports.gives = nest('message.html.meta') |
5 | |
6 | exports.needs = nest({ |
7 | 'about.obs.name': 'first', |
8 | 'message.obs.likes': 'first' |
9 | }) |
10 | |
11 | exports.create = (api) => { |
12 | return nest('message.html.meta', raw) |
13 | |
14 | function raw (msg, { rawMessage } = {}) { |
15 | return h('i.toggle-raw-msg.fa.fa-plus', { |
16 | title: 'View raw data', |
17 | style: { |
18 | order: 99, |
19 | color: '#a8a8a8', |
20 | cursor: 'pointer' |
21 | }, |
22 | 'ev-click': () => (rawMessage() === null) |
23 | ? rawMessage.set(buildRawMsg(msg)) |
24 | : rawMessage.set(null) |
25 | }) |
26 | } |
27 | } |
28 | |
29 | function buildRawMsg (msg) { |
30 | return h('pre', |
31 | colorKeys(linkify( |
32 | JSON.stringify({ key: msg.key, value: msg.value }, 0, 2) |
33 | )) |
34 | ) |
35 | } |
36 | |
37 | function colorKeys (chunks) { |
38 | var newArray = [] |
39 | chunks.forEach(chunk => { |
40 | if (typeof chunk !== 'string') return newArray.push(chunk) |
41 | |
42 | var arr = chunk.split(/("[^"]+":)/) |
43 | for (var i = 1; i < arr.length; i += 2) { |
44 | arr[i] = h('span', arr[i]) |
45 | } |
46 | newArray = [...newArray, ...arr] |
47 | }) |
48 | |
49 | return newArray |
50 | } |
51 | |
52 | function linkify (text) { |
53 | // from ssb-ref |
54 | var refRegex = /((?:@|%|&)[A-Za-z0-9/+]{43}=\.[\w\d]+)/g |
55 | |
56 | var arr = text.split(refRegex) |
57 | for (var i = 1; i < arr.length; i += 2) { |
58 | arr[i] = h('a', {href: arr[i]}, arr[i]) |
59 | } |
60 | return arr |
61 | } |
62 |
Built with git-ssb-web