aboutsummaryrefslogtreecommitdiff
path: root/lib/markdown-inline.js
blob: 0d4b89f7622cc8eb0a2a2d36ede04bc1020d7d43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var marked = require('ssb-marked')
var u = require('./util')
var unquote = u.unescapeHTML

// based on ssb-markdown, which is Copyright (c) 2016 Dominic Tarr, MIT License

var inlineRenderer = new marked.Renderer()

// inline renderer just spits out the text of links and images
inlineRenderer.urltransform = function (url) { return url }
inlineRenderer.link = function (href, title, text) { return unquote(shortenIfLink(text)) }
inlineRenderer.image  = function (href, title, text) { return unquote(shortenIfLink(text)) }
inlineRenderer.code = function(code, lang, escaped) { return escaped ? unquote(code) : code }
inlineRenderer.blockquote = function(quote) { return unquote(quote) }
inlineRenderer.html = function(html) { return html }
inlineRenderer.heading = function(text, level, raw) { return unquote(text)+' ' }
inlineRenderer.hr = function() { return ' --- ' }
inlineRenderer.br = function() { return ' ' }
inlineRenderer.list = function(body, ordered) { return unquote(body) }
inlineRenderer.listitem = function(text) { return '- '+unquote(text) }
inlineRenderer.paragraph = function(text) { return unquote(text)+' ' }
inlineRenderer.table = function(header, body) { return unquote(header + ' ' + body) }
inlineRenderer.tablerow = function(content) { return unquote(content) }
inlineRenderer.tablecell = function(content, flags) { return unquote(content) }
inlineRenderer.strong = function(text) { return unquote(text) }
inlineRenderer.em = function(text) { return unquote(text) }
inlineRenderer.codespan = function(text) { return unquote(text) }
inlineRenderer.del = function(text) { return unquote(text) }
inlineRenderer.mention = function(preceding, id) { return shortenIfLink(unquote((preceding||'') + id)) }
inlineRenderer.hashtag = function(preceding, tag) { return unquote((preceding||'') + tag) }

function shortenIfLink (text) {
  return (u.ssbRefRegex.test(text.trim())) ? text.slice(0, 8) : text
}

module.exports = function(text) {
  return marked(''+(text||''), {renderer: inlineRenderer, emoji: false})
}