]> git.ardis.lu - ardislu.dev/commitdiff
Insert post timestamp on article page
authorArdis Lu <ardisglu@gmail.com>
Fri, 10 Mar 2023 04:23:15 +0000 (20:23 -0800)
committerArdis Lu <ardisglu@gmail.com>
Fri, 10 Mar 2023 04:23:15 +0000 (20:23 -0800)
script.js

index 828cdda44417fb82666a2b8a5f498224129cae3a..c5c6a48037ccb17da1baa65664c1af80f36d71e2 100644 (file)
--- a/script.js
+++ b/script.js
@@ -95,6 +95,14 @@ async function fetchPostContent(id) {
   const url = `${proxyUrl}?https://docs.googleapis.com/v1/documents/${id}`;
   const googleDoc = await fetch(url).then(r => r.json());
 
+  // Insert article creation date and last updated date immediately after title (hardcoded index of 2)
+  const post = metadata.get(location.pathname);
+  const postTimestamp = post.createdDate.toISOString() === post.updatedDate.toISOString() ?
+    `<time datetime="${post.createdDate.toISOString()}">${post.createdDate.toLocaleDateString()}</time>` :
+    `<time datetime="${post.createdDate.toISOString()}">${post.createdDate.toLocaleDateString()}</time> — Updated <time datetime="${post.updatedDate.toISOString()}">${post.updatedDate.toLocaleDateString()}</time>`;
+  const subtitle = `<p class="subtitle">${postTimestamp}<p>\n\n`; // Trailing newlines are required to prevent marked from interpreting the next line as HTML
+  googleDoc.body.content.splice(2, 0, { paragraph: { elements: [{ textRun: { content: subtitle } }], paragraphStyle: { namedStyleType: 'SUBTITLE' } } });
+
   const mapping = {
     TITLE: '# ',
     SUBTITLE: '',
This page took 0.036526 seconds and 4 git commands to generate.