diff --git a/R/hooks-html.R b/R/hooks-html.R
index 03bfdce9e7..cf2acead62 100644
--- a/R/hooks-html.R
+++ b/R/hooks-html.R
@@ -16,7 +16,9 @@ hook_plot_html = function(x, options) {
d2 = if (plot2) paste0('', if (out_format('html')) '
')
paste0(
d1, .img.tag(
- .upload.url(x), options$out.width, options$out.height, .img.cap(options),
+ .upload.url(x), options$out.width, options$out.height,
+ .img.cap(options),
+ .img.alt(options),
paste(c(options$out.extra, 'class="plot"'), collapse = ' ')
), d2, '\n'
)
@@ -37,9 +39,9 @@ hook_animation = function(options) {
paste(c(sprintf('width="%s"', w), sprintf('height="%s"', h), extra), collapse = ' ')
}
-.img.tag = function(src, w, h, caption, extra) {
+.img.tag = function(src, w, h, caption, alt, extra) {
caption = if (length(caption) == 1 && caption != '') {
- paste0('title="', caption, '" alt="', caption, '" ')
+ paste0('title="', caption, '" alt="', alt, '" ')
}
tag = if (grepl('[.]pdf$', src, ignore.case = TRUE)) {
extra = c(extra, 'type="application/pdf"')
@@ -52,11 +54,13 @@ hook_animation = function(options) {
}
.img.cap = function(options, alt = FALSE) {
- cap = options$fig.cap %n% {
+ if (alt) {
+ cap = escape_html(options$fig.cap %n% options$fig.alt)
+ }
+ cap = cap %n% {
if (is.null(pandoc_to())) sprintf('plot of chunk %s', options$label) else ''
}
if (length(cap) == 0) cap = ''
- if (alt) return(escape_html(options$fig.alt %n% cap))
if (is_blank(cap)) return(cap)
paste0(create_label(
options$fig.lp, options$label,
@@ -64,6 +68,20 @@ hook_animation = function(options) {
), cap)
}
+.img.alt <- function(options) {
+ alt <- options$fig.alt %n% {
+ if (is.null(pandoc_to())) sprintf("plot of chunk %s", options$label) else ""
+ }
+ if (length(alt) == 0) alt <- ""
+ if (is_blank(alt)) {
+ return(alt)
+ }
+ paste0(create_label(
+ options$fig.lp, options$label,
+ if (options$fig.num > 1L && options$fig.show == "asis") c("-", options$fig.cur)
+ ), alt)
+}
+
# a wrapper to upload an image and return the URL
.upload.url = function(x) {
opts_knit$get('upload.fun')(x)
diff --git a/R/hooks-md.R b/R/hooks-md.R
index 28cc121373..8de63f2860 100644
--- a/R/hooks-md.R
+++ b/R/hooks-md.R
@@ -43,8 +43,8 @@ hook_plot_md_base = function(x, options) {
if (options$fig.show == 'animate') return(hook_plot_html(x, options))
base = opts_knit$get('base.url') %n% ''
- cap = .img.cap(options)
- alt = .img.cap(options, alt = TRUE)
+ cap = .img.cap(options, alt = TRUE)
+ alt = .img.alt(options)
w = options[['out.width']]; h = options[['out.height']]
s = options$out.extra; a = options$fig.align
@@ -84,10 +84,12 @@ hook_plot_md_base = function(x, options) {
} else {
paste0(d1, img, if (plot2) paste0('\n', d2, '\n
'))
}
- } else add_link(.img.tag(
- .upload.url(x), w, h, alt,
- c(s, sprintf('style="%s"', css_align(a)))
- ))
+ } else {
+ add_link(.img.tag(
+ .upload.url(x), w, h, cap, alt,
+ c(s, sprintf('style="%s"', css_align(a)))
+ ))
+ }
}
hook_plot_md_pandoc = function(x, options) {