Files: 09c0a2e49ccc68a4cdc3a98da00657c1aff6158b / message / html / meta / raw.js
1423 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 | 'font-size': '1rem', |
24 | cursor: 'pointer' |
25 | }, |
26 | 'ev-click': () => (rawMessage() === null) |
27 | ? rawMessage.set(buildRawMsg(msg)) |
28 | : rawMessage.set(null) |
29 | }, symbol) |
30 | } |
31 | } |
32 | |
33 | function buildRawMsg (msg) { |
34 | return h('pre', |
35 | colorKeys(linkify( |
36 | JSON.stringify({ key: msg.key, value: msg.value }, 0, 2) |
37 | )) |
38 | ) |
39 | } |
40 | |
41 | function colorKeys (chunks) { |
42 | var newArray = [] |
43 | chunks.forEach(chunk => { |
44 | if (typeof chunk !== 'string') return newArray.push(chunk) |
45 | |
46 | var arr = chunk.split(/("[^"]+":)/) |
47 | for (var i = 1; i < arr.length; i += 2) { |
48 | arr[i] = h('span', arr[i]) |
49 | } |
50 | newArray = [...newArray, ...arr] |
51 | }) |
52 | |
53 | return newArray |
54 | } |
55 | |
56 | function linkify (text) { |
57 | // from ssb-ref |
58 | var refRegex = /((?:@|%|&)[A-Za-z0-9\/+]{43}=\.[\w\d]+)/g |
59 | |
60 | var arr = text.split(refRegex) |
61 | for (var i = 1; i < arr.length; i += 2) { |
62 | arr[i] = h('a', {href: arr[i]}, arr[i]) |
63 | } |
64 | return arr |
65 | } |
66 |
Built with git-ssb-web