aboutsummaryrefslogtreecommitdiff
path: root/lib/markdown-inline.js
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-01-30 18:24:49 -0800
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-01-30 18:24:49 -0800
commit7a3b67c49b20c9063a696b8fb7dc00e541855693 (patch)
tree67cbb86265898fa7cc14ebb564c9a9f1ad872221 /lib/markdown-inline.js
downloadpatchfoo-7a3b67c49b20c9063a696b8fb7dc00e541855693.tar.gz
patchfoo-7a3b67c49b20c9063a696b8fb7dc00e541855693.zip
Init
Diffstat (limited to 'lib/markdown-inline.js')
-rw-r--r--lib/markdown-inline.js49
1 files changed, 49 insertions, 0 deletions
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(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#39;/g, '\'')
+}
+
+function escape (text) {
+ return text
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;')
+ .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})
+}