Files: a3f728451ea4f496b80c42138d030295e43de153 / message / html / markdown.js
1604 bytesRaw
1 | const renderer = require('ssb-markdown') |
2 | const h = require('mutant/h') |
3 | const ref = require('ssb-ref') |
4 | const nest = require('depnest') |
5 | var htmlEscape = require('html-escape') |
6 | |
7 | exports.needs = nest({ |
8 | 'blob.sync.url': 'first', |
9 | 'emoji.sync.url': 'first' |
10 | }) |
11 | |
12 | exports.gives = nest('message.html.markdown') |
13 | |
14 | exports.create = function (api) { |
15 | return nest('message.html.markdown', markdown) |
16 | |
17 | function markdown (content) { |
18 | if (typeof content === 'string') { content = {text: content} } |
19 | // handle patchwork style mentions and custom emoji. |
20 | var mentions = {} |
21 | var emojiMentions = {} |
22 | if (Array.isArray(content.mentions)) { |
23 | content.mentions.forEach(function (link) { |
24 | if (link && link.name && link.link) { |
25 | if (link.emoji) emojiMentions[link.name] = link.link |
26 | else mentions['@' + link.name] = link.link |
27 | } |
28 | }) |
29 | } |
30 | |
31 | var md = h('div', {className: 'Markdown'}) |
32 | md.innerHTML = renderer.block(content.text, { |
33 | emoji: (emoji) => { |
34 | var url = emojiMentions[emoji] |
35 | ? api.blob.sync.url(emojiMentions[emoji]) |
36 | : api.emoji.sync.url(emoji) |
37 | return renderEmoji(emoji, url) |
38 | }, |
39 | toUrl: (id) => { |
40 | if (ref.isBlob(id)) return api.blob.sync.url(id) |
41 | return mentions[id] || id |
42 | }, |
43 | imageLink: (id) => id |
44 | }) |
45 | |
46 | return md |
47 | } |
48 | |
49 | function renderEmoji (emoji, url) { |
50 | if (!url) return ':' + emoji + ':' |
51 | return ` |
52 | <img |
53 | src="${htmlEscape(url)}" |
54 | alt=":${htmlEscape(emoji)}:" |
55 | title=":${htmlEscape(emoji)}:" |
56 | class="emoji" |
57 | > |
58 | ` |
59 | } |
60 | } |
61 |
Built with git-ssb-web