Files: 3f6f0c96bdbbd8307fb9e5459b6857870001abfc / modules_basic / markdown.js
1114 bytesRaw
1 | var markdown = require('ssb-markdown') |
2 | var h = require('hyperscript') |
3 | var ref = require('ssb-ref') |
4 | |
5 | exports.needs = { |
6 | blob_url: 'first', |
7 | emoji_url: 'first' |
8 | } |
9 | |
10 | exports.gives = 'markdown' |
11 | |
12 | exports.create = function (api) { |
13 | |
14 | function renderEmoji(emoji) { |
15 | var url = api.emoji_url(emoji) |
16 | if (!url) return ':' + emoji + ':' |
17 | return '<img src="' + encodeURI(url) + '"' |
18 | + ' alt=":' + escape(emoji) + ':"' |
19 | + ' title=":' + escape(emoji) + ':"' |
20 | + ' class="emoji">' |
21 | } |
22 | |
23 | return function (content) { |
24 | if('string' === typeof content) |
25 | content = {text: content} |
26 | //handle patchwork style mentions. |
27 | var mentions = {} |
28 | if(Array.isArray(content.mentions)) |
29 | content.mentions.forEach(function (link) { |
30 | if(link.name) mentions["@"+link.name] = link.link |
31 | }) |
32 | |
33 | var md = h('div.markdown') |
34 | md.innerHTML = markdown.block(content.text, { |
35 | emoji: renderEmoji, |
36 | toUrl: (id) => { |
37 | if(ref.isBlob(id)) return api.blob_url(id) |
38 | return '#'+(mentions[id]?mentions[id]:id) |
39 | }, |
40 | imageLink: (id) => '#' + id |
41 | }) |
42 | |
43 | return md |
44 | |
45 | } |
46 | } |
47 | |
48 |
Built with git-ssb-web