From 7a3b67c49b20c9063a696b8fb7dc00e541855693 Mon Sep 17 00:00:00 2001 From: cel Date: Mon, 30 Jan 2017 18:24:49 -0800 Subject: Init --- lib/markdown-inline.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lib/markdown-inline.js (limited to 'lib/markdown-inline.js') diff --git a/lib/markdown-inline.js b/lib/markdown-inline.js new file mode 100644 index 0000000..2f1e696 --- /dev/null +++ b/lib/markdown-inline.js @@ -0,0 +1,49 @@ +var marked = require('ssb-marked') +var u = require('./util') + +// 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 false } +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 ? code : escape(code) } +inlineRenderer.blockquote = function(quote) { return unquote(quote) } +inlineRenderer.html = function(html) { return false } +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)) } + +function unquote (text) { + return text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, '\'') +} + +function escape (text) { + return text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/\n+/g, ' ') +} + +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}) +} -- cgit v1.2.3