git ssb

16+

cel / patchfoo



Tree: 7cbc5c587105a7add08d250458303c47bcd0e57a

Files: 7cbc5c587105a7add08d250458303c47bcd0e57a / lib / markdown-inline.js

2296 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 ? code : escape(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}
34
35function escape (text) {
36 return text
37 .replace(/&/g, '&')
38 .replace(/</g, '&lt;')
39 .replace(/>/g, '&gt;')
40 .replace(/"/g, '&quot;')
41 .replace(/\n+/g, ' ')
42}
43
44function shortenIfLink (text) {
45 return (u.ssbRefRegex.test(text.trim())) ? text.slice(0, 8) : text
46}
47
48module.exports = function(text) {
49 return marked(''+(text||''), {renderer: inlineRenderer, emoji: false})
50}
51

Built with git-ssb-web