From 91a7f69ae0302372db20f0247adbf5ca6eb43b6a Mon Sep 17 00:00:00 2001
From: Cory Dransfeldt <hi@coryd.dev>
Date: Mon, 8 Jan 2024 12:37:31 -0800
Subject: [PATCH] fix: book data

---
 package-lock.json  |  6 +++---
 src/_data/books.js | 34 ++++++++++++++++------------------
 2 files changed, 19 insertions(+), 21 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 3a9d5ee8..569ae796 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -954,9 +954,9 @@
       "dev": true
     },
     "node_modules/@types/node": {
-      "version": "20.10.6",
-      "resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.6.tgz",
-      "integrity": "sha512-Vac8H+NlRNNlAmDfGUP7b5h/KA+AtWIzuXy0E6OyP8f1tCLYAtPvKRRDJjAPqhpCb0t6U2j7/xqAuLEebW2kiw==",
+      "version": "20.10.7",
+      "resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.7.tgz",
+      "integrity": "sha512-fRbIKb8C/Y2lXxB5eVMj4IU7xpdox0Lh8bUPEdtLysaylsml1hOOx1+STloRs/B9nf7C6kPRmmg/V7aQW7usNg==",
       "dev": true,
       "dependencies": {
         "undici-types": "~5.26.4"
diff --git a/src/_data/books.js b/src/_data/books.js
index f9eaccc0..c89c0c9a 100644
--- a/src/_data/books.js
+++ b/src/_data/books.js
@@ -18,26 +18,24 @@ export default async function () {
     .then((html) => {
       const DOM = new JSDOM(html)
       const doc = DOM.window.document
-      const bookCount = doc.querySelectorAll('.md\\:block .book-pane-content').length
-      const titles = doc.querySelectorAll('.md\\:block .book-title-author-and-series h3 > a')
-      const authors = doc.querySelectorAll(
-        '.md\\:block .book-title-author-and-series h3 p:last-of-type > a'
-      )
+      const bookCount = doc.querySelectorAll('.book-pane-content').length
+      const titles = doc.querySelectorAll('.book-title-author-and-series h3 > a')
+      const authors = doc.querySelectorAll('.book-title-author-and-series h3 p:last-of-type > a')
       const images = doc.querySelectorAll('.md\\:block .book-cover img')
       const urls = doc.querySelectorAll('.md\\:block .book-cover a')
       const percentages = doc.querySelectorAll('.md\\:block .progress-tracker-pane .font-semibold')
-      const dates = doc.querySelectorAll('.md\\:block .action-menu a > p')
+      const dates = doc.querySelectorAll('.md\\:block .action-menu a p')
 
       for (let i = 0; i < bookCount; i++) {
         const date = new Date(
-          dates[i].textContent.replace('Started ', '').split('\n')[0]
-        ).toLocaleString('en-US', {
-          timeZone: 'America/Los_Angeles',
-        })
+          dates[i]?.textContent.replace('Started ', '').split('\n')[0]
+          ).toLocaleString('en-US', {
+            timeZone: 'America/Los_Angeles',
+          })
 
         if (!data[i]) {
-          data.push({ title: titles[i].textContent })
-          data.push({ author: authors[i].textContent })
+          data.push({ title: titles[i]?.textContent })
+          data.push({ author: authors[i]?.textContent })
           data.push({
             image: images[i].src.replace(
               'https://cdn.thestorygraph.com',
@@ -45,7 +43,7 @@ export default async function () {
             ),
           })
           data.push({ url: `https://app.thestorygraph.com${urls[i].href}` })
-          data.push({ percentage: percentages[i].textContent })
+          data.push({ percentage: percentages[i]?.textContent })
           data.push({
             dateAdded: date,
           })
@@ -53,14 +51,14 @@ export default async function () {
         }
 
         if (data[i]) {
-          data[i]['title'] = titles[i].textContent
-          data[i]['author'] = authors[i].textContent
-          data[i]['image'] = images[i].src.replace(
+          data[i]['title'] = titles[i]?.textContent
+          data[i]['author'] = authors[i]?.textContent
+          data[i]['image'] = images[i]?.src.replace(
             'https://cdn.thestorygraph.com',
             'https://cd-books.b-cdn.net'
           )
-          data[i]['url'] = `https://app.thestorygraph.com${urls[i].href}`
-          data[i]['percentage'] = percentages[i].textContent
+          data[i]['url'] = `https://app.thestorygraph.com${urls[i]?.href}`
+          data[i]['percentage'] = percentages[i]?.textContent
           data[i]['dateAdded'] = date
           data[i]['type'] = 'book'
         }