git ssb

16+

Dominic / patchbay



Tree: 83f0a729646d2a27162cb77fe27d448dc3f62568

Files: 83f0a729646d2a27162cb77fe27d448dc3f62568 / modules_basic / markdown.js

1265 bytesRaw
1const renderer = require('ssb-markdown')
2const fs = require('fs')
3const h = require('../h')
4const ref = require('ssb-ref')
5
6exports.needs = {
7 blob_url: 'first',
8 emoji_url: 'first'
9}
10
11exports.gives = {
12 markdown: true,
13 mcss: true
14}
15
16exports.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