Browse Source

improved reply-to link rendering with an inline markdown mode

Paul Frazee 5 years ago
parent
commit
17dad73802
2 changed files with 40 additions and 7 deletions
  1. 1
    1
      ui/lib/com/message.js
  2. 39
    6
      ui/lib/markdown.js

+ 1
- 1
ui/lib/com/message.js View File

@@ -384,7 +384,7 @@ function fetchAndRenderReplyLink (msg) {
384 384
 
385 385
     var text = '@' + com.userName(msg.author)
386 386
     if (msg.content.text && typeof msg.content.text == 'string')
387
-      text += ': ' + msg.content.text
387
+      text += ': ' + markdown.inline(msg.content.text)
388 388
     if (text.length > 60)
389 389
       text = text.slice(0, 57) + '...'
390 390
 

+ 39
- 6
ui/lib/markdown.js View File

@@ -4,10 +4,11 @@ var marked = require('ssb-marked')
4 4
 var ssbref = require('ssb-ref')
5 5
 var mlib   = require('ssb-msgs')
6 6
 
7
-var renderer = new marked.Renderer();
7
+var blockRenderer = new marked.Renderer()
8
+var inlineRenderer = new marked.Renderer()
8 9
 
9 10
 // override to only allow external links or hashes, and correctly link to ssb objects
10
-renderer.urltransform = function (url) {
11
+blockRenderer.urltransform = function (url) {
11 12
   var c = url.charAt(0)
12 13
   var hasSigil = (c == '@' || c == '&' || c == '%')
13 14
 
@@ -48,7 +49,7 @@ renderer.urltransform = function (url) {
48 49
 }
49 50
 
50 51
 // override to make http/s links external
51
-renderer.link = function(href, title, text) {
52
+blockRenderer.link = function(href, title, text) {
52 53
   href = this.urltransform(href)
53 54
   var out
54 55
   if (href !== false)
@@ -68,7 +69,7 @@ renderer.link = function(href, title, text) {
68 69
 };
69 70
 
70 71
 // override to support <video> tags (HACK)
71
-renderer.image  = function (href, title, text) {
72
+blockRenderer.image  = function (href, title, text) {
72 73
   href = href.replace(/^&amp;/, '&')
73 74
   if (ssbref.isLink(href)) {
74 75
     if ((''+text).indexOf('.webm') >= 0) {
@@ -89,6 +90,34 @@ renderer.image  = function (href, title, text) {
89 90
   return text
90 91
 }
91 92
 
93
+// inline renderer just spits out the text of links and images
94
+inlineRenderer.urltransform = function (url) { return false }
95
+inlineRenderer.link = function (href, title, text) { return unquote(text) }
96
+inlineRenderer.image  = function (href, title, text) {
97
+  if (text == 'webcam.webm') return '' // :HACK: webcam embed title, just dont render
98
+  return unquote(text)
99
+}
100
+inlineRenderer.code = function(code, lang, escaped) { return unquote(code) }
101
+inlineRenderer.blockquote = function(quote) { return unquote(quote) }
102
+inlineRenderer.html = function(html) { return false }
103
+inlineRenderer.heading = function(text, level, raw) { return unquote(text) }
104
+inlineRenderer.hr = function() { return ' --- ' }
105
+inlineRenderer.br = function() { return ' ' }
106
+inlineRenderer.list = function(body, ordered) { return unquote(body) }
107
+inlineRenderer.listitem = function(text) { return '- '+unquote(text) }
108
+inlineRenderer.paragraph = function(text) { return unquote(text)+' ' }
109
+inlineRenderer.table = function(header, body) { return unquote(header + ' ' + body) }
110
+inlineRenderer.tablerow = function(content) { return unquote(content) }
111
+inlineRenderer.tablecell = function(content, flags) { return unquote(content) }
112
+inlineRenderer.strong = function(text) { return unquote(text) }
113
+inlineRenderer.em = function(text) { return unquote(text) }
114
+inlineRenderer.codespan = function(text) { return unquote(text) }
115
+inlineRenderer.del = function(text) { return unquote(text) }
116
+inlineRenderer.mention = function(preceding, id) { return unquote((preceding||'') + id) }
117
+function unquote (text) {
118
+  return text.replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#39;/g, '\'')
119
+}
120
+
92 121
 marked.setOptions({
93 122
   gfm: true,
94 123
   mentions: true,
@@ -99,8 +128,8 @@ marked.setOptions({
99 128
   smartLists: true,
100 129
   smartypants: false,
101 130
   emoji: renderEmoji,
102
-  renderer: renderer
103
-});
131
+  renderer: blockRenderer
132
+})
104 133
 
105 134
 exports.block = function(text, mentionNames) {
106 135
   if (mentionNames && mentionNames.key && mentionNames.value) {
@@ -119,6 +148,10 @@ exports.block = function(text, mentionNames) {
119 148
   return marked(''+(text||''), { mentionNames: mentionNames })
120 149
 }
121 150
 
151
+exports.inline = function(text) {
152
+  return marked(''+(text||''), { renderer: inlineRenderer })
153
+}
154
+
122 155
 var emojiRegex = /(\s|>|^)?:([A-z0-9_]+):(\s|<|$)/g;
123 156
 exports.emojis = function (str) {
124 157
   return str.replace(emojiRegex, function(full, $1, $2, $3) {

Loading…
Cancel
Save