Book Image Shortcode for Hugo
Lets link to bookshop
- tags
- hugo
Contents
HappyFunCorp helped build Bookshop.org which is an online bookstore that distributes 30% of the book profits to independent bookstores in your area. Basically it’s a place that people can link to that isn’t Amazon, and that will kick some money to your local community.
I wanted to find a way to easily create links to these product pages, and show images if needed. There’s no API to access bookshop (I asked on our internal dev channel) but I know how to write a script so I cobbled something together and made a hugo
shortcode to render stuff on my blog.
{{< bookshop isbn=“9780394719856” />}}
Which will generate this, a embedded link to the product page of the book with the title pulled from the server:
The Gay Science: With a Prelude in Rhymes and an Appendix of Songs is a great book.
Or
{{< bookshop isbn="9780394719856" img="left">}}
Here are my thoughts about this book
{{< /bookshop >}}
Here are my thoughts about this book.
We are going to use a two step process here. The first is to use a bash script to pull the data from the site and store it in json
and jpg
, and then a shortcode to format this in the webpage. Most of my posts are directories, so this will be part of the page bundles.
Get the book information
Since there’s no API and I want to keep this simple, our strategy is to:
- Hit the search page with the ISBN
- Use
awk
to pull put the link from the search results. - Load the product page.
- Use an unholy combination of grep, sed, and awk to pull out the meta data.
- Store this in
${ISBN}.json
- Download the cover image in
${ISBN}.jpg
|
|
The shortcode
I’m not an expert in hugo shortcodes, but this is what we’re doing.
- Look for the file
${ISBN}.json
in the page bundle. - Show a message if we don’t find it.
- Pull in data into a map using
$jsonFile.Content | unmarshal
- If we are showing the cover image, choose which set of bootstrap utility classes we want to use.
- Wrap the output in
<p class="clearfix">
to deal with the overfloat. Can’t believe I’m still doing this. - Link the image or the title to the bookshop product page, data pulled from the json file.
- Add the
{{ .Inner }}
content inside of the tag.
bookshop.html
:
/link to bookship.html/ TODO
Example output:
Dui, id ornare arcu odio ut sem nulla pharetra diam sit amet nisl suscipit adipiscing bibendum est ultricies integer quis auctor! Odio eu feugiat pretium, nibh ipsum consequat nisl, vel. Ac tortor dignissim convallis? Tincidunt nunc pulvinar sapien et ligula ullamcorper? Egestas diam in arcu cursus euismod quis viverra nibh cras pulvinar mattis nunc, sed blandit libero volutpat sed cras ornare arcu dui vivamus?
References
Previously
Next