Files: 7a1861362de764504e02614b8c8bc5837fdc089d / message / html / meta / raw.js
1394 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 | const symbol = '\u2699' // gear ⚙ |
13 | |
14 | return nest('message.html.meta', raw) |
15 | |
16 | function raw (msg, { rawMessage } = {}) { |
17 | return h('a', { |
18 | title: 'View raw data', |
19 | classList: 'toggle-raw-msg', |
20 | style: { |
21 | order: 99, |
22 | color: '#a8a8a8', |
23 | cursor: 'pointer' |
24 | }, |
25 | 'ev-click': () => (rawMessage() === null) |
26 | ? rawMessage.set(buildRawMsg(msg)) |
27 | : rawMessage.set(null) |
28 | }, symbol) |
29 | } |
30 | } |
31 | |
32 | function buildRawMsg (msg) { |
33 | return h('pre', |
34 | colorKeys(linkify( |
35 | JSON.stringify({ key: msg.key, value: msg.value }, 0, 2) |
36 | )) |
37 | ) |
38 | } |
39 | |
40 | function colorKeys (chunks) { |
41 | var newArray = [] |
42 | chunks.forEach(chunk => { |
43 | if (typeof chunk !== 'string') return newArray.push(chunk) |
44 | |
45 | var arr = chunk.split(/("[^"]+":)/) |
46 | for (var i = 1; i < arr.length; i += 2) { |
47 | arr[i] = h('span', arr[i]) |
48 | } |
49 | newArray = [...newArray, ...arr] |
50 | }) |
51 | |
52 | return newArray |
53 | } |
54 | |
55 | function linkify (text) { |
56 | // from ssb-ref |
57 | var refRegex = /((?:@|%|&)[A-Za-z0-9\/+]{43}=\.[\w\d]+)/g |
58 | |
59 | var arr = text.split(refRegex) |
60 | for (var i = 1; i < arr.length; i += 2) { |
61 | arr[i] = h('a', {href: arr[i]}, arr[i]) |
62 | } |
63 | return arr |
64 | } |
65 |
Built with git-ssb-web