Files: 7478b5388306a177f85de2b6a0c3bcedf8635c87 / modules_basic / markdown.js
1085 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: function (id) { |
37 | if(ref.isBlob(id)) return api.blob_url(id) |
38 | return '#'+(mentions[id]?mentions[id]:id) |
39 | } |
40 | }) |
41 | |
42 | return md |
43 | |
44 | } |
45 | } |
46 | |
47 |
Built with git-ssb-web