aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-05-01 20:26:55 -1000
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-05-01 20:35:09 -1000
commit271a1375b5d1d41739547d0349003dea91b33593 (patch)
tree2461bb82093505bf9de04ec1fb7bf93ac01c34f1
parent3d93a6b19ffbce96e6bf66e1b91a9b99e561bece (diff)
downloadpatchfoo-271a1375b5d1d41739547d0349003dea91b33593.tar.gz
patchfoo-271a1375b5d1d41739547d0349003dea91b33593.zip
Add size to image alt text
-rw-r--r--lib/render.js26
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