JS updates position of TOC for Rmarkdown file in blogdown

[This article was first published on R | Bangyou Zheng, 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.

The TOC is rendered underneath when using blogdown 0.20 to write Rmarkdown file.

However, the current version of hugo academic theme is rendered TOC at the right hand side and always keep on the screen which would be more useful.

A simple solution is to adjust the position of TOC using js.

  • Update plugins_js in the configuration file config\_default\params.toml into plugins_js = [custom].
  • Create a custom.js under \assets\js\ folder.
  • Add the js script below into custom.js.
$(document).ready(function(){
	
	// Find TOC
	var toc = $("#TOC");
	if (toc.length === 0) {
	  return;
	}

	// Find sidebar
	var sidebar = $("div.flex-xl-nowrap")
	if (sidebar.length !== 1) {
	   return;
	}
	// Generate html
	var html = '<div class="d-none d-xl-block col-xl-2 docs-toc">' + 
	'<ul class="nav toc-top"><li><a href="#" id="back_to_top" class="docs-toc-title">Contents</a></li></ul>' +
	'<nav id="TableOfContents" class="nav flex-column">' + 
	toc.html() +
	'</nav></div>';
	sidebar.append(html);
	toc.html("")
});

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

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)