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