Hi fellow travelers!
I love using the Book Search plugin by anpigon, the only problem is the {{author}} field is a single string, which is okay for single author books but a bit messy for multi author books as I like to turn my author’s names into links so I can have author pages that list all the books by that author (I also manually include bio info from Goodreads or Wikipedia).
Any way… I needed a way to split and reformat the {{author}} field so I came up with this.
Be warned this is a complete hack job and probably needs cleaning up by someone that knows what they are doing
So here is my new book template. I hope it’s useful to someone.
EDIT: Sorry the code highlighter doesn’t like my inline comments.
---
title: {{title}}
series:
seriesNum:
<%*
let authors = "{{author}}";
const names = authors.split(", ")
var arrayLength = names.length;
let authorFL = "[[" + names[0] + "]]";
for (var i = 1; i < arrayLength; i++) {
authorFL = "[[" + names[i] + "]]" + ", " + authorFL;
}
const result = []
for (const name of names) {
const nameParts = name.trim().split(" ") //remove redundant space and break the string down into array items (names)
const lastName = nameParts.pop() //take the last item
nameParts.unshift(lastName + ",") //add the last item to the first position
result.push(`"${nameParts.join(" ")}"`) //add "" to a name
}
const authorLF = result.join(", ") //make the entire array to become a string with a separator `,`
-%>
authors: <% authorFL %>
authorLF: <% authorLF %>
publisher: {{publisher}}
publish: {{publishDate}}
pages: {{totalPage}}
isbn10: {{isbn10}}
isbn13: {{isbn13}}
asin:
read:
cover: {{coverUrl}}
---
# [[{{title}}]]
By <% authorFL %>
