Setting up postref Shortcode for Remote Blog

[This article was first published on R | datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I ran into a bit of a snag when updating a {blogdown} site. Suddenly, inexplicably, the images were no longer present. When I investigated I found that some HTML <img> tags looked like this:

<img src="{{< blogdown/postref >}}index_files/figure-html/unnamed-chunk-3-1.png">

Obviously there’s something wrong there! And that {{< blogdown/postref >}} looks like a Hugo shortcode.

After some investigation I found that {blogdown} now requires a layouts/shortcodes/blogdown/postref.html file which defines the required shortcode.

The default content for postref.html is

{{ .Page.Permalink }}

With this in place the image path looks like this:

<img src="//localhost:4321/blog/test/index_files/figure-html/unnamed-chunk-3-1.png">

This works perfectly if you are building the site locally. Since you’re building and viewing the post on localhost you’re able to see the image.

If, however, you are using a remote RStudio Server then this image reference won’t work (because your localhost is not the same as the localhost at which the image is being served).

What’s the problem? Well, .Page.Permalink is generating an absolute URL for the resource. If we want this to work from a remote machine then we’ll need to use a relative URL. Replace the contents of postref.html with this:

{{ .Page.RelPermalink }}

The result:

<img src="../../blog/test/index_files/figure-html/unnamed-chunk-3-1.png">

I burned a couple of hours figuring this out, hopefully this will save you some time.

Epilogue

Not too long after this post was published I got a very helpful response from Yihui Xie.

The problem is now fixed (see issue on GitHub repository). Kudos and thanks to the incredibly responsive team of Alison Presmanes Hill, Christophe Dervieux and Yihui Xie.

To leave a comment for the author, please follow the link and comment on their blog: R | datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)