#' An RStudio addin for inserting newest image into a blogdown post
#'
#' insert.thumbnail()
insert.thumbnail <- function(){
ctx = rstudioapi::getSourceEditorContext()
if (ctx$path == '') stop(
'Please select the blog post source file before using this addin', call. = FALSE
)
ctx_ext = tolower(xfun::file_ext(ctx$path))
path = normalizePath(ctx$path)
imgdir = paste0(xfun::sans_ext(basename(path)), '_files')
datapath = paste0("/post/",imgdir,"/figure-html/thumbnail-1.png")
###
txt = ctx$contents
res = blogdown:::split_yaml_body(txt)
yml = res$yaml_list
rng = res$yaml_range
if (!"thumbnail" %in% names(yml)){
rstudioapi::setSelectionRanges(list(c(rng[1] + 1, 1, rng[2], 1)))
slct = rstudioapi::getSourceEditorContext()$selection[[1]]
yml$thumbnail <- datapath
seq_keys = Filter(function(key) {
identical(attr(yml[[key]], 'yml_type'), 'seq')
}, names(yml))
seq_keys = unique(c(seq_keys, 'categories', 'tags'))
for (i in seq_keys) yml[[i]] = if (length(yml[[i]]) > 0) as.list(yml[[i]])
if (!getOption('blogdown.yaml.empty', TRUE)) yml = blogdown:::filter_list(yml)
rstudioapi::modifyRange(
slct$range, blogdown:::as.yaml(yml, .trim_ws = FALSE)
)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.