Skip to content

Instantly share code, notes, and snippets.

@skippednote
Created September 19, 2023 03:56
Show Gist options
  • Save skippednote/fe77cf00d096a3328eef062590377744 to your computer and use it in GitHub Desktop.
Save skippednote/fe77cf00d096a3328eef062590377744 to your computer and use it in GitHub Desktop.
import fs from "node:fs";
const booksJSON = fs.readFileSync("./static/data/books.json", "utf-8");
const books = JSON.parse(booksJSON);
function slugify(str) {
return String(str)
.normalize("NFKD") // split accented characters into their base characters and diacritical marks
.replace(/[\u0300-\u036f]/g, "") // remove all the accents, which happen to be all in the \u03xx UNICODE block.
.trim() // trim leading or trailing whitespace
.toLowerCase() // convert to lowercase
.replace(/[^a-z0-9 -]/g, "") // remove non-alphanumeric characters
.replace(/\s+/g, "-") // replace spaces with hyphens
.replace(/-+/g, "-"); // remove consecutive hyphens
}
function generateMDFromJSON(book) {
return `+++
title = "${book.title}"
date = "${book.date}"
[extra]
author = "${book.author}"
isbn = "${book.isbn}"
isbn13 = "${book.isbn13}"
pages = "${book.pages}"
rating = "${book.rating}"
dateRead = "${book.dateRead}"
dateAdded = "${book.dateAdded}"
+++
`;
}
books.forEach((book) => {
fs.writeFileSync(
"./content/books/" + slugify(book.title) + ".md",
generateMDFromJSON(book)
);
});
// Go to https://www.goodreads.com/review/list/51138437-bassam-ismail?utf8=✓&shelf=read&sort=date_read&title=bassam-ismail&per_page=100
// and paste the script in the console. This is will grab the details of the books visible (100 books)
// and copy it to the clipboard.
// Save the copied data in `static/data/books.json`
let trs = [...document.querySelector("#booksBody").querySelectorAll("tr")];
let books = trs.map((tr) => {
const title = tr
.querySelector(".title .value")
.textContent.trim()
.split("\n")
.join(" ");
const author = tr.querySelector(".author .value a").textContent.trim();
const isbn = tr.querySelector(".isbn .value").textContent.trim();
const isbn13 = tr.querySelector(".isbn13 .value").textContent.trim();
const pages = tr
.querySelector(".num_pages .value")
.textContent.trim()
?.split("\n")?.[0];
const rating = tr
.querySelector(".rating .value .stars")
.dataset.rating.trim();
const dateRead = tr
.querySelector(".date_read .date_read_value")
?.textContent.trim();
const dateAdded = tr.querySelector(".date_added .value").textContent.trim();
const date = dateRead ? new Date(dateRead).toISOString() : "";
return {
title,
date,
author,
isbn,
isbn13,
pages,
rating,
dateRead,
dateAdded,
};
});
copy(books);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment