Files: 7cbc5c587105a7add08d250458303c47bcd0e57a / lib / markdown-inline.js
2296 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 false } |
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 ? code : escape(code) } |
13 | inlineRenderer.blockquote = function(quote) { return unquote(quote) } |
14 | inlineRenderer.html = function(html) { return false } |
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 | } |
34 | |
35 | function escape (text) { |
36 | return text |
37 | .replace(/&/g, '&') |
38 | .replace(/</g, '<') |
39 | .replace(/>/g, '>') |
40 | .replace(/"/g, '"') |
41 | .replace(/\n+/g, ' ') |
42 | } |
43 | |
44 | function shortenIfLink (text) { |
45 | return (u.ssbRefRegex.test(text.trim())) ? text.slice(0, 8) : text |
46 | } |
47 | |
48 | module.exports = function(text) { |
49 | return marked(''+(text||''), {renderer: inlineRenderer, emoji: false}) |
50 | } |
51 |
Built with git-ssb-web