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