I am using the bookdown package in R to publish a nicely looking "book" online. I want to include the date that the book was last generated into the footer.html, so that this is visible on each page of the book. I've searched for an answer many times, but no solution that I've found has worked so far, and I don't know whether this is due to my limited knowledge of HTML/JS or due to peculiarities in the bookdown package.
Content of my current files:
Most important yml fields in index.Rmd (located in the root of my project folder, following bookdown's guidance):
---
title: "Title of book"
author: "Author of book"
date: 'r format(Sys.time(), "%d %B %Y")'
site: bookdown::bookdown_site
description: |
Some description
output:
html_document:
toc: true
toc_depth: 4
toc_float: true
---
Most important content in _output.yml (also located in the root of my project folder, see Rmd guidance):
bookdown::gitbook:
split_by: section
css: assets/style.css
includes:
in_header: assets/header.html
after_body: assets/footer.html # The footer is loaded on each page
Simplification of footer.html, located in the assets folder in my project:
<div class = "footer">
<footer>
<small>
Last updated:
<!-- This does not work: results in "undefined" -->
<script type="text/javascript">
var date = document.querySelector('meta[name="date"]').content;
document.write(date);
</script>
</small>
</footer>
</div>
The problem
I want to access the date variable that was created in the index.Rmd yaml. Through Inspect on the published web page, it looks like date is written into the metadata of the site as it says:
<meta name = "date" content = "2022-08-08"> == $0. Therefore, I assumed that I should access the date via the head metadata.
I've tried to use some javascript to extract the "date" metadata from the head (see the footer.html content I pasted above). Strangely, I can access other metadata, such as the description and author. These are also defined in the yaml in index.Rmd. But all of these other fields I can access, just not the date (results in "undefined"). What am I doing wrong? Is there another way I can (set and) access the date in index.Rmd?
Update
After regenerating the documents, date is not even in the metadata anymore, whereas other fields from the index.Rmd are (e.g., author, description, github-repo). Why is date the only variable from index.Rmd that is not written in the metadata? Is this a bookdown issue?
Here's a copy of the content (NB without <link>s to stylesheets):
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Title of book</title>
<meta name="description" content="<p>Some description</p>" />
<meta name="generator" content="bookdown 0.28 and GitBook 2.6.7" />
<meta property="og:title" content="Title of book" />
<meta property="og:type" content="book" />
<meta property="og:image" content="image.png" />
<meta property="og:description" content="<p>Some description</p>" />
<meta name="github-repo" content="account/repo" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Title of book" />
<meta name="twitter:description" content="<p>Some description</p>" />
<meta name="twitter:image" content="img.png" />
<meta name="author" content="Author of book" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<script src="libs/accessible-code-block-0.0.1/empty-anchor.js"></script>
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<script src="book.js"></script>
</head>