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