Files: 2f3fa2face6b2be68c4502ddf460be8c0783d0bc / modules_basic / about.js
2400 bytesRaw
1 | const fs = require('fs') |
2 | const h = require('../h') |
3 | const { when } = require('@mmckegg/mutant') |
4 | |
5 | exports.needs = { |
6 | blob_url: 'first', |
7 | markdown: 'first' |
8 | } |
9 | |
10 | exports.gives = { |
11 | mcss: true, |
12 | message_content: true, |
13 | message_content_mini: true |
14 | } |
15 | |
16 | exports.create = function (api) { |
17 | return { |
18 | message_content, |
19 | message_content_mini, |
20 | mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8') |
21 | } |
22 | |
23 | function message_content (msg) { |
24 | if (msg.value.content.type !== 'about') return |
25 | |
26 | var { content: about, author: authorId } = msg.value |
27 | var { about: aboutId, name, image, description } = about |
28 | |
29 | if (!aboutId) return null |
30 | |
31 | return h('About', [ |
32 | Name({ aboutId, authorId, name }), |
33 | Image({ aboutId, authorId, image }), |
34 | Description({ aboutId, authorId, description }) |
35 | ]) |
36 | } |
37 | |
38 | function message_content_mini (msg) { |
39 | if (msg.value.content.type !== 'about') return |
40 | |
41 | var { content: about, author: authorId } = msg.value |
42 | var { about: aboutId, name, image, description } = about |
43 | |
44 | if (!aboutId) return null |
45 | if (image || description) return null |
46 | |
47 | return h('About', Name({ aboutId, authorId, name })) |
48 | } |
49 | |
50 | |
51 | function Name ({ aboutId, authorId, name }) { |
52 | if (!name) return null |
53 | return h('section -name', [ |
54 | h('header', when(authorId === aboutId, |
55 | 'self-identifies as', |
56 | ['identifies ', targetLink(aboutId), ' as'] |
57 | )), |
58 | h('section', h( |
59 | 'a -name', |
60 | { href: `#${aboutId}` }, |
61 | name |
62 | )) |
63 | ]) |
64 | } |
65 | |
66 | function Image ({ aboutId, authorId, image }) { |
67 | if (!image) return null |
68 | return h('section -image', [ |
69 | h('header', when(authorId === aboutId, |
70 | 'self-portrays as', |
71 | ['portrays ', targetLink(aboutId), ' as'] |
72 | )), |
73 | h('section', h( |
74 | 'a -image', |
75 | { href: `#${aboutId}` }, |
76 | h('img', { src: api.blob_url(image) }) |
77 | )) |
78 | ]) |
79 | } |
80 | |
81 | function Description ({ aboutId, authorId, description }) { |
82 | if (!description) return null |
83 | return h('section -description', [ |
84 | h('header', when(authorId === aboutId, |
85 | 'self-describes as', |
86 | ['describes ', targetLink(aboutId), ' as'] |
87 | )), |
88 | h('section', api.markdown(description)) |
89 | ]) |
90 | } |
91 | } |
92 | |
93 | function targetLink (aboutId) { |
94 | const content = aboutId.slice(0, 9) + '...' |
95 | return h( |
96 | 'a -target', |
97 | { href: `#${aboutId}` }, |
98 | content |
99 | ) |
100 | } |
101 |
Built with git-ssb-web