Skip to content

Instantly share code, notes, and snippets.

@AdamBuchweitz
Forked from basilioss/goodreads-frontmatter.js
Last active June 29, 2022 17:39
Show Gist options
  • Save AdamBuchweitz/58b41d0eb96e0d6b4984e9153a7461fd to your computer and use it in GitHub Desktop.
Save AdamBuchweitz/58b41d0eb96e0d6b4984e9153a7461fd to your computer and use it in GitHub Desktop.
This is an Obsidian Templater script that generates frontmatter for literature notes from Goodreads link
---<%*
let url = await tp.system.clipboard()
let page = await tp.obsidian.request({url})
let p = new DOMParser()
let doc = p.parseFromString(page, "text/html")
let $ = s => doc.querySelector(s)
let $A = s => doc.querySelectorAll(s)
let title = $("h1[id='bookTitle']").innerHTML.trim()
let shortTitle = /[A-Za-z0-9_ ]+/.exec(title)[0]
%>
<% tp.file.rename(shortTitle) %>
title: "<% title %>"
authors: [<%* let authors = $A("span[itemprop=name]")
var keys = Array.from(authors, authors => authors.textContent)
tR += keys.join(', ') %>]
isbn: <% $("meta[property='books:isbn']").content %>
publishedDate: <%*
var pubDate = ""
let nobr = $("nobr[class='greyText']")
if (nobr)
{
pubDate = nobr.innerHTML
}
else
{
let details = $A("div[id='details'] div.row")
pubDate = details[details.length -1].innerHTML
}
const regex = /\d{4}/
let match = regex.exec(pubDate)
if (match)
{
tR += match[0]
}
%>
series:
genres: [<%* let genres = $A("a[class='actionLinkLite bookPageGenreLink']")
var keys = Array.from(genres, genres => genres.textContent.toLowerCase())
tR += keys.join(', ') %>]
cover: "<% $("img[id='coverImage']").src %>"
tags: [book]
status: Unread
---
Up:: [[Books]]
Author:: [[<% authors[0].textContent %>]]
@AdamBuchweitz
Copy link
Author

Revisions:

  • Get publication date, in either format
  • Tag as book
  • Default status to "Unread"
  • Added Dataview fields for Up and Author, to support Breadcrumbs

@AdamBuchweitz
Copy link
Author

AdamBuchweitz commented Jun 29, 2022

Latest version will also rename the file to the titled of the book, shortened to avoid non-word characters like colons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment