diff options
-rw-r--r-- | lib/render.js | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/lib/render.js b/lib/render.js index cca56f0..13d5d13 100644 --- a/lib/render.js +++ b/lib/render.js @@ -23,13 +23,13 @@ MdRenderer.prototype.urltransform = function (href) { return this.render.toUrl(href) } -MdRenderer.prototype.image = function (href, title, text) { - href = this.render.imageUrl(href) +MdRenderer.prototype.image = function (ref, title, text) { + var href = this.render.imageUrl(href) var name = text || title if (name) href += '?name=' + encodeURIComponent(name) return h('img', { src: href, - alt: text, + alt: this.render.getImageAlt(ref, text), title: title || undefined }).outerHTML } @@ -93,15 +93,19 @@ function fixSymbols(str) { Render.prototype.markdown = function (text, mentions) { if (!text) return '' var mentionsObj = this._mentions = {} + var mentionsByLink = this._mentionsByLink = {} if (Array.isArray(mentions)) mentions.forEach(function (link) { if (!link) return else if (link.name) mentionsObj['@' + link.name] = link.link else if (link.host === 'http://localhost:7777') mentionsObj[link.href] = link.link + if (link.link) + mentionsByLink[link.link] = link }) var out = marked(String(text), this.markedOpts) delete this._mentions + delete this._mentionsByLink return out //fixSymbols(out) } @@ -111,6 +115,22 @@ Render.prototype.imageUrl = function (ref) { return this.opts.img_base + ref } +Render.prototype.getImageAlt = function (id, fallback) { + var link = this._mentionsByLink[id] + if (!link) return fallback + var name = link.name || fallback + return name + + (link.size != null ? ' (' + this.formatSize(link.size) + ')' : '') +} + +Render.prototype.formatSize = function (size) { + if (size < 1024) return size + ' B' + size /= 1024 + if (size < 1024) return size.toFixed(2) + ' KB' + size /= 1024 + return size.toFixed(2) + ' MB' +} + Render.prototype.toUrl = function (href) { if (!href) return href var mentions = this._mentions |