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