git ssb

16+

cel / patchfoo



Tree: 80d6f2d0c52876f05da71c887266523c37645bf1

Files: 80d6f2d0c52876f05da71c887266523c37645bf1 / lib / markdown-inline.js

2172 bytesRaw
1var marked = require('ssb-marked')
2var u = require('./util')
3
4// based on ssb-markdown, which is Copyright (c) 2016 Dominic Tarr, MIT License
5
6var inlineRenderer = new marked.Renderer()
7
8// inline renderer just spits out the text of links and images
9inlineRenderer.urltransform = function (url) { return false }
10inlineRenderer.link = function (href, title, text) { return unquote(shortenIfLink(text)) }
11inlineRenderer.image = function (href, title, text) { return unquote(shortenIfLink(text)) }
12inlineRenderer.code = function(code, lang, escaped) { return escaped ? unquote(code) : code }
13inlineRenderer.blockquote = function(quote) { return unquote(quote) }
14inlineRenderer.html = function(html) { return false }
15inlineRenderer.heading = function(text, level, raw) { return unquote(text)+' ' }
16inlineRenderer.hr = function() { return ' --- ' }
17inlineRenderer.br = function() { return ' ' }
18inlineRenderer.list = function(body, ordered) { return unquote(body) }
19inlineRenderer.listitem = function(text) { return '- '+unquote(text) }
20inlineRenderer.paragraph = function(text) { return unquote(text)+' ' }
21inlineRenderer.table = function(header, body) { return unquote(header + ' ' + body) }
22inlineRenderer.tablerow = function(content) { return unquote(content) }
23inlineRenderer.tablecell = function(content, flags) { return unquote(content) }
24inlineRenderer.strong = function(text) { return unquote(text) }
25inlineRenderer.em = function(text) { return unquote(text) }
26inlineRenderer.codespan = function(text) { return unquote(text) }
27inlineRenderer.del = function(text) { return unquote(text) }
28inlineRenderer.mention = function(preceding, id) { return shortenIfLink(unquote((preceding||'') + id)) }
29inlineRenderer.hashtag = function(preceding, tag) { return unquote((preceding||'') + tag) }
30
31function unquote (text) {
32 return text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, '\'')
33 .replace(/>/g, '>')
34 .replace(/&lt;/g, '<')
35}
36
37function shortenIfLink (text) {
38 return (u.ssbRefRegex.test(text.trim())) ? text.slice(0, 8) : text
39}
40
41module.exports = function(text) {
42 return marked(''+(text||''), {renderer: inlineRenderer, emoji: false})
43}
44

Built with git-ssb-web