From c8f96e2696d4d538e2b5e9afdec945c3091ce03c Mon Sep 17 00:00:00 2001 From: cel Date: Thu, 28 May 2020 11:34:25 -0400 Subject: Truncate long link texts --- lib/render.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib/render.js') diff --git a/lib/render.js b/lib/render.js index a39ba73..57cc3b5 100644 --- a/lib/render.js +++ b/lib/render.js @@ -15,6 +15,18 @@ var Highlight = require('highlight.js') var md = require('ssb-markdown') var Diff = require('diff') +var maxWordLength = 80 +var truncateWordsRe = new RegExp('[^\\s<>&"]{' + (maxWordLength+1) + ',}') +function truncateWord(word) { + var len = maxWordLength - 5 // "…[##]" + var truncatedLength = word.length - len + return word.substr(0, len) + '…[' + truncatedLength + ']' +} +function truncateWords(text) { + // truncate long words to avoid horizontal scrollbar + return text.replace(truncateWordsRe, truncateWord) +} + module.exports = Render function MdRenderer(render) { @@ -62,6 +74,7 @@ MdRenderer.prototype.link = function (ref, title, text) { href: href !== false ? hrefToken : undefined, title: title || undefined }) + text = truncateWords(text) // text is already html-escaped a.innerHTML = text var html = a.outerHTML -- cgit v1.2.3